我有一个包含多个阶段的Dockerfile。一个中间阶段进行测试。如果测试失败,我希望构建停止,但下面继续进行。您可以看到输出,该输出将继续在失败的RUN命令之后继续显示。
# Base image
FROM python:3.5 as base
# For running tests
FROM base
WORKDIR /root
RUN mkdir /root/src
ADD src/requirements.txt /root/src
RUN pip install -r /root/src/requirements.txt
ADD . /root
RUN ["/bin/bash", "-c", "cd test; ./run-tests.sh"]
# For publishing src files
FROM base
ADD src /root/src
ADD .pypirc /root/.pypirc
WORKDIR /root/src
CMD python setup.py sdist upload -r local
输出:
Step 6/13 : RUN pip install -r /root/src/requirements.txt
---> Using cache
---> 56fa7fc2f2e8
Step 7/13 : ADD . /root
---> 74c52977edcf
Step 8/13 : RUN ["/bin/bash", "-c", "cd test; ./run-tests.sh"]
---> Running in 68a184ab54af
...
----------------------------------------------------------------------
Ran 1 test in 10.122s
FAILED (failures=1)
The command '/bin/bash -c cd test; ./run-tests.sh' returned a non-zero code: 1
running sdist
running egg_info
...
running upload
Submitting dist/<artifact-name>-2.6.1.2.tar.gz to https://<subdomain>.jfrog.io/<context>/api/pypi/python-local
...
答案 0 :(得分:1)
您的命令run-tests.sh
需要使用非零退出代码退出,而docker将停止构建。在这种情况下,发生了这种情况:
The command '/bin/bash -c cd test; ./run-tests.sh' returned a non-zero code: 1
无论您想调用什么docker build
,都需要处理该退出代码并在此时停止运行。 Docker的行为是为您提供退出代码以指示失败:
$ cat df.fail
FROM busybox
RUN exit 1
RUN echo still running
$ docker build -f df.fail .
Sending build context to Docker daemon 23.04kB
Step 1/3 : FROM busybox
---> 59788edf1f3e
Step 2/3 : RUN exit 1
---> Running in 70d90fb88d6e
The command '/bin/sh -c exit 1' returned a non-zero code: 1
$ echo $?
1
从上面的示例中,您可以看到docker在命令返回非零退出代码后立即停止,它没有运行echo still running
行,并且{ {1}}本身,您可以用其运行构建的任何内容进行处理。