While working with Docker, you’ll often spend time pushing and pulling single images or full image repositories to and from the public Docker registry. The Docker registry is an online public location, commonly known as Docker Hub, where you store your Docker images. Images are organized in repositories, which are sort of directories containing one or more images from the same source but with different tag.
You can for instance, have a base minimal Ubuntu image tagged as v1.0, and the same Ubuntu image with some additional packages installed and tagged as v2.0. You would group these images in a public or private repository on Docker Hub.
In fact, a repository can also be created locally on your Docker host where you’ll have all your images on your local server, but this is not considered a best practice, because of the risk to loose all your customized images should any unexpected failure happens to the Docker host.
This is why it’s a common practice to link then push your local repositories to Docker Hub. Hence, having all your images online, protected from any eventual failure, and available to any host that can actually connect to Docker Hub.
Docker images can also be pushed to an on-premise registry when you have the Docker Trusted Registry installed in your environment, but this features comes only with the Enterprise Edition of Docker.
In this post we will go through the steps on how to push your local repository containing your different customized and tagged images to Docker Hub.
We have on an Ubuntu local image hosted on a Docker host that we will use for this demonstration. It has the latest tag.
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 735f80812f90 4 weeks ago 83.5MB
Using the tag argument, the command syntax looks like this:
$ docker tag imageID RepositoryName/imageName:tag
So to create a tagged image from the ubuntu image and place it in a local repository named mouradn81, we will use the following command:
$ docker tag 735f80812f90 mouradn81/ubuntu:v1.0
Because we want to push this local repository to the online registry, the repository name needs to match your Docker ID because this is the base name for any Docker online repository.
We have now two Ubuntu images, one of them is in the local repository mouradn81 and tagged v1.0.
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 735f80812f90 4 weeks ago 83.5MB mouradn81/ubuntu v1.0 735f80812f90 4 weeks ago 83.5MB
Note that both images have the same IMAGE ID value, which is normal, they are the same object but tagged differently.
Similarly, let’s create two other images, each with one different tag
$ docker tag 735f80812f90 mouradn81/ubuntu:v2.0 $ docker tag 735f80812f90 mouradn81/ubuntu:v3.0
Tagging the same image does not have much sense. An image is usually tagged after some changes have been applied to it, like for instance, installing some dependencies and libraries using a Docker File. In our case, this is just a demo to show the tag argument use case to have three different images.
We now have three local images in our local repository
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE mouradn81/ubuntu v1.0 735f80812f90 4 weeks ago 83.5MB mouradn81/ubuntu v2.0 735f80812f90 4 weeks ago 83.5MB mouradn81/ubuntu v3.0 735f80812f90 4 weeks ago 83.5MB ubuntu latest 735f80812f90 4 weeks ago 83.5MB
Time now to push our local repository to the public Docker registry. The first thing to do is to login to Docker Hub. If you don’t have a Docker ID, create one from here.
Currently, I have only one repository visible from my Docker Hub Dashboard.
$ 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: mouradn81 Password: Login Succeeded
Once logged in, you can proceed with the repository push.
$ docker push mouradn81/ubuntu
If everything goes fine, you’ll have an output similar to this.
The push refers to repository [docker.io/mouradn81/ubuntu] 268a067217b5: Mounted from library/ubuntu c01d74f99de4: Mounted from library/ubuntu ccd4d61916aa: Mounted from library/ubuntu 8f2b771487e9: Mounted from library/ubuntu f49017d4d5ce: Mounted from library/ubuntu v1.0: digest: sha256:958eaeb7e33e6c4f68f7fef69b35ca178c7f5fb0dd40db7b44a8b9eb692b9bc5 size: 1357 268a067217b5: Layer already exists c01d74f99de4: Layer already exists ccd4d61916aa: Layer already exists 8f2b771487e9: Layer already exists f49017d4d5ce: Layer already exists v2.0: digest: sha256:958eaeb7e33e6c4f68f7fef69b35ca178c7f5fb0dd40db7b44a8b9eb692b9bc5 size: 1357 268a067217b5: Layer already exists c01d74f99de4: Layer already exists ccd4d61916aa: Layer already exists 8f2b771487e9: Layer already exists f49017d4d5ce: Layer already exists v3.0: digest: sha256:958eaeb7e33e6c4f68f7fef69b35ca178c7f5fb0dd40db7b44a8b9eb692b9bc5 size: 1357
Repository pushed. If we check now the Docker Hub Dashboard, we can see that our repository is there.
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE
You should be able to pull back down the images previously pushed up to Docker Hub.
$ docker pull mouradn81/ubuntu:v2.0 v2.0: Pulling from mouradn81/ubuntu c64513b74145: Pull complete 01b8b12bad90: Pull complete c5d85cf7a05f: Pull complete b6b268720157: Pull complete e12192999ff1: Pull complete Digest: sha256:958eaeb7e33e6c4f68f7fef69b35ca178c7f5fb0dd40db7b44a8b9eb692b9bc5 Status: Downloaded newer image for mouradn81/ubuntu:v2.0
Once complete, your image should be present again on your local host.
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE mouradn81/ubuntu v2.0 735f80812f90 4 weeks ago 83.5MB