Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if docker run succeeded programmatically?

Tags:

bash

docker

I'm writing a very simple bash script to quickly check that my container still builds and starts correctly and that the app inside responds to requests.

Sometimes docker run fails, e.g. because the port I'm trying to bind the container to is already allocated. But when this happens docker run's exit code is still 0 so I can't use the exit code. How can I check programmatically that the container got started correctly?

The solutions I'm considering are:

  • parse the output for errors
  • docker ps to see if the container is running

but these both seem a little overkill and ugly. Am I missing a better way to check whether docker run succeeded?

like image 557
Jules Olléon Avatar asked Jul 03 '14 03:07

Jules Olléon


People also ask

How do I know if my docker is successful?

If the process in question behaves the usual way then you could simply check for the exit code. If it emits an exit code of 0 even for failed cases, try to figure if it's a bug. If the program returns an exit code of 0 in whatever case, then you're probably left with no choice but to parse the output.

How do you check whether the docker container is running or not?

The status of individual containers is accessed via the docker ps command. This emits a table containing the details of all currently running containers. Now the output will be filtered to show the container you've selected. There'll be no records if the container isn't running.

How can I tell if a docker container is running by name?

docker ps -q -f name=ContainerName is then a simple list of running containers with the name ContainerName. docker ps -qa -f would include exited containers as well so the logic must be to check -a (there, running or not), then without -a to see if it's not only there, but running (and needs to be stopped first).

What does v flag do in docker?

The -v (or --volume ) argument to docker run is for creating storage space inside a container that is separate from the rest of the container filesystem. There are two forms of the command. When given a single argument, like -v /var/lib/mysql , this allocates space from Docker and mounts it at the given location.


2 Answers

As suggested by Abel Muiño in comments, this may have been fixed in more recent Docker versions (I'm currently running 0.9.1).

But, if you're temporarily stuck like me with an older version, I did find a decent workaround to check if the container started by using docker inspect.

docker inspect returns a JSON object with a lot of info about the container, and in particular whether the container is currently running or not. The -f flag lets you easily extract the bits needed:

docker inspect -f {{.State.Running}} $CONTAINER_ID 

or

docker inspect -f "{{.State.Running}}" $CONTAINER_ID 

will return true or false.

Note that you probably want to sleep 1 (or more) between starting the container and checking if it is up. If there's something wrong with your setup it's possible that it would appear as 'running' for a very short time before actually exiting.

like image 113
Jules Olléon Avatar answered Oct 11 '22 05:10

Jules Olléon


To avoid parsing anything, you could use docker top, which returns 1 if the container is not running:

id=$(docker run mycontainer) if ! docker top $id &>/dev/null then     echo "Container crashed unexpectedly..."     return 1 fi 
like image 27
pedroapero Avatar answered Oct 11 '22 04:10

pedroapero