How to push an image to Docker hub

Introduction

In our previous post, we explained some of the ways we could work with containers like running commands on containers during startup and also while they were running. We also explained how to start and connect to stop containers and also how to remove stopped containers. Also, we briefly talked about images and explained how to remove an image. In this post, we will explain step by step how to push an image we created to our docker hub repository.

Step 1: Login to docker hub account
In order to push an image to the Docker hub, we must first log in to our docker hub account on the system where the image is located and the docker container engine is running.

[sahil@linuxnix ~]$ 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: sahilsuri008
Password:
WARNING! Your password will be stored unencrypted in /home/sahil/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded
[sahil@linuxnix ~]$

As you can see our login succeeded.

Step 2: Tag the image to upload
Let’s see the images that we have available on our system with the docker image ls command.

[sahil@linuxnix ~]$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> e1c1d07a11b5 8 hours ago 182MB
ubuntu 16.04 13c9f1285025 2 weeks ago 119MB
[sahil@linuxnix ~]$

We will tag the image that we had earlier created from the Ubuntu 16.04 image. I’ll tag our image as v1 under the repository linuxnix-docker.

[sahil@linuxnix ~]$ docker tag e1c1d07a11b5 sahilsuri008/linuxnix-docker:v1

Now if we run the docker image ls command again we will see that our image tag has been updated.

[sahil@linuxnix ~]$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
sahilsuri008/linuxnix-docker v1 e1c1d07a11b5 8 hours ago 182MB
ubuntu 16.04 13c9f1285025 2 weeks ago 119MB
[sahil@linuxnix ~]$

Step 3: Push the image to docker hub
To push the image to our docker hub account we will use the docker push command.

[sahil@linuxnix ~]$ docker push sahilsuri008/linuxnix-docker:v1
The push refers to repository [docker.io/sahilsuri008/linuxnix-docker]
54e9d4f04a1f: Pushed
7377153e4ada: Pushed
92d3f22d44f3: Mounted from library/ubuntu
10e46f329a25: Mounted from library/ubuntu
24ab7de5faec: Mounted from library/ubuntu
1ea5a27b0484: Mounted from library/ubuntu
v1: digest: sha256:b7f1085ee77aff13550f5881dda8d961d47b5e142a1ade8dde8452017137bb86 size: 1574
[sahil@linuxnix ~]$
[sahil@linuxnix ~]$ cat linuxnix-docker/dockerfile
FROM ubuntu:16.04 RUN apt-get update RUN apt-get install -y python3
[sahil@linuxnix ~]$

In the output of the above command, the two layers that get pushed are the ones that we added for the apt-get update and the python3 install respectively. The other four layers that show up as mounted are the ones that made up the Ubuntu 16.04 image that we used as a base for our own image.

Linuxnix-free-e-book

Step 4: Verify that you can pull this image to your system
Since we’ve successfully pushed the image to our docker hub account we’ll now verify that we are able to pull this image back to our system. For this, we will first remove the image and then download it from the docker hub.

[sahil@linuxnix ~]$ docker image rm e1c1d07a11b5
Untagged: sahilsuri008/linuxnix-docker:v1
Untagged: sahilsuri008/linuxnix-docker@sha256:b7f1085ee77aff13550f5881dda8d961d47b5e142a1ade8dde8452017137bb86
Deleted: sha256:e1c1d07a11b5f9d2c73dba4a860315f984e7e4a8347acc1b6993a9e5a487012b
Deleted: sha256:83a13d31d711d2b9efe23e7e474a369f2a46c6d69fc7e23b3d3cc7ddc0a8a896
Deleted: sha256:fac336606358210f37a469c1da6599f934f1fc77aebf695ea793eac4cc152029
Deleted: sha256:2f629305b9750d556ba601bba251fc08c087d0187f5f470a7d8cf4409ff2cf0a
[sahil@linuxnix ~]$
[sahil@linuxnix ~]$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 16.04 13c9f1285025 2 weeks ago 119MB
[sahil@linuxnix ~]$
[sahil@linuxnix ~]$ docker pull sahilsuri008/linuxnix-docker:v1
v1: Pulling from sahilsuri008/linuxnix-docker
35b42117c431: Already exists
ad9c569a8d98: Already exists
293b44f45162: Already exists
0c175077525d: Already exists
ce77da78a2c2: Pull complete
b0929421459c: Pull complete
Digest: sha256:b7f1085ee77aff13550f5881dda8d961d47b5e142a1ade8dde8452017137bb86
Status: Downloaded newer image for sahilsuri008/linuxnix-docker:v1
[sahil@linuxnix ~]$
[sahil@linuxnix ~]$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
sahilsuri008/linuxnix-docker v1 e1c1d07a11b5 8 hours ago 182MB
ubuntu 16.04 13c9f1285025 2 weeks ago 119MB
[sahil@linuxnix ~]$

With this, we’ve confirmed that we are indeed able to pull down images from our own docker hub account.

Conclusion

This concludes our post on understanding how to push container images to docker hub. We hope that you’ve found this post to be useful and look forward towards your feedback and suggestion.

The following two tabs change content below.

Sahil Suri

He started his career in IT in 2011 as a system administrator. He has since worked with HP-UX, Solaris and Linux operating systems along with exposure to high availability and virtualization solutions. He has a keen interest in shell, Python and Perl scripting and is learning the ropes on AWS cloud, DevOps tools, and methodologies. He enjoys sharing the knowledge he’s gained over the years with the rest of the community.

Latest posts by Sahil Suri (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *