Docker study notes (5)-the role of Dockerfile file and how to build a mirror

What is Dockerfile?

Dockerfile is a text file used to build an image. The text content contains instructions and instructions for building an image.

Use Dockerfile to customize the image

1. To customize an nginx image below (the built image will have a /usr/share/nginx/html/index.html file)
in an empty directory, create a new file named Dockerfile, and add the following in the file content:

FROM nginx
RUN echo '这是一个本地构建的nginx镜像' > /usr/share/nginx/html/index.html

2. The role of FROM and RUN instructions

FROM:定制的镜像都是基于 FROM 的镜像,这里的 nginx 就是定制需要的基础镜像。后续的操作都是基于 nginx。

RUN:用于执行后面跟着的命令行命令。有以下俩种格式:
shell 格式:
RUN <命令行命令>
# <命令行命令> 等同于,在终端操作的 shell 命令。
exec 格式:
RUN ["可执行文件", "参数1", "参数2"]
# 例如:
# RUN ["./test.php", "dev", "offline"] 等价于 RUN ./test.php dev offline

Note : Every time the instructions of the Dockerfile are executed, a new layer will be created on the docker. So too many meaningless layers will cause the image to expand too much. E.g:

FROM centos
RUN yum install wget
RUN wget -O redis.tar.gz "http://download.redis.io/releases/redis-5.0.3.tar.gz"
RUN tar -xvf redis.tar.gz
以上执行会创建 3 层镜像。可简化为以下格式:
FROM centos
RUN yum install wget \
&& wget -O redis.tar.gz "http://download.redis.io/releases/redis-5.0.3.tar.gz" \
&& tar -xvf redis.tar.gz
如上,以 && 符号连接命令,这样执行后,只会创建 1 层镜像

Start to build the image

In the storage directory of the Dockerfile file, execute the build action.

The following example builds a nginx:v3 (image name: image label) from the Dockerfile in the directory.

Note: The last . Represents the context path of this execution, which will be introduced in the next section.

docker build -t nginx:v3 .

Insert picture description here
The context of the path
one on, the last mentioned directive . Is the context path, then what is the context of the path it?

docker build -t nginx:v3 .

The context path refers to when docker is building a mirror, sometimes it wants to use the local files (such as copying). After the docker build command knows this path, it will package all the content under the path.

Analysis: Because the operating mode of docker is C/S. Our native machine is C, and the docker engine is S. The actual build process is completed under the docker engine, so our local files cannot be used at this time. This requires that the files in the specified directory of our local machine are packaged together for use by the docker engine.

If the last parameter is not specified, the default context path is the location of the Dockerfile.

Note: Do not put useless files in the context path, because they will be packaged and sent to the docker engine. If there are too many files, the process will be slow.

Guess you like

Origin blog.csdn.net/m0_45388819/article/details/109547207