在编写 Dockerfile 时,我们可能需要将多条指令合并成一行,这样语法简洁,并且可以减少镜像层数。

将多条 COPY 指令合并为一条

比如,我们的 Dockerfile 里面有这样的语句:

1FROM python:2.7-stretch
2WORKDIR /xdhuxc/
3COPY dingtalk_callback.py /xdhuxc/
4COPY requirements.txt /xdhuxc/
5COPY settings.py /xdhuxc/
6RUN pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple
7ENTRYPOINT ["python", "-u", "/xdhuxc/dingtalk_callback.py"]

我们想把多个 COPY 指令合并成一条,可以改写该 Dockerfile 为如下形式:

1FROM python:2.7-stretch
2WORKDIR /xdhuxc/
3COPY dingtalk_callback.py requirements.txt settings.py /xdhuxc/
4RUN pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple
5ENTRYPOINT ["python", "-u", "/xdhuxc/dingtalk_callback.py"]

或者

1FROM python:2.7-stretch
2WORKDIR /xdhuxc/
3COPY ["dingtalk_callback.py", "requirements.txt", "settings.py", "/xdhuxc/"]
4RUN pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple
5ENTRYPOINT ["python", "-u", "/xdhuxc/dingtalk_callback.py"]

路径中包含空格时,必须使用这种形式。

COPY 指令支持通配符,如果复制多个文件时,文件名称本身有规律,则可以使用通配符来实现

1FROM python:2.7-stretch
2WORKDIR /xdhuxc/
3COPY dingtalk* /xdhuxc/ # 复制所有以 "dingtalk" 开头的文件到 /xdhuxc/ 目录下
4RUN pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple
5ENTRYPOINT ["python", "-u", "/xdhuxc/dingtalk_callback.py"]

但要注意,COPY 只能复制目录下的内容,不能复制目录,对于目录的复制,要这样写

1COPY templates/ /scmp/templates

其中,templates 为目录名,/scmp/templates路径中的templates相当于新的目录名。

参考资料

https://docs.docker.com/engine/reference/builder/#copy