Dockerfile-使用目标的哪个选项? [英] Dockerfile - Which option to use the target?
问题描述
我有dockerfile
I have dockerfile
FROM cypress/base:12.1.0 as hlg
RUN mkdir /app WORKDIR /app COPY . /app
RUN npm install
RUN $(npm bin)/cypress verify
RUN ["npm", "run", "cy:runhlg"]
FROM cypress/base:12.1.0 as tst
RUN mkdir /app WORKDIR /app COPY . /app
RUN npm install
RUN $(npm bin)/cypress verify
RUN ["npm", "run", "cy:runtst"]
然后我构建它
docker build -t cypress --target tst .
但是,当我使用目标"tst"运行时,每次运行只需要运行一次.最终两者都运行.
But I need to run only one per run, when I run with the target "tst" it ends up running both.
我试图用"if"来做,但是也没有成功
I tried to do it with "if", but without success too
RUN if [ "$arg" = "hlg" ] ; then echo ["npm", "run", "cy:runhlg"] ; else echo ["npm", "run", "cy:runhlg"]; fi
docker build -t cypress --build-arg hlg .
我可以用其他方式做到吗?
Can I do it the other way?
推荐答案
泊坞窗中的经典构建引擎将按顺序处理多阶段构建,直到完成目标阶段,或者未指定目标最后阶段.因此,第一阶段将始终被构建.
The classic build engine in docker will process a multi-stage build sequentially until completing the target stage, or the final stage is no target is specified. So the first stage would always be built.
如果切换到buildkit,它将Dockerfile处理为依赖关系图,并跳过未使用的阶段.您可以使用以下命令在当前shell中启用buildkit:
If you switch to buildkit, it processes the Dockerfile into a dependency graph and skips unused stages. You can enable buildkit in your current shell with:
export DOCKER_BUILDKIT=1
然后您可以通过启用/etc/docker/daemon.json
( daemon.json
也可以在docker的桌面版本中的首选项菜单之一下设置):
And you can make buildkit the default for all builds in the docker engine by enabling a feature flag in /etc/docker/daemon.json
(the daemon.json
can also be set in the desktop versions of docker under one of the preferences menus):
{
"features": {"buildkit": true }
}
要处理 daemon.json
文件,请重新加载Docker引擎,例如 systemctl重新加载docker
.
To process the daemon.json
file, reload the docker engine, e.g. systemctl reload docker
.
这篇关于Dockerfile-使用目标的哪个选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!