Step by Step Guide: How to Build Your Docker Image?

Step by Step Guide: How to Build Your Docker Image?

February 25, 2019 / Nirav Shah

In this blog, we will learn some basics about Docker Images. We will talk about, how can we build a Docker image of our desired application.

Docker Image & Containers An instance of an image is called a container. You have an image, which is a set of layers as you describe. If you start this image, you have a running container of this image. You can have many running containers of the same image.

For the first step to learn about building the docker image, We can pull the base OS image and In the interactive shell of the container image, We can add our required software on the top of this image.
Next, we shall take a look at how to build our own Docker images via a Dockerfile in another blog. writing a Dockerfile is a more consistent and repeatable way to build your own images.

Let us begin with starting boot2docker utility and then working with the base Ubuntu image. First, make sure that you do have ubuntu latest image to get going here.

Fire up the following command:

This should give you a list of Docker images that you have. Now pull the ubuntu latest version image:Docker Image & Containers

Let us launch a container from the ubuntu:latest image by giving the following command:

This should lead you to a prompt. For e.g. on my machine, I have the following prompt:root@ea503e60bae3:/#

Now, let’s install the popular Git software on this container instance by running the following commands:

This should output a stream of messages. Let the process continue its work till you see a message as given below:
Reading package lists… Done

You will be back at the prompt. Now, let us install Git via the command given below:

It will prompt you with a message:

Do you want to continue? [Y/n]
Please go ahead with a Y.
It will take a few seconds to download the Git software and install it. You should finally be at the prompt once everything is done.
To verify that Git is installed, simply type git at the prompt as shown below:

This should print out the Git version at the console as shown below:

Now, let us exit the container by typing exit. Keep in mind that we had provided the — rm flag while starting the container, which means that the container is removed on termination.

Now, let us launch another instance of the same ubuntu container as shown below.

Type git at the prompt. It displays the message that git is not found:

What happened? Didn’t we just install Git on ubuntu and expecting it to be present. The point is that each Container instance launched this way is independent and is depending on the master image i.e. ubuntu:latest, which does not have Git installed.

Committing your Image

To ensure that next time we have a version of Ubuntu with Git installed, we need to commit our image. What this means is that we started with a container based on the Ubuntu image. Then we added some software to it like git.

This means that we modified the state of the container. Hence we need to save that state of the container as an image, so that can relaunch additional new containers from that image. This way they will have the git software installed too.

Let us exit from the container in the previous section and following these steps:

Launch the container based on Ubuntu, this time without the — rm flag

Update the OS and install Git via the commands given below:

Verify that Git is installed via the command given below:

Exit the container by typing exit.

Run the docker ps -all command to see the mycontainer1 that we launched:

This should show you the mycontainer1 that we had launched. It is currently in the exited state.
Commit the container image via the docker commit command as shown below:

docker commit [ContainerID] [Repository[:Tag]

In our case, we use the mycontainer1 as the container since we have given it a name. If you had not started that container with a name, you could have used the Container Id that is visible in the docker ps -all command.

The Repository name is important. Eventually (and which is what we will do) we will want to push this to the Docker Hub. So the format of the Repository name that is recommended is the following:

In our case, the repository-name can be something like ubuntu-git and you will need to substitute your tag above with your Docker Hub user name.

If you do not give the tag, it will be marked as ‘latest’, which is fine for us. For e.g. my Docker Hub user name is Eternal and hence I will commit it in the following manner:

This will give you back the image ID.

You should see at the top of the list an image that is like the following entry that I have:

REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
Eternal/ubuntu-git latest 23cf273fafed About a minute ago 247.1 MB

This shows that our Repository Eternal/ubuntu-git has got created.

Pushing the Image to the Docker Hub

Push the image to the Docker Hub via the Following Steps —

  1. Get an account at Docker Hub, through singing up section at hub.docker.com
  2. Now at the docker engine host, execute the command docker login and follow the instructions for your username and password.
  3. On successful login, you should get a Login Succeeded message.
  4. To a docker push as shown below:

This will take a while to upload the details. Note that Docker is smart in the sense that it already will detect if the base image i.e. ubuntu already exist and hence it will cleverly choose only those layers that need to be uploaded

Just try to understand the whole committing process by following block diagram. It will help you more for a better understanding of the docker images building and hosting process.

Talk to AWS Certified Consultant

    Spread Love By Sharing:

    Let’s Talk About Your Needed AWS Infrastructure Management Services

    Have queries about your project idea or concept? Please drop in your project details to discuss with our AWS Global Cloud Infrastructure service specialists and consultants.

    • Swift Hiring and Onboarding
    • Experienced and Trained AWS Team
    • Quality Consulting and Programming
    Let’s Connect and Discuss Your Project