For a detailed explanation of Dockerfile, it is enough to read this article

There are two types of instruction formats: comments and instructions

Comments begin with a pound sign followed by information

Commands start with an uppercase command name followed by parameters

There are two types of instruction formats: comments and instructions

Comments begin with a pound sign followed by information

Commands start with an uppercase command name followed by parameters

common instructions

FROM

The two forms are as follows:

FROM <IMAGE>FROM <IMAGE>:<TAG>
The image name specified by FROM must be an existing image, this image is called the base image, and must be located in the first non-comment instruction

MAINTAINER

MAINTAINER <NAME>
Specify the author information of the image, including the owner and contact information of the image

RUN

Used to specify the command to run when building the image, two modes:

RUN <command> (shell模式)RUN [ "executable", "param1", "param2" ] (exec模式)
In shell mode, use /bin/sh -c COMMAND to run commands
In exec mode, you can specify other shells to run commands RUN [“/bin/bash”, “-c”, “echo hello”]

Multiple RUN instructions can be combined into one:

RUN yum install httpd && yum install ftp
This will reduce the generation of intermediate layer images during construction

EXPOSE

Specify the port used by the container running the image, which can be multiple.

EXPOSE <PORT>

The purpose of using this command is to tell the port that the application in the application container will use. You also need to use the -p parameter to specify the mapped port at runtime. This is for docker's security purpose and will not automatically open the port.

docker run -p 80 -d dockertest/dockerfile_build nginx -g "daemon off"

CMD

Used to provide the default command for the container to run, if in docker

If the command to run is specified during run, the CMD command will not be executed.

CMD has three modes:

CMD <command> (shell模式)
CMD [ "executable", "param1", "param2" ] (exec模式)
CMD [ 'param1', 'param2'] (通常与ENTRYPOINT搭配指定ENTRYPOINT的默认参数)

ENTRYPOINT

Similar to CMD, ENTRYPOINT will not be accessed by docker

The command specified in run is overwritten. If you want to overwrite ENTRYPOINT, you need to use it in docker

Specify the --entrypoint option in run

It has two modes:

ENTRYPOINT <command> (shell模式)
ENTRYPOINT [ "executable", "param1", "param2" ] (exec模式)

ADD and COPY

The role is to copy files or directories to the image built by Dockerfile

ADD <src> <dest>
ADD ["<src>" "<dest>"] (适用于文件路径包含空格的情况)
COPY <src> <dest>
ADD ["<src>" "<dest>"] (适用于文件路径包含空格的情况)
ADD includes a tar-like decompression function. If you just copy the file, it is recommended to use COPY. Moreover, the source file path of the two uses the relative path of the Dockerfile, and the target path uses the absolute path.
COPY index.html /var/www/html

VOLUME

Used to add volumes to containers, which can provide functions such as shared storage

VOLUME ['/data']

WORKDIR

Set the working directory inside the container, so that the commands specified by ENTRYPOINT and CMD will be executed in this directory in the container.

WORKDIR /path/to/workdir

ENV

用于设置环境变量

ENV <KEY> <VALUE>
ENV <KEY>=<VALUE>

USER

用于指定镜像为什么用户去运行

USER nginx
镜像就会以nginx身份运行,可以使用uid,gid等各种组合使用

ONBUILD

为镜像创建触发器,当一个镜像被用作其他镜像的基础镜像时,这个触发器会被执行。当子镜像被构建时会插入触发器中的指令。

ONBUILD COPY index.html /var/www/html

Dockerfile的构建过程

  1. docker会从Dockerfile文件头FROM指定的基础镜像运行一个容器

  1. 然后执行一条指令,对容器修改

  1. 接着执行类似docker commit的操作,创建新的镜像层

  1. 在基于刚创建的镜像运行一个新的容器

  1. 执行Dockerfile下一条指令,直到所有指令执行完毕

docker会删除中间层创建的容器,但不会删除中间层镜像,所以可以使用docker run运行一个中间层容器,从而查看每一步构建后的镜像状态,这样就可以进行调试。

Guess you like

Origin blog.csdn.net/robinhunan/article/details/128911727