2021-01-25 - (non)interactive docker container

you can run docker container to do some non-interactive processing. eg with:

docker run --rm myalgo:4.2 --foo --bar

when you need to do it interactively, you just add -it:

docker run -it --rm myalgo:4.2 --foo --bar

so far, so good. now how about CI scripts, that on CI server are being run in a non-interactive way, but you also want the very same scripts to be runnable interactively by developers (so that they can eg. ^C it during the run)? it turns out it is fairly simple with tty command to detect if terminal is interactive or not and bash arrays to pass parameters to docker:

EXTRA_FLAGS=()
if tty -s
then
  EXTRA_FLAGS+=("-it")
fi
 
docker run \
  "${EXTRA_FLAGS[@]}" \
  --rm \
  myalgo:4.2 --foo --bar

as a free bonus you can now add multiple optional flags, to the same array and/or use it for all the arguments, to simplify the invocation of docker.

blog/2021/01/25/2021-01-25_-_non_interactive_docker_container.txt · Last modified: 2021/06/15 20:09 by 127.0.0.1
Back to top
Valid CSS Driven by DokuWiki Recent changes RSS feed Valid XHTML 1.0