arcseldon
arcseldon

Reputation: 37105

Jenkins does not wait for docker exec command to complete

Here's the situation:

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.

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.

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"

It seems maybe related to this question.

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

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

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

Upvotes: 0

Views: 3427

Answers (2)

ckaserer
ckaserer

Reputation: 5702

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

to get going in the first place.

This works on macOS as well as Linux.

Upvotes: 1

arcseldon
arcseldon

Reputation: 37105

Ugh. This was down to the way that I'd set up docker support on the slave container.

I'd used socat to provide a TCP server proxy. Instead, switched that out for a plain old docker.sock volume between host & container.

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

The very first time, I had to also sort out a permissions issue by doing (inside the container):

rm -Rf ~/.docker 
chmod 666 /var/run/docker.sock

After that, everything "just worked". Very painful experience.

Upvotes: 0

Related Questions