Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The result of docker exec command

Tags:

bash

shell

docker

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).

like image 432
Aliance Avatar asked Dec 24 '15 05:12

Aliance


People also ask

What does exec command do in docker?

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.

What is docker run and exec?

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!

Does Docker Exec return exit code?

Docker exec always returns 0 - ignores exit code of process #5692.

How do I get out of Docker exec?

docker exec You can leave the container with exit or ctrl-D .


2 Answers

I found this to be working quite well:

docker exec -t -i my-container sh -c 'my-command; exit $?'
like image 199
conradkleinespel Avatar answered Oct 20 '22 01:10

conradkleinespel


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.

like image 20
anubhava Avatar answered Oct 19 '22 23:10

anubhava