catalogue
FROM: Specifies the base image, which must be the first command
MAINTAINER: MAINTAINER information
RUN: command executed when building a mirror
CMD: call after building the container, that is, when the container starts.
Difference between CMD and RUN
ADD: adds a local file to the container
ARG: used to specify the variables passed to the build runtime
LABEL: used to add metadata to the image
Export: Specifies the port for external interaction
VOLUME: used to specify the persistent directory
background
Previously, we have explained how docker operates containers and images. The images explained here are ready-made images contributed by third parties, but docker will also be used to make our own images in actual production, and this content is also one of the highlights. To create your own project image, you need to use the Dockerfile file. It is a text file used to configure the image. Docker generates binary image files based on this file.
Dockerfile concept
Dockerfile is a text document that contains commands for combining images. You can use any command in the command line. Docker automatically generates an image by reading the instructions in the dockerfile.
This file has many knowledge points. How to make a good image file requires an in-depth understanding of the configuration and principle of Dockerfile. Here, we just share the common configurations of Dockerfile. Basically, it can meet the daily routine development requirements.
Dockerfile is the full name of a file without extension, as follows:
Dockerfile demo:
FROM openjdk:8 COPY . /future/ WORKDIR /future EXPOSE 8000
Don't worry about understanding the above demo. It will be explained step by step below.
Basic structure of Dockerfile
Dockerfile is generally divided into four parts: basic image information, maintainer information, image operation instructions and instructions executed when the container is started
be careful
The four parts mentioned above do not refer to four lines, but four categories
The first 2 characters start with # and are comments in Dockerfile.
Dockerfile file description
Docker runs the Dockerfile instructions in top-down order. In order to specify the basic image, the first instruction must be FROM. A declaration that begins with a character is considered a comment. RUN, CMD, FROM, EXPOSE, ENV and other instructions can be used in docker files.
FROM: Specifies the base image, which must be the first command
Generally, it is the syntax at the beginning of Dockfile. Its function is to specify the base image of the newly created docker image, such as
FROM openjdk:8
You may be curious about the meaning of openjdk:8. In fact, I called the image published on the docker hub. I don't need to configure the JDK environment myself. Similar to github, we can download the required code from the code warehouse implemented by Niu Ren, and then develop based on these code. Some people may be confused at this time. Why do I know to use it like this? In fact, it is based on the guidance on dockerhub, as shown in the figure below. Later, after reading the whole series of tutorials and understanding the construction principle of docker, you can draw inferences from one instance. Usually, the environment we want to use is from the docker hub.
MAINTAINER: MAINTAINER information
This is used to annotate the image. Optional
Format: MAINTAINER <name> Example: MAINTAINER Jasper Xu MAINTAINER sorex@163.com MAINTAINER Jasper Xu <sorex@163.com>
RUN: command executed when building a mirror
It is equivalent to executing commands. For example, if we need to install some software in the image, we need to use the RUN syntax. However, it should be noted that since each RUN operation will create a new image in the docker, we try to combine some commands into one. Here, we can use the backslash to wrap lines to make it beautiful to read.
RUN It is used to execute commands in the mirror container. There are two command execution modes: shell implement Format: RUN <command> exec implement Format: RUN ["executable", "param1", "param2"] Example: RUN ["executable", "param1", "param2"] RUN apk update RUN ["/etc/execfile", "arg1", "arg1"] Note: RUN The intermediate image created by the instruction will be cached and used in the next build. If you do not want to use these cache images, you can specify them at build time--no-cache Parameters, such as: docker build --no-cache
Multiple merges:
RUN yum update && \ yum install -y vim
CMD: call after building the container, that is, when the container starts.
Format: CMD ["executable","param1","param2"] (Executable, priority) CMD ["param1","param2"] (Set ENTRYPOINT,Call directly ENTRYPOINT Add parameter) CMD command param1 param2 (implement shell Internal command) Example: CMD echo "This is a test." | wc - CMD ["/usr/bin/wc","--help"]
Difference between CMD and RUN
CMD is different from RUN. CMD is used to specify the command to be executed when the container is started, while RUN is used to specify the command to be executed when the image is built
ENTRYPOINT
Configure the container to make it executable. With CMD, "application" can be omitted and only parameters can be used.
Format: ENTRYPOINT ["executable", "param1", "param2"] (Executable file, first) ENTRYPOINT command param1 param2 (shell Internal command) Example: FROM ubuntu ENTRYPOINT ["top", "-b"] CMD ["-c"]
Attention
ENTRYPOINT is very similar to CMD, except that the command executed through docker run will not overwrite ENTRYPOINT, and any parameters specified in docker run will be passed to ENTRYPOINT again as parameters. Only one entry point command is allowed in Dockerfile. Multiple assignments will override the previous settings and only the last entry point command will be executed.
ADD: adds a local file to the container
tar files will be decompressed automatically (compressed network resources will not be decompressed). You can access network resources, similar to wget
Format: ADD <src>... <dest> ADD ["<src>",... "<dest>"] Used to support paths with spaces Example: ADD hom* /mydir/ # Add all files starting with "hom" ADD hom?.txt /mydir/ # ? Replace a single character, for example: "home.txt" ADD test relativeDir/ # Add "test" to 'workdir' / relativedir/ ADD test /absoluteDir/ # Add "test" to / absoluteDir/
COPY
The function is similar to ADD, but it will not automatically decompress files or access network resources
ADD and COPY comparison
WORKDIR
Similar to the cd command under linux.
Note:
1. After setting the working directory through WORKDIR, the subsequent commands RUN, CMD, ENTRYPOINT, ADD, COPY, etc. in Dockerfile will be executed in this directory. When using docker run to RUN the container, you can override the working directory set during construction with the - w parameter.
2. Try to use WORKDIR instead of RUN cd (as mentioned earlier, RUN can RUN any shell command)
3. Try to use absolute path (i.e. starting with /)
Format: WORKDIR /path/to/workdir Example: WORKDIR /a (The working directory is/a) WORKDIR b (The working directory is/a/b) WORKDIR c (The working directory is/a/b/c)
ARG: used to specify the variables passed to the build runtime
Format: ARG <name>[=<default value>] Example: ARG site ARG build_user=www
ENV
LABEL maintainer = "Author's name" LABEL version = "1.0" LABEL description = "describe"
When setting environment variables for the current container, we can use ENV to set a constant. For example, if we want to install a 5.7 MYSQL, we can do so
ENV MYSQL_VERSION 5.7 # Set constant MySQL_ The version value is 5.7 RUN apt-get install -y mysql-server = "${MYSQL_VERSION}" #Reference constant
LABEL: used to add metadata to the image
Add labels to the created image, such as author information, version information, description information, etc.
Format: LABEL <key>=<value> <key>=<value> <key>=<value> ... Example: LABEL version="1.0" description="This is a Web The server" by="IT record" Note: use LABEL When specifying metadata, a LABEL Specify that one or more metadata can be specified. When multiple metadata are specified, different metadata are separated by spaces. It is recommended to pass all metadata through one LABEL Directive to avoid generating too many intermediate images.
In fact, LABEL is more like the comments when we write code
LABEL maintainer = "Author's name" LABEL version = "1.0" LABEL description = "describe"
Export: Specifies the port for external interaction
The function is to listen to the external port when the container is running
Format: EXPOSE <port> [<port>...] Example: EXPOSE 80 443 EXPOSE 8080 EXPOSE 11211/tcp 11211/udp
Note: export does not allow the port of the container to access the host. To make it accessible, you need to publish these ports through - P when docker run runs the container, or publish all ports exported by export through the - P parameter
VOLUME: used to specify the persistent directory
The mount function can be realized. The container tells Docker to create a directory on the host (in / var / lib / Docker by default), and then mount it to the specified path. When you delete a container that uses the Volume, the Volume itself will not be affected, and it can exist forever. This is usually the case with database services.
Format: VOLUME ["/path/to/dir"] Example: VOLUME ["/data"] VOLUME ["/var/www", "/var/log/apache2", "/etc/apache2"
FROM centos #Mount the storage contents of the image to the test folder. In this way, even if the image is deleted and recreated, the data will not be affected VOLUME /test CMD echo "hello docker"
Attached is a particularly understandable picture from netizens: