Image Operations (Pull, Push, Tag)
Whenever we run a container using an image, it first checks for the image locally, and later if it doesn't find any, it looks for it on the docker hub.
Docker Hub
Docker Hub repositories is a platform where you can share container images with your team, customers and also find other images shared by the Docker community.
Docker images are pushed to Docker Hub through the docker push command. A single Docker Hub repository can hold many Docker images with the help of tags.
To pull the latest image, use the command suggested on the docker hub portal as follows:
Suppose we want to pull an ubuntu image, use the below command to pull and by default it will pull the latest tag.
And to pull a specific version, use the below tags along with the command-
learning-ocean:~ gaurav$ docker image pull ubuntu:14.04
Prerequisite to use Docker Hub
Create repositories in Docker Hub. To create a repository, sign in to Docker Hub, click on Repositories then Create Repository.
Pushing a Docker image to Docker Hub
To push an image to Docker Hub, you must first name your local image using your Docker Hub username and the repository name that you created through Docker Hub on the web.
You need to add the images to a repository by appending a specific :<tag> to them-
$ docker tag <existing-image> <hub-user>/<repo-name>[:<tag>]
By default, the docker hub repo will be hub.docker.com, but if you want to push it to the custom repo, you have to pass it in the above command. Please refer to official documentation for the same. Once the image is tagged, use the docker login command to log in to the docker hub from CLI.
$ docker login
Username: gauravsharma6421
Password:
Login Succeeded
Once we have logged in, we can now push our container to Docker Hub.
Now you can push this repository to the docker hub registry-
$ docker push <hub-user>/<repo-name>:<tag>
The image is then uploaded and available for use.
Example
Suppose we want to publish the below image myubuntu_free_git to docker hub which has tree and git installed in it.
learning-ocean:~ gaurav$ docker image ls
REPOSITORY Â Â Â Â Â Â Â Â TAG Â Â Â Â Â Â Â Â Â Â Â IMAGE ID Â Â Â CREATED Â Â Â Â SIZE
newubuntu         latest          f74a9dbd167e  5 hours ago   197MB
myubuntu_free_git     latest          aee602b47ab0  5 hours ago   215MB
ubuntu           14.04           13b66b487594  3 months ago  197MB
learning-ocean:~ gaurav$Â
Tag the image-
docker image tag myubuntu_free_git gauravsharma6421/myubuntu_free_git
Listing the images-
learning-ocean:~ gaurav$ docker image ls
REPOSITORY Â Â Â Â Â Â Â TAG Â Â Â Â Â Â Â Â Â IMAGE ID Â Â Â CREATED Â Â Â Â SIZE
myubuntu_free_git    latest        aee602b47ab0  5 hours ago   215MB
gauravsharma6421/myubuntu_free_git  latest        aee602b47ab0  5 hours ago   215MB
ubuntu          14.04         13b66b487594  3 months ago  197MB
As you can see, the image id of both the images is the same.
Now, login to docker-
learning-ocean:~ gaurav$ docker loginÂ
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: gauravsharma6421
Password:Â
Login Succeeded
Now run the push command-
learning-ocean:~ gaurav$ docker image push gauravsharma6421/myubuntu_free_git
Using default tag: latest
The push refers to repository [docker.io/gauravsharma6421/myubuntu_free_git]
d427f7a0731f: Pushing [=> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ]Â 5.332MB/214.6MB
Verifying the same on docker hub And, if someone wants to pull this image again, run the command suggested by docker hub as below-
learning-ocean:~ gaurav$ docker image pull gauravsharma6421/myubuntu_free_git
Create a container out of this image and check whether we have tree and git installed-
learning-ocean:~ gaurav$ docker container run -it gauravsharma6421/myubuntu_free_git