Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Create Docker Images for Docker Hub

Let's look at what Docker images and containers are, how to create a docker image, and how to push our image to Docker Hub.

Jul 25, 2019 • 13 Minute Read

Create Docker Images for Docker Hub

Introduction

The story of the development and deployment of any application in Docker begins with Docker images. But do you need to know how to create Docker images? Let's look at an example!

Suppose that you are a software developer and you want to develop an application that needs a Python runtime environment and some dependencies. But not just that, you need to run it on your cloud machine and have it work the same as it works on your local machine.

One way you might think of doing that is to download and install Python on your machine, configure the environmental variables, install all your application dependencies, and then run the application. Now, to run your application on the server, you need to do these steps all over again. To add a pinch of craziness to this situation, assume you want to scale that application and have to deploy that on hundred more servers. Even if you are a hard-working programmer, and you are ready to do it all, what if, while reviewing your application code, you discovered a bug or instead, you need to add a new feature? Bingo!!

Another, more efficient way, to deal with this type of problem is to create a Docker image of your application. For that, you need to grab a Python runtime as an image and then build your application alongside this Python runtime and all its dependencies. This is an easy and one-time process. But, once that image is ready, you can deploy your application on any Linux machine as many times you want. This application will get deployed in containers and is called containerization. Containers are flexible, lightweight, portable, and scalable.

I know you will now be excited to create your own first image but, before that, let us look at what Docker images and containers are. According to the documentation on Docker,

An image is an executable package that includes everything needed to run an application--the code, a runtime, libraries, environment variables, and configuration files.

A container is a runtime instance of an image--what the image becomes in memory when executed (that is, an image with the state, or a user process).

In this guide, we will explore and learn how to create Docker images and push it to the Docker Hub.

Some Things to Know About Docker Hub

A Docker image can be compared to a git repository. Just like a git repository, it can be hosted on GitHub, Bitbucket, GitLab, or even a private git repo hosting service, but we could host our Docker image on Docker repository hosting service like Docker Hub.

Docker Hub is a service provided by Docker for hosting, finding, and sharing Docker Repositories. Just like git repo hosting services, a Docker repository can be public or private. A third-party repository hosting services also exists. The Docker Hub and other third party repository hosting services are called registries. For example, RedHat has their own registry to host their container images.

One important point to remember is that a registry has many repositories, while a repository has many different versions of the same image. These registries can be either private or public, depending on the needs of the organization. Docker Hub is one such example of a public registry.

Docker is configured to use Docker Hub as it's default registry. Use the $ docker info command to view the registry that Docker is currently using. By default, it points to https://index.docker.io/v1/ which is the registry location for Docker Hub.

      $ docker info
...
Registry: https://index.docker.io/v1/
...
    

Official and Unofficial Docker Images

On Docker Hub, there are two kinds of images - official and unofficial. Official images are trusted and optimized. They have clear documentation, promote best practices, and are designed for the most common use cases. On the other hand, an unofficial image is any image that is created by a user. Docker Hub follows some standards so that both can easily be identified. Official images contain only the <image_name> as its image name but unofficial images have the syntax as <username>/<image_name>. Also, the official image has official written in the listing as shown in the below screenshot.

Official Image of Ubuntu

Unofficial Image by User ubuntuz

Downloading Image from Docker Hub

Search the Docker Hub for Images

We could also look up and find images on Docker Hub by either using the search bar on the website or using the below command:

      $ docker search <image_name>
    

Let us search for an image with the name of busybox:

      $ docker search busybox
NAME                DESCRIPTION                           STARS     OFFICIAL   AUTOMATED
busybox             Busybox base image.                               316        [OK]
progrium/busybox                                           50                    [OK]
radial/busyboxplus  Full-chain, Internet enabled ...        8                    [OK]
odise/busybox-python                                        2                    [OK]
azukiapp/busybox    This image is meant to be used as ...   2                    [OK]
    

Pull an Image or a Repository from a Registry

To download a particular image, or set of images (i.e., a repository), use docker pull. If no tag is provided, Docker Engine uses the :latest tag as a default.

      $ docker pull <image_name>:<tag_name>
    

Let us pull the latest image of debian:

      $ docker pull debian
Using default tag: latest
latest: Pulling from library/debian
fdd5d7827f33: Pull complete
a3ed95caeb02: Pull complete
Digest: sha256:e7d38b3517548a1c71e41bffe9c8ae6d6d29546ce46bf62159837aad072c90aa
Status: Downloaded newer image for debian:latest
    

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:

      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:

      $ docker build -t example_image .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM busybox:latest
---> db8ee88ad75f
Step 2/2 : CMD ["date"]
---> Using cache
---> b89335fdacdf
Successfully built b89335fdacdf
Successfully 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:

      $ docker run -it --name example_app example_image
Sun 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:

      $ docker ps -a
CONTAINER ID     IMAGE      COMMAND    CREATED               STATUS              NAMES
c3df2dd33276  example-image "date"  5 seconds ago   Exited (0) 4 seconds ago  example_app

$ docker commit example_app example_image2:latest
sha256: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 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:

    $ docker login
    
    • 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
      
      • Publish the image

        $ docker push gauravvv/example_image:latest
        

        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:

        $ docker pull gauravvv/example_image:latest
        $ 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:

      $ 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
    

Wrap Up

Congratulations on finishing up this guide! I hope you have enjoyed learning how to create a Docker image and publish it to Docker Hub. Understanding the use cases of Docker is as vital as learning its concepts. Now, go ahead, Dockerize your applications and share them with the world. Happy Dockerizing!

Gaurav Singhal

Gaurav S.

Guarav is a Data Scientist with a strong background in computer science and mathematics. He has extensive research experience in data structures, statistical data analysis, and mathematical modeling. With a solid background in Web development he works with Python, JAVA, Django, HTML, Struts, Hibernate, Vaadin, Web Scrapping, Angular, and React. His data science skills include Python, Matplotlib, Tensorflows, Pandas, Numpy, Keras, CNN, ANN, NLP, Recommenders, Predictive analysis. He has built systems that have used both basic machine learning algorithms and complex deep neural network. He has worked in many data science projects, some of them are product recommendation, user sentiments, twitter bots, information retrieval, predictive analysis, data mining, image segmentation, SVMs, RandomForest etc.

More about this author