'The result of docker exec command

I need to know in my shell script the output of some docker exec commands, for example I have an nginx container and in my script I run:

docker exec -it containerName /etc/init.d/nginx configtest

I want to continue script execution only if nginx config test is success, not when fail.

I've tried out to use $?, but it is 0 even then configtest output is fail (because docker exec is successfully executed, as I understand).



Solution 1:[1]

I found this to be working quite well:

docker exec -t -i my-container sh -c 'my-command; exit $?'

Solution 2:[2]

Translating my comment into an answer. This should work:

docker exec -it mynginx /etc/init.d/nginx configtest && echo "pass" || echo "fail"

It works for me.

Solution 3:[3]

The api/client/exec.go#L97-L100 does get the exit code:

var status int
if _, status, err = getExecExitCode(cli, execID); err != nil {
    return err
}

That comes from api/client/utils.go#L84-L97

// getExecExitCode perform an inspect on the exec command. It returns
// the running state and the exit code.
func getExecExitCode(cli *DockerCli, execID string) (bool, int, error) {
    resp, err := cli.client.ContainerExecInspect(execID)
    if err != nil {
        // If we can't connect, then the daemon probably died.
        if err != lib.ErrConnectionFailed {
            return false, -1, err
        }
        return false, -1, nil
    }

    return resp.Running, resp.ExitCode, nil
}

So if your command fail, you will get the exit code.
Although, as mentioned here, you could use nginx -t instead of configtest.

Solution 4:[4]

If you don't want to immediately respond to exit code returned from a process triggered by docker exec command, you could do something like this.

docker exec my-app-container /app/scripts/test.sh
UNIT_TEST_EXIT_CODE=$? # Get exit code from last command (/app/scripts/test.sh)
... # do some stuff after unit testing 
exit $UNIT_TEST_EXIT_CODE

I used this approach in the context of CI/CD pipelines.

Solution 5:[5]

A new version ready to test to run a bash command:

docker exec -t -i oracle bash -c "echo hola && exit"
hola

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 conradkleinespel
Solution 2 anubhava
Solution 3 VonC
Solution 4 Lukasz Dynowski
Solution 5 eleuteron