This article introduces the knowledge of Dockerfile.
An overview,
Dockerfile is the file that builds a Docker image. This file describes how to build a Docker image. It has its own syntax and usage. Comments use “#”. In practice, it can be created based on official or other images. Dockerfile is divided into four parts: basic image information, maintainer information, image operation instructions and container startup execution instructions. Such as:
Basic image information is shown here #...... FROM Ubuntu # here describes the maintainer information #...... MAINTAINER DOCKER_USER # MAINTAINER DOCKER_USER #....... RUN apt-get update # CMD /bin/bashCopy the code
Most dockerfiles consist of these parts. Dockerfile is not easy to learn, but it is also not difficult. After understanding the basic concepts, the best way is to read the official Dockerfile file. To name a few:
Busybox: https://github.com/docker-library/busybox/blob/4b11c30f87d3033b2abd446c9bc1ef12f96abaa0/uclibc/Dockerfile (only three lines, You can practice from here. https://github.com/docker-library/redis/blob/ede954da6de61f1d6a8572db4cd7bdf1278a2815/5.0/Dockerfile mysql:https://github.com/docker-library/mysql/blob/ed0e47e48b8ca3dbc4d68d68f56384bdd1fb5cdb/5.6/DockerfileCopy the code
The author also accumulated some, interested can refer to: github.com/latelee/doc… .
Instructions
The general format of the instruction is < instruction > < parameter >, including FROM, MAITNTAINER, RUN, etc., as follows:
RUN RUN command CMD Specifies the default command used when the container is started. LABEL Specifies the metadata LABEL information for generating the image. EXPOSE Specifies the port monitored by the service in the image ENTRYPOINT Specifies the default entry for an image. VOLUME Creates the mount point of a data VOLUME. USER Specifies the USER name or UID used to run the container Specify the parameters (such as version number information) used in the image. ONBUILD When configuring the currently created image as the base image of other images, Creation operation executed STOPSIGNAL container exit signal value HEALTHCHECK how to perform HEALTHCHECK SHELL specifies the default SHELL type when using SHELLCopy the code
The following are introduced respectively.
FROM
Function to specify the base image and must be the first instruction. If the local image does not exist or the image repository does not exist, go back to DockerHub to download the specified image. If you create multiple images in the same Dockerfile, you can use multiple FROM directives (one for each image). Grammar:
FROM <image>
FROM <image>:<tag>
FROM <image>:<digest>
Copy the code
Three options, where and is optional. If not, the default is latest.
RUN
Function to run the specified command. Grammar:
RUN <command>
RUN ["executable", "param1", "param2"]
Copy the code
The first type is directly followed by the shell command. On Linux, the default is /bin/sh -c. The second is something like a function call. Executable can be understood as an executable, followed by two parameters. Example:
RUN /bin/bash -c 'source $HOME/.bashrc; echo $HOME
RUN ["/bin/bash", "-c", "echo hello"]
Copy the code
Note: Multi-line commands do not write multiple runs, because each instruction in a Dockerfile creates a layer. The number of runs builds as many layers of images as possible, resulting in bloated, multi-layered images that not only increase the deployment time of components, but also make them prone to errors.
If the RUN command is long, the newline character \ can be used. Example:
RUN apt-get update \
&& apt-get install vim
Copy the code
CMD
Function is the command to run when the container is started. Grammar:
CMD ["executable","param1","param2"]
CMD ["param1","param2"]
CMD command param1 param2
Copy the code
There can only be one CMD command per Dockerfile. If multiple commands are specified, only the last command will be executed. If the user manually specifies the command to run when starting the container, the commands specified by CMD are overwritten. Example:
CMD [ "sh", "-c", "echo $HOME"
CMD [ "echo", "$HOME" ]
Copy the code
Additional details: always use double quotation marks (“, not single quotation marks) for arguments. Never use single quotation marks.
LABEL
The metadata label information for the image is specified. Grammar:
LABEL <key>=<value> <key>=<value> <key>=<value> ...
Copy the code
Example:
LABEL "com.example.vendor"="ACME Incorporated"
LABEL com.example.label-with-value="foo"
LABEL version="1.0"
LABEL description="This text illustrates \
that label-values can span multiple lines."
Copy the code
Note: The LABEL inherits the LABEL of the base mirror. If the key is the same, the value is overwritten.
MAINTAINER
Function to specify author syntax:
MAINTAINER <name>
Copy the code
Example:
MAINTAINER "[email protected]"
Copy the code
This information is written to the Author attribute field of the generated image.
EXPOSE
Function for leak container running listening port to the outside. Grammar:
EXPOSE < PORT > / < PORT >...Copy the code
Example:
EXPOSE 22 80 443
Copy the code
Note that this directive is declarative only and does not automatically complete port mapping. You need to add -p when starting the container.
ENV
Syntax for setting environment variables:
ENV <key> <value>
ENV <key>=<value> ...
Copy the code
Example:
ENV PATH /usr/local/bin:$PATH
Copy the code
ADD
Function to copy files to an image. If you think of the virtual machine and container as two Linux servers, this command is similar to SCP, except that SCP requires user name and password authentication, while ADD does not. Grammar:
ADD <src>... <dest>
ADD ["<src>",... "<dest>"]
Copy the code
The
path can be an absolute path within the container or a path relative to the working directory. < SRC > can be a local file or a local compressed file, or it can be a URL. If < SRC > is written as a URL, ADD is similar to the wget command
Example:
ADD test relativeDir/
ADD test /relativeDir
ADD http://example.com/foobar /
Copy the code
Try not to write < SCR > as a folder. If < SRC > is a folder, copy the contents of the entire directory, including file system metadata
COPY
Function to copy files to an image. Grammar:
COPY <src>... <dest>
COPY ["<src>",... "<dest>"]
Copy the code
Unlike ADD, COPY < SRC > can only be a local file
ENTRYPOINT
Function is the default entry command at startup. The entry command is executed as the root command when the container is started, with all the values passed as arguments to the command. Grammar:
ENTRYPOINT ["executable", "param1", "param2"]
ENTRYPOINT command param1 param2
Copy the code
You can only write one, and if you write more than one, then only the last one is valid.
VOLUME
Function to hang folders or folders in other containers into this container. Grammar:
VOLUME ["/data"]
Copy the code
Note: [“/data”] can be a JsonArray or multiple values. Such as:
VOLUME ["/var/log/"]
VOLUME /var/log
VOLUME /var/log /var/db
Copy the code
This command is used when data needs to be persisted.
USER
Function sets the user to start the container, which can be a user name or UID. Grammar:
USER daemo
USER UID
Copy the code
Note: If you set up a container to RUN as a daemon user, then RUN, CMD and ENTRYPOINT will all be RUN as that user
WORKDIR
Function Configures working directories for subsequent RUN, CMD, and ENTRYPOINT commands. Grammar:
WORKDIR /path/to/workdir
Copy the code
If it does not exist, it will be created, or it can be set multiple times. Example:
WORKDIR /a
WORKDIR b
WORKDIR c
RUN pwd
Copy the code
The result of PWD execution is /a/b/ C
WORKDIR can also parse environment variables. Example:
ENV DIRPATH /path
WORKDIR $DIRPATH/$DIRNAME
RUN pwd
Copy the code
The result of PWD is /path/$DIRNAME
ARG
Function specifies the parameters (version number information, etc.) to be used in the image. Grammar:
ARG <name>[=<default value>]
Copy the code
–build-arg
=
specifies a variable when docker build creates the image. Example:
FROM busybox
ARG user1
ARG buildno=1
Copy the code
3. Create an image
After writing the Dockerfile, you can use the Docker build command to create the image. Grammar:
docker build [OPTIONS] PATH | URL | -
Copy the code
Example:
#docker build –t ubuntu:v1 .
Copy the code
Note: The dot at the end of the command indicates the current path. The default file name is Dockerfile. Otherwise, you need to specify this name manually, but the default is generally used.