The result of the docker exec command

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

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

I want to continue running the script only if the nginx configuration check succeeds and not fail.

I tried to use $? , but it is 0 , even then configuretest output does not work (since docker exec succeeds, as I understand it).

+6
source share
3 answers

Translation of my comment in response. This should work:

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

This works for me.

+5
source

api/client/exec.go#L97-L100 gets the exit code:

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

This 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 team failed, you will get an exit code.
Although as mentioned here, you can use nginx -t instead of configtest .

+3
source

I found this one to work well enough:

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

All Articles