How to detect if docker run succeeded programmatically?

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.

Leave a Comment