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).
Docker exec is a command that allows the execution of any given command within a Docker container. This means it will interpret the arguments passed to it as commands to be run inside the container.
Docker Run vs Docker Exec! This is a fairly common question – but has a simple answer! In short, docker run is the command you use to create a new container from an image, whilst docker exec lets you run commands on an already running container!
Docker exec always returns 0 - ignores exit code of process #5692.
docker exec You can leave the container with exit or ctrl-D .
I found this to be working quite well:
docker exec -t -i my-container sh -c 'my-command; exit $?'
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With