【docker】Common commands for building images in Dockerfile:


1. Commonly used commands:

Dockerfile is a text file that contains instructions one by one. Based on the specified image, one instruction builds a layer, and finally builds a new image.

keywords effect Remark Format
FROM Specify the base image Specify which mirror to start building on FROM <image>:<tag>
MAINTAINER author information Indicate who wrote this dockerfile (deprecated) MAINTAINER xxx xxx
LABEL Label Add metadata to the image, in the form of key-value pairs, which can understand similar comments LABEL <key>=<value> <key>=<value> <key>=<value>
RUN Commands executed when making an image The command to run when building the image, the default is /bin/sh RUN <命令行命令>或RUN["可执行文件", "参数1", "参数2"...,"参数n"](相当于:RUN 可执行文件 参数1 参数2 … 参数n)
CMD Commands to execute when the container starts The shell environment to execute when running the container. Each Dockerfile can only have one CMD command. If multiple commands are specified, only the last one will be executed. There are 3 formats:【1】exec执行:CMD ["executable","param1","param2"];【2】/bin/sh中执行:CMD command param1 param2;【3】提供给 ENTRYPOINT 的默认参数:CMD ["param1","param2"]
ENTRYPOINT Entrance Generally, it will be used in the production of some containers that are executed and closed ENTRYPOINT ["executable", "param1", "param2"]
COPY Copy files to mirror Copy files to the mirror when building COPY 源路径 目标路径(The source path is the path of the host machine, and the target path is the path of the container)
ADD add files Add files to the image when building. The difference from COPY is that ADD will automatically decompress ADD 源路径 目标路径(The source path is the path of the host machine, and the target path is the path of the container)
ENV environment variable When building the image, set the environment variable, which can be overwritten by -e when starting the container (that is, it can also be used in the container) ENV <key> <value>
ARG build parameters Used to specify the variables passed to the build runtime (passing parameters to the dockerfile), which is equivalent to passing parameters externally to the inside when building a mirror image ARG <name>[=<default value>]
VOLUME Define anonymous data volumes that can be mounted externally If you forget to mount the data volume when starting the container, it will be automatically mounted to the anonymous volume VOLUME ["/xxx/xx/xxxx", "xxx", ..., "xxx"]
EXPOSE Declare the service port of the container Only declare the service port of the container (and the port that provides the service) EXPOSE 端口号
WORKDIR Work list Specify the starting working directory of the container (it will be created automatically if it does not exist). After setting, subsequent commands such as RUN, CMD, ENTRYPOINT, ADD, and COPY in the Dockerfile will execute WORKDIR in this directory 最好是绝对路径
USER Specifies the container execution user Specify the user name or ID execution user when running the container USER user

2. Matters needing attention:

  1. Dockerfile is essentially a file that describes the process of building an image through instructions.
  2. Dockerfile的注释符号是#。
  3. Dockerfile的第一行必须是FROM,从一个基础镜像来构建。
  4. 通过Dockerfile构建镜像命令格式为:docker build -f Dockerfile文件路径 -t 镜像名称:版本 .
  1. 默认一般把文件命名为Dockerfile,然后构建命令直接用docker build -t 要保存的镜像名称:版本 .
  2. 最后的.的含义是将当前目录告诉docker引擎,然后当执行docker build命令后,本机将当前目录下的所有文件发送给docker引擎,最后由docker引擎完成镜像的构建

三、add和copy的区别:

Dockerfile 中的 ADD 与 COPY 指令都可以用于将本地文件或目录复制到 Docker 镜像中,但它们之间有一些区别。

【1】ADD 指令支持自动解压缩功能

当使用 ADD 指令将本地文件复制到 Docker 镜像中时,如果该文件是压缩包格式,Docker 会自动解压缩该文件。例如:

ADD nginx-1.21.0.tar.gz /usr/local/

上述例子中在将本地的 nginx-1.21.0.tar.gz 文件复制到 Docker 镜像的 /usr/local/ 目录下时,Docker 会将该文件解压缩。
而 COPY 指令并不支持自动解压缩功能,需要手动解压缩后再复制进镜像。

【2】ADD 指令可以从 URL 复制内容

ADD 指令除了能够复制本地文件和目录外,还可以复制远程文件(例如从 URL 下载)到 Docker 镜像中。例如:

ADD https://example.com/nginx-1.21.0.tar.gz /usr/local/

上述例子中会从远程 URL https://example.com/nginx-1.21.0.tar.gz 下载文件,并将其复制到 Docker 镜像的 /usr/local/ 目录下。
而 COPY 指令只能复制本地文件和目录。

【3】 ADD 指令具有隐式的文件拷贝功能

ADD 指令除了能够复制本地文件和目录外,还具有一个隐式的文件拷贝功能:当复制一个压缩包文件到容器中时,Docker 会自动解压,并且可以直接从 URL 下载文件并解压缩。例如:

ADD package.tar.gz /app/

上述例子中,如果 package.tar.gz 是一个压缩包文件,则 Docker 会自动解压缩该文件,并将其复制到 /app/ 目录下。而 COPY 指令无法实现这一功能。

Guess you like

Origin blog.csdn.net/weixin_53791978/article/details/132171466