Address: http://www.maoyupeng.com/dockerfile-command-introduction.html
What is dockerfile?
Dockerfile is a script composed of a series of commands and parameters. These commands are applied to the basic image and finally create a new image. They simplify the process from start to end and greatly simplify the deployment. Dockerfile starts from the from command, followed by various methods, commands, and parameters of the followers. The output is a new image that can be used to create a container.
When you use docker to build an image, each command forms a new layer based on the previous command. These basic images can be used to create new containers. This article will teach you how to build a container from dockerfile step by step from the basic image.
Dockerfile example
# Version 1.0FORM ubuntu:14.04MAINTAINER Mao "[email protected]"RUN apt-get update && apt-get install -y nginxRUN echo ‘Hello,I am work‘ > /usr/share/nginx/html/index.htmlEXPOSE 80 80
Docker execution step analysis
AboveDockerfile
In this example, each command creates a new image layer and submits the image. docker executes the general process of dockerfile:
Docker runs a container from the basic image;
Execute a command to modify the container;
Execute similardocker commit
To submit a new image layer;
Docker then runs a new container Based on the submitted image;
RunDockerfile
Until all commands are executed;
Example
From: The first command of each dockerfile is the from. From command to specify an existing image, which means that subsequent commands of from are based on this image (ubuntu14.04.
Maintainer: This Command tells docker the author and email address.
Run: In layman's terms,RUN
Command will use the command package in Shell/bin/sh -c
Run the Run Command in Exec format if it is run on a platform that does not support shell.RUN ["apt-get","install","-y","nginx"]
Expose: Public ports
Dockerfile command summary and parsing maintainer
I suggest placing this command in the starting part of dockerfile, although theoretically it can be placed anywhere in dockerfile. This command is used to declare the author and should be placed behind the from.
# MAINTAINER [name] [email]MAINTAINER authors_name "[email protected]"
From
The from command may be the most important dockerfile command. The command defines which basic image is used to start the building process. The basic image can be any image. If the basic image is not found, docker will try to find the image from the docker image index. The from command must be the first command of dockerfile.
# FROM [image name]FROM ubuntu
Add
ADD
The command has two parameters: source and target. Its basic function is to copy files from the source system file system to the target container file system. If the source is a URL, the URL content will be downloaded and copied to the container.
# ADD [source directory or URL] [destination directory]ADD /my_app_folder /my_app_folder
Run
RUN
Command isDockerfile
The core part of the command execution. It accepts commands as parameters and is used to create images. UnlikeCMD
Command,RUN
Command is used to create an image (a new layer is formed above the previous commit Layer ).
# RUN [command]RUN apt-get update
CMD
AndRUN
Similar commands,CMD
It can be used to execute specific commands. AndRUN
The difference is that these commands are not executed during the image building process, but are called after the container is built using the image.
# CMD application "argument", "argument", ..CMD "echo" "Hello Mao!"
Entrypoint
ENTRYPOINT
It helps you configure a container to make it executable.CMD
Commands andENTRYPOINT
Command, you canCMD
Command to remove the "application" and only retain the parameter. The parameter will be passedENTRYPOINT
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
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 in the container. This mechanism brings great convenience to running applications in containers.
# ENV key valueENV SERVER_WORKS 4
User
The USER command is used to set the UID of the running container.
# USER [UID]USER 751
Volume
The volume command is used to allow 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 of the command specified by CMD.
# WORKDIR /pathWORKDIR ~/
Expose
EXPOSE
Command is used to tell docker which ports the container will listen for at runtime. docker uses this information when connecting to different containers (using the-Link parameter;
The two core concepts of docker are repeatable and portable. The image 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 never need to map public ports through dockerfile. By ing public ports to the host, you can run only one containerized application instance. (Note: Running multiple ports does not conflict)
# EXPOSE [port]# private and public mappingEXPOSE 80:8080# private onlyEXPOSE 80
Dockerfile command summary and parsing