What is a docker image?

An image in docker is a single file that contains everything required to run an application such as
1. An Operating system(OS).
2. Application code or files.
3. Environment variables.
4. Supporting applications. Example, JRE for running java applications,
node for running javascript etc.
5. Other required files such as a batch file or shell script etc.

In this article, we will take a deep dive into docker images and below topics

Base Image

Every docker image contains an Operating System. This OS is not a full fledged one but a cut-down OS that is supposed to run an application.
There are many pre-built images that contain such Operating Systems and these images are called base images.
So, a docker image must be built on top of a base image.
A base image must contain an OS, but it may also contain a runtime environment such as an image that contains Linux OS and a .NET environment for running C# applications, or a Linux OS and a JRE for running java applications.

What is docker file?

A docker file is a simple file which contains instructions for creating or building a docker image.
A docker file is generally named as Dockerfile, which is D in upper case and rest all in lower case, without any extension.

Commonly used instructions in docker file are

1. FROM
Used to specify base image on which this image will be built.
2. WORKDIR
Used to set the working directory. Once this is used, all subsequent commands are assumed to be executed from this directory.
3. COPY
For copying files from local system to docker image.
4. ADD
Similar to add but supports copying from a URL as well.
5. RUN
For running operating system commands inside a docker container.
6. CMD
Command executed at container startup or during execution.
7. ENV
For specifying environment variables.

We will take a deeper look at docker file in later sections. For this article, it is sufficient to know its use and these commands.

Docker image creation

Now, let’s create an image or dockerize an application.
Suppose, we have a simple javascript file, app.js having a single statement as below

console.log("Hello World!");

When this file is executed, it simply prints a message.
For running this file, we need node.js. So, when we run the command node app.js, this message will be printed.

As a first step to create a docker image, place a file named Dockerfile, in the same directory as app.js.
This docker file should contain below instructions.

FROM node:14.19.3-alpine3.16
COPY . /home
WORKDIR /home
CMD node app.js

Details of these steps are explained below step by step.

1. FROM specifies the base image.
Since we need to have node runtime and linux OS, we use the base image tag as node:14.19.3-alpine3.16.
How do we know this tag. Head over to Dockerhub and search for node.

In the filter search box, type 14 as shown below. This is because, we are trying to build our image on node v14.
You can use any version you like.
searching for base image

Secondly, we chose alpine image since these are smaller in size.
Note that alpine is a variant of Linux.
Tag that we need to use, is highlighted.
2. COPY command copies the contents from current folder into the specified location in the image.
Current directory contains app.js file, which is copied into /home director inside the image.
Note that /home will be created inside the image.
3. WORKDIR is used to set working directory to /home.
This step is required since we need access to app.js file, which is placed in /home.

Alternatively, we can omit this step and execute the next step by providing complete path to app.js as CMD /home/app.js.

4. CMD is for executing our app.js file using node.

Building docker image

A docker image is built using below command

docker build -t <tag name> <docker file> <path>

Here,
tag is a name that is used to identify the docker image.
docker file is the name of docker file. If omitted, it assumes the name as Dockerfile.
path is the path of docker file.

The command for building a docker image in our example would be

docker build -t hello-world .

We have omitted docker file argument, since the name of this file is Dockerfile(default).
Notice the .(dot) at the end, which signifies that the docker file is located in the current directory.

Open command prompt on windows and terminal on linux/Mac and navigate to the folder where app.js and Dockerfile are located and run this command.

You will see below output
building docker image

We have successfully created a docker image containing
1. an OS.
2. node
3. Javascript file app.js.

Building image with docker image

There is a second way to build a docker image with docker image command.This command is similar to docker build with all options except that it contains image between docker and build as shown below.

docker image build -t <tag name> <docker file> <path>

So, the command to create a docker image this way using our example would be

docker image build -t hello-world .

Running docker image

Once we create an image, it contains all the requirements that an application needs to execute including the application code.
A docker image can be executed on any system that is running docker irrespective of its own OS or runtime environments.
To run a docker image below command is used
docker run <image name>
Here, image name can be the name of the image specified while building it or it can be the id of image. Example,

docker run hello-world

This will produce below output

D:\work\docker>docker run hello-world
Hello World!

To get the id of an image list all the images as explained in the next section and check the id against the name of your image.

Listing images

After creating an image, you might want to see its details or details of other images.
For this, docker provides a command given below

docker image ls

This will list all the images created on the local system. Note that you do not need to be in any specific directory.
This command will work across the local machine.

Output is be as below
docker list images

Removing images

Often there are many images that are created but never used.
To remove all unused images, following docker command is used

docker image prune

Example,
removing all docker images

This command will ask for your confirmation to delete all dangling images.
Dangling images are those which do not have any reference, they are not related to any containers.

To delete a specific image use below command

docker image rm <image name>

where image name can be either the name of image or its id.

Hope this article on docker images was useful.