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:
docker ps
to see if the container is runningbut these both seem a little overkill and ugly. Am I missing a better way to check whether docker run
succeeded?
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.
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.
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).
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.
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.
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
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