0

In my Travis CI, part of my verification is to start a docker container and verify that it doesn't fail within 10 seconds. I have a yarn script docker:run:local that calls docker run -it <mytag> node app.js.

If I call the yarn script with timeout from a bash shell, it works fine:

$ timeout 10 yarn docker:run:local; test $? -eq 124 && echo "Container ran for 10 seconds without error"

This calls docker run, lets it run for 10 seconds, then kills it (if not already returned). If the exit code is 124, the timeout did expire, which means the container was still running. Exactly what I need to verify that my docker container is reasonably sane.

However, as soon as I run this same command from within a script, either in a test.sh file called from the shell, or if putting it in another yarn script and calling yarn test:docker, the behaviour is completely different. I get:

ERRO[0000] error waiting for container: context canceled

Then the command hangs forever, there's no 10 second timeout, I have to ctrl-Z it and then kill -9 the process. If I run top I now have a docker process using all my CPU forever. If using timeout with any other command like sleep 20 && echo "Finished sleeping", this does not happen, so I suspect it may have something to do with how docker works in interactive mode or something, but that's only my guess.

What's causing timeout docker:run to fail from a script but work fine from a shell and how do I make this work?

2
  • Try using docker -a as opposed to docker -i Commented Dec 10, 2020 at 15:47
  • Actually removing -it completely seems to have resolved this, and I don't actually need interactive mode to start my application. Thanks. Commented Dec 10, 2020 at 15:54

1 Answer 1

1

Looks like running docker in interactive mode is causing the issue.

Run docker in detached more by removing the -it and allowing it to run in default detached mode or specify -d instead of -it and so:

docker run -d <mytag> node

or

docker run <mytag> node
Sign up to request clarification or add additional context in comments.

1 Comment

Not sure what signal the timeout command uses, on Mac I had to pass -s 9 and -k 2 parameters to force kill after 2 seconds since the timeout if the app keeps running. Also, for future reference, on Mac the command is gtimeout and needs to be installed via brew install coreutils. So the final command looked like this gtimeout -k 2 -s 9 8 docker run -e ENV1=VAL1 <docker_image_id>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.