• No results found

Pushing images to the Docker Hub

In document Learning Docker (Page 84-89)

Here, we will create a Docker image on the local machine, and push this image to the 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 dockercommit sub command

° Using the docker commit sub command with Dockerfile 2. Push this created image to the Docker Hub.

3. Delete the image from the 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 have seen the creation of the Docker image using Dockerfile. You may refer to this to check for details of the Dockerfile syntax.

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

$ sudo docker run -i --name="containerforhub" -t ubuntu /bin/bash root@e3bb4b138daf:/#

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@bd7cc5df6d96:/# mkdir mynewdir root@bd7cc5df6d96:/# cd mynewdir

root@bd7cc5df6d96:/mynewdir# echo 'this is my new container to make image and then push to hub' >mynewfile

root@bd7cc5df6d96:/mynewdir# cat mynewfile

This is my new container to make image and then push to hub root@bd7cc5df6d96:/mynewdir#

Let's build the new image with the docker commit command from the container, which has just been created. Note that the commit command would be executed from the host machine, from where the container is running, and not from inside this container:

$ sudo docker commit -m="NewImage" containerforhub vinoddandy/imageforhub

3f10a35019234af2b39d5fab38566d586f00b565b99854544c4c698c4a395d03

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

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

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

$ sudo docker push vinoddandy/imageforhub

The push refers to a repository [vinoddandy/imageforhub] (len: 1) Sending image list

Pushing tag for rev [c664d94bbc55] on {https://cdn-registry- 1.docker.io/v1/repositories/vinoddandy/imageforhub/tags/latest }

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

To test the image from the 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/imageforhub image:

$ sudo docker rmi vinoddandy/imageforhub

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

$ sudo docker run -i --name="newcontainerforhub" -t vinoddandy/imageforhub /bin/bash

Unable to find image 'vinoddandy/imageforhub' locally Pulling repository vinoddandy/imageforhub

c664d94bbc55: Pulling image (latest) from vinoddandy/imageforhub, endpoint: http

c664d94bbc55: Download complete 5506de2b643b: Download complete

root@9bd40f1b5585:/# cat /mynewdir/mynewfile

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

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

Finally, we will delete the image from the Docker Hub using https://registry.

hub.docker.com/u/vinoddandy/imageforhub/ and then click on Delete Repository, as shown in the following screenshot:

We'll again create this image but 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 the Docker Hub.

The Dockerfile on the local machine is as follows:

###########################################

# Dockerfile to build a new image

###########################################

# Base image is Ubuntu FROM ubuntu:14.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 build the image locally using the following command:

$ sudo docker build -t="vinoddandy/dockerfileimageforhub" . Sending build context to Docker daemon 2.56 kB

Sending build context to Docker daemon Step 0 : FROM ubuntu:14.04

---> 5506de2b643b

Step 1 : MAINTAINER Vinod Singh <[email protected]>

---> Running in 9f6859e2ca75 ---> a96cfbf4a810

removing intermediate container 9f6859e2ca75 Step 2 : RUN mkdir mynewdir

---> Running in d4eba2a31467 ---> 14f4c15610a7

removing intermediate container d4eba2a31467 Step 3 : RUN touch /mynewdir/mynewfile ---> Running in 7d810a384819

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

$ sudo docker run -i --name="dockerfilecontainerforhub" –t vinoddandy/dockerfileimageforhub

root@d3130f21a408:/# 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 the Docker Hub, and push this newly created image:

$ sudo docker login Username (vinoddandy):

Login Succeeded

$ sudo docker push vinoddandy/dockerfileimageforhub

The push refers to a repository [vinoddandy/dockerfileimageforhub]

(len: 1)

Sending image list

Pushing repository vinoddandy/dockerfileimageforhub (1 tags) 511136ea3c5a: Image already pushed, skipping

d497ad3926c8: Image already pushed, skipping b5bbd55f221c: Image successfully pushed bcd8f63cfa79: Image successfully pushed 224affbf9a65: Image successfully pushed Pushing tag for rev [224affbf9a65] on {https://cdn-registry-1.docker.io/v1/repos

itories/vinoddandy/dockerfileimageforhub/tags/latest}

$

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

Automating the building process for

In document Learning Docker (Page 84-89)