Jenkins不等待docker exec命令完成

问题描述:

这里是情况:

我有一个docker容器(詹金斯)。我已将套接字安装到容器上,以便可以在jenkins容器内执行docker命令。

I have a docker container (jenkins). I've mounted the sockets to my container so that I can perform docker commands inside my jenkins container.

手动,一切都在容器中进行。但是,当詹金斯执行作业时,它不会等待 docker exec 命令执行完毕。

Manually, everything works in the container. However, when Jenkins executes the job, it doesn't "wait" for the docker exec command to run to completion.

下面是Jenkinsfile的摘录。短暂的 printenv 命令正确运行,并打印环境变量。仅运行下一个命令(python),然后Jenkins立即继续前进,而无需等待完成。 Jenkins代理(从属)在Ubuntu映像上运行。在Jenkins外部运行所有这些命令,按预期方式工作。

Below, is an extract from the Jenkinsfile. The short-lived printenv command runs correctly, and prints the environment variables. The next command (python) just gets run and then Jenkins moves on immediately, not waiting for completion. The Jenkins agent (slave) is running on an Ubuntu image. Running all these commands outside Jenkins work as expected.

echo "Running the app docker container in detached tty mode to keep it up"
docker run --detach --tty --name "${CONTAINER_NAME}" "${IMAGE_NAME}"

echo "Listing environment variables"
docker exec --interactive "${CONTAINER_NAME}" bash -c "printenv"

echo "Running test coverage"
docker exec --interactive "${CONTAINER_NAME}" bash -c "python -m coverage run --source . --branch -m pytest -vs"

似乎与这个问题

请任何人解释如何让Jenkins等待 docker exec 命令完成,然后再继续下一步。

Please can anyone explain how to get Jenkins to wait for the docker exec command to complete before proceeding to the next step.

已经考虑了替代方案,例如Docker Pipeline插件,但更喜欢使用与我现有的接近的替代品

Have considered alternatives, like the Docker Pipeline Plugin, but would much prefer to use something close to what I have above where possible.

好,我尝试使用Docker Pipeline插件此处

Ok, another approach, I've tried using Docker Pipeline plugin here.

您可以使用 docker.sock 作为卷装入,在您的 docker-compose中协调主机上的容器。 yml

You can use docker.sock as volume mount to orchestrate containers on your host machine like this in your docker-compose.yml

volumes:
  - /var/run/docker.sock:/var/run/docker.sock

根据您的设置,您可能需要运行

Depending on your setup you might need to run

chmod 666 /var/run/docker.sock

获得第一名。

这在macOS和Linux上均可。

This works on macOS as well as Linux.