6

I have a docker container that runs a script via the entrypoint directive. The container closes after the entrypoint script is finished. I need to get the exit code from the script in order to do some logging if the script fails. Right now I'm thinking of something like this

docker run container/myContainer:latest

if [ $? != 0 ];
then
    do some stuff
fi

Is this proper way to achieve this? Specifically, will this be the exit code of docker run or of my entrypoint script?

1
  • 2
    Yes the $? is the exit status of the last command executed.
    – Jetchisel
    Mar 10, 2020 at 23:11

2 Answers 2

7

Yes, the docker container run exit code is the exit code from your entrypoint/cmd:

$ docker container run busybox /bin/sh -c "exit 5"

$ echo $?
5

You may also inspect the state of an exited container:

$ docker container inspect --format '{{.State.ExitCode}}' \
    $(docker container ls -lq)
5
3
  • what if the entrypoint contains a pipeline? Is it possible to use "${PIPESTATUS[0]}"?
    – tuxErrante
    May 4, 2022 at 15:19
  • @tuxErrante Docker and the Linux kernel doesn't know what a pipeline is, that's a shell thing. If the shell exits with a non-zero, docker will accept that. So you need your shell to handle pipeline failures and pass the appropriate response to docker as an exit code.
    – BMitch
    May 4, 2022 at 16:07
  • Actually I resolved using SHELL bash before the entrypoint and then exit ${PIPESTATUS[0]}
    – tuxErrante
    May 5, 2022 at 19:05
2

Checking the value of $? is not needed if you just want to act upon the exit status of the previous command.

if docker run container/myContainer:latest; then
  do_stuff
fi

The above example will run/execute do_stuff if the exit status of docker run is zero which is a success.

You can add an else and elif clause in that

Or if you want to negate the exit status of the command.

if ! docker run container/myContainer:latest; then
  do_stuff
fi

The above example will run do_stuff if the exit status of docker run is anything but zero, e.g. 1 and going up, since the ! negates.

If the command has some output and if does not have a silent/quite flag/option you can redirect it to /dev/null

if docker run container/myContainer:latest >/dev/null; then
  do_stuff
fi
  • Should not output anything to stdout
  • see help test | grep -- '^[[:blank:]]*!'
  • In some cases if some output is still showing then that might be stderr which you can silent with >/dev/null 2>&1 instead of just >/dev/null
2
  • will that be the exit code/output of docker run, or of the script that my container runs as its entrypoint? Mar 10, 2020 at 22:59
  • 2
    I can't give you an answer because I don't use docker, but as far as the shell is concern 0 is a success and the rest are a failure.
    – Jetchisel
    Mar 10, 2020 at 23:03

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.