Docker Images

Docker Images

In this tutorial we will discuss about what is docker images how to create your own docker images.

In Docker, everything is based on Images. A Docker image is an immutable (unchangeable) file that contains the source code, libraries, dependencies, tools, and other files needed for an application to run.

When the Docker user runs an image, it can become one or multiple instances of that container.

Now before creating your own docker image, why do we need to create your own image? It could either be because you cannot find a component or a service that you want to use as part of your application on docker hub already.

How to create my own image?
Create docker images

First we need to understand what we are containerized or what application we are creating an image for and how the application is built.

So start by thinking what you might do if you want to deploy the application manually. We write down the steps required in the right order and creating an image for a simple web application.

If I were to set it up manually, I would start with an operating system like Ubuntu then update the source repositories using the apt command. Then install dependencies using the apt command then install python dependencies using pip command then copy over the source code of your application to a location like /opt and then finally run the web server using the flask command.

Now that you have the instructions to create a Docker file using these. Here is the quick overview of the process of creating your own image.

First create a docker file named Dockerfile and write down the instructions for setting up your application in it. Such as installing dependencies, where to copy the source code from and to and what the entry point of the application etc.

FROM Ubuntu:18.04

RUN apt-get update
RUN apt-get install python

RUN pip install flask
RUN pip install flask-mysql

COPY . /opt/myapp/src/

ENTRYPOINT FLASK_APP=/opt/myapp/src/app.py flask run

Once done build your image using docker build command and specify the docker file as input as well as a tag name for the image.

This will create an image locally on your system. To make it available on the public docker hub registry then run the docker push command and specify the name of the image you just created.

Understanding about Dockerfile

Now let’s take a closer look at that docker file. Dockerfile is a text file written in a specific format that docker can understand. It is an instruction and arguments format.

For example in the above example everything on the left side in CAPS is an instruction. In this case FROM, RUN, COPY, ENTRYPOINT are all instructions.

Each of these instruct Docker to perform a specific action while creating the image. Everything on the right side is an argument to those instructions.

The first line FROM Ubuntu defines what is the base Operating System should be focused container. Every docker image must be based off of another image. It is either an OS or another image that was created before based on an OS.

You can find official releases of all Operating systems on docker hub. It’s important to note that all docker files must start with a from instruction.

The RUN instruction instructs docker to run a particular command on those base images and then the COPY instruction copies files from local system onto the docker image.

Finally ENTRYPOINT allows us to specify a command that will be run when the image is run as a container.

Layered Architecture

When docker builds the images it builds these in a layered architecture. Each line of instruction creates a new layer in the docker image with just the changes from the previous layer.

For example the first layer is a base Ubuntu OS followed by the second instruction that creates the second layer which installs all the apt packages and then the third instruction creates a third layer with the python packages followed by the fourth layer that copies the source code over and the final layer that updates the entry point of the image.

Docker image Layered Architecture

Since each layer only stores the changes from the previous layer, it is reflected in the size as well.

If you look at the base Ubuntu image it is around 120 MB in size. The apt packages that you install is around 300 MB and the remaining layers are small.

You could see this information if you run the docker history command followed by the name of the image name.

When you run the docker build command, you could see the various steps involved and the result of each task.

All the layers build are cached. So the layered architecture helps you restart docker build from that particular step. In case it fails or if you were to add new steps in the build process you wouldn’t have to start all over again.

Docker Images


Scroll to top