What is Dockerfile
Dockerfile is a script consisting of a series of commands and parameters that apply to the underlying mirror and eventually create a new mirror. They simplify the process from beginning to end and greatly simplify the deployment effort. The dockerfile begins with the from command, followed by the various methods, commands, and parameters of the follower. The output is a new mirror that can be used to create the container.
When you build a mirror using Docker, each command will form a new layer based on the previous command. These base mirrors can be used to create a new container. This article will take you step-by-step through the process of building a container from the Dockerfile, step-by-step, from the base image.
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 Execution Step Analysis
In the Dockerfile example above, each instruction creates a new mirror layer and commits the mirror. Docker execute dockerfile approximate process:
- Docker runs a container from the underlying mirror;
- Execute an instruction to make modifications to the container;
- Perform a similar Docker commit operation to submit a new mirror layer;
- Docker then runs a new container based on the newly submitted mirror;
- Execute the next instruction in the dockerfile until all instructions have been executed;
Sample resolution
- From: The first command for each dockerfile is that the from.from instruction specifies an existing mirror, and the instructions representing the from follow are based on the Mirror (ubuntu14.04).
- Maintainer: This instruction tells Docker, author and email address
- Run: In layman's parlance, the run instruction is executed using the command wrapper/bin/sh-c in the shell. If you are running on a platform that does not support the shell, you can use the Exec format Run command run ["Apt-get", "Install", "Y", " Nginx "]
- Expose: Exposing ports outward
Summary and analysis of dockerfile instruction
Maintainer
I suggest that this command be placed at the beginning of the dockerfile, although theoretically it can be placed anywhere in the dockerfile. This command is used to declare the author and should be placed behind from.
# maintainer [name] [email]
Maintainer Authors_name "hongtu1993@sina.cn"
From
The from command may be the most important dockerfile command. The change command defines which underlying image is used to start the build process. The underlying mirror can be any mirror. If the underlying mirror is not found, Docker will attempt to find the mirror from Docker image index. The from command must be the first command of Dockerfile.
# from [Image name]
From Ubuntu
ADD
The add command has two parameters, source, and destination. Its basic role is to copy files from the file system on the source system to the target container's file system. If the source is a URL, the contents of the URL are 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 a central part of the Dockerfile execution command. It takes a command as an argument and is used to create a mirror. Unlike the cmd command, the Run command is used to create a mirror (a new layer is formed above the previous commit layer).
# RUN [command]
RUN apt-get Update
Cmd
Like the Run command, CMD can be used to execute a specific command. Unlike run, these commands are not executed while mirroring is being built, but are invoked after the container is built with mirrors.
# CMD Application "argument", "argument", ...
CMD "echo" "Hello mao!"
EntryPoint
EntryPoint helps you configure a container to be executable, and if you combine the cmd command with the entrypoint command, you can remove "application" from the cmd command and simply reserve the parameters, which are 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 overridden during *run*
CMD "Hello docker!"
EntryPoint Echo
Env
The env command is used to set environment variables. These variables exist in the form of "Key=value" and can be called by scripts or programs within the container. This mechanism has brought great convenience to the operation of the container.
# ENV Key value
ENV Server_works 4
USER
The user command is used to set the UID of the run container.
# USER [UID]
USER 751
VOLUME
The volume command allows your container to access the directory on the host.
# VOLUME ["/dir_1", "/dir_2" ...]
VOLUME ["/my_files"]
Workdir
The Workdir command is used to set the running directory for the command specified by CMD.
# Workdir/path
Workdir ~/
Expose
The expose instruction is used to tell Docker which ports the container listens to at runtime, and Docker use this information when connecting to different containers (using –link parameters);
The core concept of two Docker is repeatable and portable. The mirror should be able to run on any host and run 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 be able to run only one instance of the container application. (Translator Note: Running multiple ports is not a conflict)
# expose [Port]
# Private and public mapping
Expose 80:8,080
# Private only
Expose 80
Finally, let's take a simple example.
Using Dockerfile to automatically build nginx containers
Because we command Docker to replace the default profile with the Nginx profile of the current directory, we want to make sure that this new profile exists. In the directory where Dockerfile exists, create the nginx.conf:
Then replace the original content with the following:
Worker_processes 1;
Events {worker_connections 1024;}
HTTP {
sendfile on;
server {
listen;
Location/{
Proxy_pass http://httpstat.us/;
Proxy_set_header x-real-ip $remote _addr
}}
}
Let's save nginx.conf. We can then use Dockerfile and configuration files to build mirrors.