Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins is not waiting for Docker command to finish

Tags:

docker

jenkins

I'm trying to get Jenkins to run the following command within docker on a Jenkins slave:

docker run -i -v `pwd`:/opt/myapp -w /opt/myapp -t mydockerimage /bin/bash -c "./setup_dev_env.sh && make all"

The trouble is that when I run this from within Jenkins the Docker process is spawned, but Jenkins doesn't wait for it and returns success immediately. If, however, I replace the Docker command with an infinite loop Jenkins will wait, as expected.

When I run this command from the build slave directly, as the Jenkins user, the system waits for the command to finish (which is what I want to happen).

I've tried doing a docker attach, but that also returns immediately. docker wait will cause Jenkins to wait for it, but I won't see any of the output.

Perhaps I'm going about this all wrong...

  1. How can I get Jenkins to wait for this command to finish?
  2. How can I get Jenkins to display the output of the command running in the docker container?
  3. How can I get the exit code of the command run in Docker to be set such that Jenkins can determine if it succeeded or failed?
like image 821
Tom Hennen Avatar asked Jul 17 '14 12:07

Tom Hennen


People also ask

How do I wait for Docker?

Start a container in the background. Run docker wait , which should block until the container exits. In another terminal, stop the first container. The docker wait command above returns the exit code.

How do I keep Docker containers running after exit?

Method 1: You can use the -t (pseudo-tty) docker parameter to keep the container running. Method 2: You can run the container directly passing the tail command via arguments as shown below. Method 3: Another method is to execute a sleep command to infinity.


1 Answers

It turns out the problem was the -t in the command line. Removing -t caused Jenkins to wait for docker to finish what it was doing.

I had a hunch this was the case because ttys can sometimes be strange and Jenkins is probably using a different type of tty (or not at all) than I am when I'm running the same command at the shell prompt.

In the end, this command worked:

docker run -i -v `pwd`:/opt/myapp -w /opt/myapp mydockerimage /bin/bash -c "./setup_dev_env.sh && make all"
like image 148
Tom Hennen Avatar answered Nov 01 '22 19:11

Tom Hennen