Dockerfile instruction details

  • 2020-05-17 06:56:40
  • OfStack

What is a Dockerfile

Dockerfile is a script composed of 1 series of commands and parameters that apply to the base image and eventually create a new image. They simplify the process from start to finish and greatly simplify deployment. Dockerfile starts with the FROM command and follows various methods, commands, and parameters. The output is 1 new image that can be used to create the container.

When you build an image using Docker, each command creates a new layer on top of the previous one. These base images can be used to create new containers. This article will walk you through the process of building a container from Dockerfile, step by step, layer by layer, from base mirroring.

Dockerfile sample

# Version 1.0
FORM ubuntu:14.04
MAINTAINER Mao "hongtu1993@sina.cn"
RUN apt-get update && apt-get install -y nginx
RUN echo 'Hello,I am work' > /usr/share/nginx/html/index.html
EXPOSE 80 80

Docker performs step analysis

In the Dockerfile example above, each instruction creates a new mirror layer and commits the image.

Docker runs 1 container from the base image; Execute 1 instruction to modify the container; Perform an operation similar to docker commit and submit a new mirror layer; Docker then runs a new container based on the image just submitted. Execute the next instruction in Dockerfile until all instructions have been executed;

The sample analysis

FROM: the first command of each dockerfile is the FROM.FROM directive specifying an existing image, which means that subsequent instructions for FROM are based on that image (ubuntu14.04). MAINTAINER: this directive tells Docker, author, and email address RUN: in layman's terms, the RUN directive will be executed in shell using the command wrapper /bin/ sh-c. If running on a platform that does not support shell, the RUN directive RUN [" apt-get ","install"," -y ","nginx"] EXPOSE: expose the port outward

Summary and parsing of Dockerfile instructions

MAINTAINER
I recommend that this command be placed at the beginning of Dockerfile, although it could theoretically be placed anywhere on Dockerfile. This command is used to declare the author and should be placed after FROM.

# MAINTAINER [name] [email]
MAINTAINER authors_name "hongtu1993@sina.cn"

FROM

The FROM command is probably the most important Dockerfile command. The change command defines which base image to use to start the build process. The base image can be any mirror image. If the base image is not found, Docker will try to find the image from Docker image index. The FROM command must be the first command of Dockerfile.

# FROM [image name]

FROM ubuntu

ADD

The ADD command takes two arguments, source and target. Its basic function is to copy files from the file system of the source system to the file system of the target container. If the source is 1 URL, the contents of that URL will be downloaded and copied to the container.

# ADD [source directory or URL] [destination directory]

ADD /my_app_folder /my_app_folder

RUN

The RUN command is the core part of the Dockerfile execution command. It takes commands as arguments and is used to create the image. Unlike the CMD command, the RUN command is used to create an image (creating a new layer on top of the previous commit layer).

# RUN [command]

RUN apt-get update

CMD

Similar to the RUN command, CMD can be used to execute specific commands. Unlike RUN, these commands are not executed during the mirror build process, but are called after the container is built with the mirror.

# CMD application "argument", "argument", ..

CMD "echo" "Hello Mao!"

ENTRYPOINT

ENTRYPOINT helps you configure a container to make it executable. If you combine the CMD command with the ENTRYPOINT command, you can remove "application" from the CMD command and only keep the parameters, which will be passed to the ENTRYPOINT command.

# Usage: ENTRYPOINT application "argument", "argument", ..
# Remember: arguments are optional. They can be provided by CMD
# or during the creation of a container.
ENTRYPOINT echo

# Usage example with CMD:
# Arguments set with CMD can be overridden during *run*
CMD "Hello docker!"
ENTRYPOINT echo

ENV

The ENV command is used to set environment variables. These variables exist as "key=value" and can be called by scripts or programs in the container. This mechanism provides great convenience for running applications in containers.

# ENV key value

ENV SERVER_WORKS 4

USER

The USER command is used to set up UID for the run container.

# USER [UID]

USER 751

VOLUME

The VOLUME command is used to give your container access to directories on the host machine.

# VOLUME ["/dir_1", "/dir_2" ..]

VOLUME ["/my_files"]

WORKDIR

The WORKDIR command sets the running directory of the command specified by CMD.

# WORKDIR /path

WORKDIR ~/

EXPOSE

The EXPOSE directive is used to tell Docker which ports the container will listen to at run time, and Docker USES this information when connecting different containers (using the wok and link parameters).
The two core Docker concepts are repeatable and portable. The image should run on any host and as many times as possible. In Dockerfile you have the ability to map private and public ports, but you should never map public ports through Dockerfile. By mapping the public port to the host, you will only be able to run one instance of a containerized application. (running multiple ports is a conflict)

# EXPOSE [port]

# private and public mapping
EXPOSE 80:8080

# private only
EXPOSE 80

Let's finish with a simple example

Build the Nginx container automatically using Dockerfile

Since we command Docker to replace the default configuration file with Nginx's configuration file in the current directory, we want to make sure that this new configuration file exists. In the directory where Dockerfile exists, create nginx.conf:


sudo nano nginx.conf

Then replace the content with the following:


worker_processes 1;
events { worker_connections 1024; }
http {
   sendfile on;
   server {
     listen 80;
     location / {
       proxy_pass http://httpstat.us/;
       proxy_set_header X-Real-IP $remote_addr;
     }
   }
}

Let's save nginx.conf. Then we can use Dockerfile and the configuration file to build the image.


Related articles: