- Commit a container with new configurations (–change)
- 提交容器ID:c3f279d17e0a,
- 仓库/镜像名:svendowideit/testimage
- TAG:version3
- 修改配置:–change “ENV DEBUG=true”
- Commit a container with new CMD and EXPOSE instructions
- 提交容器ID:c3f279d17e0a,
- 仓库/镜像名:svendowideit/testimage
- TAG:version4
- 修改配置: --change=‘CMD [“apachectl”, “-DFOREGROUND”]’
- 修改配置:-c “EXPOSE 80”
3.2 使用Dockerfile 构建镜像
编写 Dockerfile 文件,使用docker build命令来构建镜像。
3.2.1 Docker官方文档-Dockerfile
Dockerfile
It all starts with a Dockerfile.
这一切都是从Dockerfile开始的。
Docker builds images by reading the instructions from a Dockerfile. A Dockerfile is a text file containing instructions for building your source code. The Dockerfile instruction syntax is defined by the specification reference in the Dockerfile reference.
Docker通过从Dockerfile中读取指令来构建镜像。Dockerfile是一个文本文件,包含构建源代码的指令。Dockerfile指令语法在参考规范中定义,详见Dockerfile reference。
Here are the most common types of instructions:
以下是最常见的指令类型:
- FROM :定义镜像的基础镜像。
- RUN :在当前镜像顶部的新层中执行命令并提交结果。RUN还有一个shell形式,用于运行命令。
- WORKDIR :为Dockerfile中的任何RUN、CMD、ENTRYPOINT、COPY和ADD指令设置工作目录。
- COPY :从复制新文件或目录,并将它们添加到容器文件系统的路径中。
- CMD :用于定义使用镜像启动容器后运行的默认程序。每个Dockerfile应该只有一个CMD,如果存在多个CMD时,只有最后一个生效。
Dockerfiles are crucial inputs for image builds and can facilitate automated, multi-layer image builds based on your unique configurations. Dockerfiles can start simple and grow with your needs to support more complex scenarios.
Dockerfiles是镜像构建的关键输入,它可以根据您特定配置,自动化构建多层镜像。Dockerfiles可以从简单开始,并随着您的需求而增长,以支持更复杂的场景。
Filename
The default filename to use for a Dockerfile is Dockerfile, without a file extension. Using the default name allows you to run the docker build command without having to specify additional command flags.
Dockerfile使用的默认文件名是Dockerfile,没有文件扩展名。使用默认名称可以运行docker构建命令,而无需指定其他命令标志。
Some projects may need distinct Dockerfiles for specific purposes. A common convention is to name these .Dockerfile. You can specify the Dockerfile filename using the --file flag for the docker build command. Refer to the docker build CLI reference to learn about the --file flag.
某些项目可能需要不同的Dockerfile来实现特定目的。一个通常的约定是使用<something>.Dockerfile形式命名。您可以使用docker构建命令的–file标志来指定Dockerfile文件名。请参阅docker build CLI参考,了解–file标志。
Note
We recommend using the default (Dockerfile) for your project’s primary Dockerfile.
注意
我们推荐使用默认(Dockerfile)作为您项目的主Dockerfile。