Build a basic Docker image using Dockerfile

In the previous article, we introduced the basic use of Docker. Today let me show you how to write our own docker image with Dockerfile.

A Docker image is a blueprint for a Docker container, which contains the application and everything needed to run the software. The container is the runtime instance of the image. In this article, I will explain what a Dockerfile is, how to create a Dockerfile and how to use Dockerfile to build a Docker image.

What is dockerfile?

Dockerfile is essentially just a text file (we can modify it with any text editor), which contains all the instructions that users can run on the command line to create an image. It contains all the instructions Docker needs to build the image.

A Docker image is composed of a series of file system layers, which represent the instructions in the Dockerfile of the image and these instructions constitute an executable software application as we want.

The instructions are like the followings:

# Comment

The syntax of Dockerfile is very simple, the commonly used instructions are listed in the following table:

InstructionsDescription of instructions
FROMSpecify the base image used to build our new image. This instruction is generally the first non-comment instruction in the Dockerfile.
MAINTAINERCreator of the new image
ENVSet environment variables
RUNThe command specified in this instruction will be executed during execution. Each RUN instruction creates a new layer on top of the current image
ADDUsed to copy files or directories from the specified source to the container. The source can be a local file or directory, or even a URL
COPYSimilar to ADD instruction, but the source can only be a local file or directory
WORKDIRSpecify working directory. Similar to cd command in bash
EXPOSE Expose the a port and allow external connections to this port
CMDUsed to run a command. Unlike the RUN command executed during the construction phase of the image file, the execution result will be packaged into the image file; the CMD command is executed after the container is started. In addition, a Dockerfile can contain multiple RUN commands, but only one CMD command
VOLUMEMount the host directory to the container

To exclude files or directories from the image, we can create a file named .dockerignore in the root directory. The syntax of .dockerignore is similar to git’s .gitignore file. For a complete reference and detailed description of Dockerfile instructions, please see the official Dockerfile reference page.

Create a dockerfile

Now let me show you hot to create to build Redis image with dockerfile.

Step 1: Preparation.

Create a clean directory and download Redis source code.

$ cd ~
$ mkdir docker-redis
$ cd docker-redis
$ wget
Step 2: Write Dockerfile
# base image: centos7
FROM centos:centos7

# install dependencies to compile redis source code
RUN ["yum" , "install" , "-y" ,"gcc","gcc-c++","net-tools","make"]

# set workdir to /usr/local
WORKDIR /usr/local

# copy a file to workdir
ADD redis-5.0.10.tar.gz .

# reset workdir to compile redis
WORKDIR /usr/local/redis-5.0.10/src

# install redis
RUN make && make install

# reset dir where redis is installed
WORKDIR /usr/local/redis-5.0.10

# expose a port

# run redis after container starts
CMD ["redis-server"]
Step 3: Build image.

Now we can build image using below command.

docker image build -t redis_demo .

In this command above, the -t parameter is used to specify the name of the image file, and a colon can also be used to specify the label. If not specified, the default label is latest. The last dot represents the path where the Dockerfile file is located. The above example is the current path.

Now run docker image ls to check result.

$ docker image ls
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
redis_demo            latest              f9394bc3aa12        10 minutes ago      516MB

Note: Now, we have built a Redis image of our own. There is no need to do this in production environment. The Redis official has already provided relevant images. This article is only a comprehensive demo of the previous Dockerfile commands and docker commands.

In the next article, I will introduce some best practice of Dockerfile.