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...
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.
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.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With