Pushing images to Docker Hub

Here, we will create a Docker image on the local machine and push this image to Docker Hub. You need to perform the following steps in this section:

  1. Create a Docker image on the local machine by doing one of the following:
  • Using the docker commit subcommand
  • Using the docker commit subcommand with Dockerfile
  1. Pushing this created image to Docker Hub
  2. Deleting the image from Docker Hub

We will use the ubuntu base image, run the container, add a new directory and a new file, and then create a new image. In Chapter 3, Building Images, we saw how to create a Docker image using Dockerfile. You may refer to that chapter to check for details of the Dockerfile syntax.

We will run the container with the containerforhub name from the base ubuntu image, as shown in the following Terminal code:

$ sudo docker run -i --name="containerforhub" -t ubuntu /bin/bash 
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
952132ac251a: Pull complete
Digest: sha256:f4691c96e6bbaa99d99ebafd9af1b68ace2aa2128ae95a60369c506dd6e6f6ab
Status: Downloaded newer image for ubuntu:latest
root@1068a1fae7da:/#

Next, we'll create a new directory and file in the containerforhub container. We will also update the new file with some sample text to test later:

root@1068a1fae7da:/# mkdir mynewdir 
root@1068a1fae7da:/# cd mynewdir
root@1068a1fae7da:/mynewdir# echo 'this is my new container to make image and then push to hub' > mynewfile
root@1068a1fae7da:/mynewdir# cat mynewfile
this is my new container to make image and then push to hub
root@1068a1fae7da:/mynewdir#

Let's build the new image with the docker commit command from the container, which has just been created.

The commit command will be executed from the host machine, from where the container is running, and not from inside this container:
$ sudo docker commit -m="NewImage for second edition" containerforhub  vinoddandy/imageforhub2 
sha256:619a25519578b0525b4c098e3d349288de35986c1f3510958b6246fa5d3a3f56

You should use your own username of Docker Hub in place of vinoddandy to create the image.

Now, we have a new Docker image available on the local machine with the vinoddandy/imageforhub2 name. At this point, a new image with mynewdir and mynewfile is created locally:

$ sudo docker images -a 
REPOSITORY TAG IMAGE ID CREATED SIZE
vinoddandy/imageforhub2 latest 619a25519578
2 minutes ago 126.6 MB

We will log in to Docker Hub using the sudo docker login command, as discussed earlier in this chapter.

Let's push this image to Docker Hub from the host machine:

$ sudo docker push vinoddandy/imageforhub2 
The push refers to a repository [docker.io/vinoddandy/imageforhub2]
0ed7a0595d8a: Pushed
0cad5e07ba33: Mounted from library/ubuntu
48373480614b: Mounted from library/ubuntu
latest: digest: sha256:cd5a86d1b26ad156b0c74b0b7de449ddb1eb51db7e8ae9274307d27f810280c9 size: 1564

Now, we'll login to Docker Hub and verify the image in Repositories.

To test the image from Docker Hub, let's remove this image from the local machine. To remove the image, first we need to stop the container and then delete the container:

$ sudo docker stop containerforhub 
$ sudo docker rm containerforhub

We will also delete the vinoddandy/imageforhub2 image:

$ sudo docker rmi vinoddandy/imageforhub2
Untagged: vinoddandy/imageforhub2:latest
Untagged: vinoddandy/imageforhub2@sha256:cd5a86d1b26ad156b0c74b0b7de449ddb1eb51db7e8ae9274307d27f810280c9
Deleted: sha256:619a25519578b0525b4c098e3d349288de35986c1f3510958b6246fa5d3a3f56

We will pull the newly created image from Docker Hub, and run the new container on the local machine:

$ sudo docker run -i --name="newcontainerforhub" -t  vinoddandy/imageforhub2 /bin/bash 
Unable to find image 'vinoddandy/imageforhub2:latest' locally
latest: Pulling from vinoddandy/imageforhub2

952132ac251a: Already exists
82659f8f1b76: Already exists
Digest: sha256:cd5a86d1b26ad156b0c74b0b7de449ddb1eb51db7e8ae9274307d27f810280c9
Status: Downloaded newer image for vinoddandy/imageforhub2:latest

root@9dc6df728ae9:/# cat /mynewdir/mynewfile
this is my new container to make image and then push to hub
root@9dc6df728ae9::/#

So, we have pulled the latest image from Docker Hub and created the container with the new vinoddandy/imageforhub2 image. Make a note that the Unable to find image 'vinoddandy/imageforhub2:latest' locally message confirms that the image is downloaded from the remote repository of Docker Hub.

The text in mynewfile verifies that it is the same image that was created earlier.

Finally, we will delete the image from Docker Hub at https://hub.docker.com/r/vinoddandy/imageforhub2/ and then click on Settings and then Delete, as shown in the following screenshot:

We'll again create this image, but now using the Dockerfile process. So, let's create the Docker image using the Dockerfile concept explained in Chapter 3, Building Images, and push this image to Docker Hub.

The Dockerfile on the local machine is as follows:

########################################### 
# Dockerfile to build a new image
###########################################
# Base image is Ubuntu
FROM ubuntu:16.04
# Author: Dr. Peter
MAINTAINER Dr. Peter <[email protected]>
# create 'mynewdir' and 'mynewfile'
RUN mkdir mynewdir
RUN touch /mynewdir/mynewfile
# Write the message in file
RUN echo 'this is my new container to make image and then push to hub'
>/mynewdir/mynewfile

Now we'll build the image locally using the following command:

$ sudo docker build -t="vinoddandy/dockerfileimageforhub1" .
Sending build context to Docker daemon 16.74 MB
Step 1 : FROM ubuntu:16.04
16.04: Pulling from library/ubuntu
862a3e9af0ae: Pull complete
7a1f7116d1e3: Pull complete
Digest: sha256:5b5d48912298181c3c80086e7d3982029b288678fccabf2265899199c24d7f89
Status: Downloaded newer image for ubuntu:16.04
---> 4a725d3b3b1c
Step 2 : MAINTAINER Dr. Peter <[email protected]>
---> Running in 5be5edc9b970
---> 348692986c9b
Removing intermediate container 5be5edc9b970
Step 3 : RUN mkdir mynewdir
---> Running in ac2fc73d75f3
---> 21585ffffab5
Removing intermediate container ac2fc73d75f3
Step 4 : RUN touch /mynewdir/mynewfile
---> Running in c64c98954dd3
---> a6304b678ea0
Removing intermediate container c64c98954dd3
Step 5 : RUN echo 'this is my new container to make image and then push to hub' > /mynewdir/mynewfile
---> Running in 7f6d087e29fa
---> 061944a9ba54
Removing intermediate container 7f6d087e29fa
Successfully built 061944a9ba54

We'll run the container using this image, as shown here:

$ sudo docker run -i --name="dockerfilecontainerforhub" -t vinoddandy/dockerfileimageforhub1 /bin/bash 

root@236bfb39fd48:/# cat /mynewdir/mynewfile
this is my new container to make image and then push to hub

This text in mynewdir confirms that the new image is built properly with a new directory and a new file.

Repeat the login process in Docker Hub and push this newly created image:

$ sudo 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 (vinoddandy): vinoddandy
Password:
Login Succeeded

$ sudo docker push vinoddandy/dockerfileimageforhub1
The push refers to a repository [docker.io/vinoddandy/dockerfileimageforhub1]
92e394693590: Pushed
821a2be25576: Pushed
dca059944a2e: Pushed
ffb6ddc7582a: Mounted from library/ubuntu
344f56a35ff9: Mounted from library/ubuntu
530d731d21e1: Mounted from library/ubuntu
24fe29584c04: Mounted from library/ubuntu
102fca64f924: Mounted from library/ubuntu
latest: digest: sha256:c418c88f260526ec51ccb6422e2c90d0f6fc16f1ab81da9c300160d0e0f7bd87 size: 1979

Finally, we can verify the availability of the image on Docker Hub:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset