Create Docker Images for Docker Hub

Create Docker Images for Docker Hub

Creating a Docker Image

We could create our own Docker image in two ways:

1. Using Dockerfile to Create an Image

A Dockerfile is a simple text document that contains a series of commands that Docker uses to build an image. Several commands supported in Dockerfile are FROM, CMD, ENTRYPOINT, VOLUME, ENV, and more. A simple Dockerfile looks as follows:

Dockerfile

FROM busybox:latest
CMD ["date"]

Note: An important point to remember is that this file should be named as Dockerfile.

The docker build command builds an image from a Dockerfile. To build the image from our above Dockerfile, use this command:

1$ docker build -t example_image .
2Sending build context to Docker daemon  2.048kB
3Step 1/2 : FROM busybox:latest
4---> db8ee88ad75f
5Step 2/2 : CMD ["date"]
6---> Using cache
7---> b89335fdacdf
8Successfully built b89335fdacdf
9Successfully tagged example-image:latest

Now, let’s run a container based on our image. You will find that it will print out the date as shown below:

1$ docker run -it --name example_app example_image
2Sun Jul 21 20:52:20 UTC 2019

2. Create an Image from a Container

Another way to create an image is by pulling a Docker image, creating a container from it, and then modifying or making changes in it like installing our app in that container. Now, using the docker commit command, we can create a Docker image from the container.

Let's look at an example of how to create a Docker image from our example_app container:

1$ docker ps -a
2CONTAINER ID     IMAGE      COMMAND    CREATED               STATUS              NAMES
3c3df2dd33276  example-image "date"  5 seconds ago   Exited (0) 4 seconds ago  example_app
4
5$ docker commit example_app example_image2:latest
6sha256:7b48e8355aa7a7ea32d554f26d0bd21f4d069d8526c68f1d098acac9111a9adf

Publish an Image to Docker Hub

To be able to publish our Docker images to Docker Hub, there are some steps that we need to follow:

Step 1: Sign Up for Docker Hub

Before we can push our image to Docker Hub, we will first need to have an account on Docker Hub. Create an account by visiting this link. The signup process is relatively simple.

Step 2: Create a Repository on Docker Hub

For uploading our image to Docker Hub, we first need to create a repository. To create a repo:

  1. Sign in to Docker Hub

  2. Click on ‘Create Repository’ on the Docker Hub welcome page:

  1. Fill in the repository name as example-image, the Docker image that we created earlier using Dockerfile. Also, describe your repo like "My First Repository". Finally, click on the create button. Refer to the below screenshot:

Step 3: Push the Image to Docker Hub

Now we will push our built image to the Docker Hub registry:

  1. Log into the Docker public registry from your local machine terminal using Docker CLI:
1$  docker login
  1. Tag the image

    This is a crucial step that is required before we can upload our image to the repository. As we discussed earlier, Docker follows the naming convention to identify unofficial images. What we are creating is an unofficial image. Hence, it should follow that syntax. According to that naming convention, the unofficial image name should be named as follows: <username>/<image_name>:<tag_name>. In my case, I need to rename it as gauravvv/example_image:latest

$ docker tag example_image:latest gauravvv/example_image:latest
  1. Publish the image

     1$ docker push gauravvv/example_image:latest
    
  2. Upload your tagged image to the repository using the docker push command. Once complete, you can see the image there on Docker Hub. That's it; you have successfully published your Docker image. If you want to test out your image, use the below command and launch a container from it:

1$ docker pull gauravvv/example_image:latest
2$ docker run -it gauravvv/example_image:latest

Bonus: Pushing to a Non-Docker Hub Registry

So far, we have learned how to create a new Docker image, tag it, and push it to the Docker Hub. While using Kubernetes or OpenShift, we would use the internally hosted Docker registry; some organizations also prefer to host their own internal private registry. Therefore, you should know how to publish your Docker image to an internally hosted Docker registry. Luckily, Docker made it more accessible, just like publishing to Docker Hub but a little bit different.

Step 1: Log in to a Non-Docker Hub Registry

Use the docker login command to log in to the specified registry and to tell Docker which registry we want to sign in to.

$ docker login registry.example.com

Step 2: Tag the Image

As you may remember, for publishing our image to Docker Hub, we have tagged the image to include the username at first. Now, to publish to the internal registry, continuing on that pattern, we will also add the registry name at first, followed by a username, then followed by image name. Therefore, the final syntax will look like this:<registry>/<username>/<image_name>:<tag>. Let's take a look at an example:

1$ docker tag gauravvv/example_image registry.example.com/gauravvv/example_image

Step 3: Push the Image

The docker push command takes the name of the image. It will know where to push our Docker image by looking at the image name because the name contains the registry location. By looking at our image name, which is registry.example.com/gauravvv/example_image , it will determine that we are pushing an image name, example_image, to a user's repository, gauravvv, at the registry registry.example.com.

$ docker push registry.example.com/gauravvv/example_image