1. 带有 ONBUILD
的场景
基本的Dockerfile
FROM ubuntu:latest
RUN apt-get update && apt-get install python3
ONBUILD COPY test.py test.py
显然,当我们构建上述Dockerfile
(test-image:latest
)时,COPY
不会受到影响。(即未复制test.py
)
现在是onbuild Dockerfile
FROM test-image:latest
现在,当我们构建上述Dockerfile
时,COPY
会生效,复制test.py
2. 没有使用ONBUILD
的情况
我可以不使用ONBUILD
来实现同样的功能。
基本Dockerfile
FROM ubuntu:latest
RUN apt-get update && apt-get install python3
以上Dockerfile
构建Docker镜像,其中包含python3
(test-image2:latest
)。
现在是子Docker镜像的Dockerfile
FROM test-image2:latest
COPY test.py /test.py
所以,我的问题是,为什么应该使用
ONBUILD
或何时应该使用?是否存在性能差异?