I am using the docker-compose
.
Some commands like up -d service_name
or start service_name
are returning right away and this is pretty useful if you don't want the containers running to depend on the state of the shell, like they do with regular up service_name
. The one use-case is running it from some kind of continious integration/delivery server.
But this way of running/starting services does not provide any feedback about the actual state of the service afterwards.
The Docker Compose CLI reference for up
command does mention the relevant option, but, as for version 1.7.1
, it is mutually exclusive with -d
:
--abort-on-container-exit Stops all containers if any container was stopped. *Incompatible with -d.*
docker-compose ps -q <service_name>
will display the container ID no matter it's running or not, as long as it was created.docker ps
shows only those that are actually running.Let's combine these two commands:
docker ps
shows short version of IDs by default, so we need to specify--no-trunc
flag.UPDATE: It threw "grep usage" warning if the service was not running. Thanks to @Dzhuneyt, here's the updated answer.
As for version
1.7.1
, there are no such commands built-in.Instead, the
exec
can be used in similar way.When you run it for the service which has some containers up it will run ok:
But when you run it for the service which has no running service containers, it will show an error:
So, it can be used in order to check, is there any "alive" containers for given service.
To see all services running:
To see if your-service is running:
Note that
--filter
must be used with--services
for some foreign reason.You can run:
And you will get the id of the container if
service-name
is running. Something like:If the service is not running the output is empty, so if you want to use this in a script you can do something like:
I had a similar need. However, I have a
restart: always
in my environment. So it can be a bit tricky to detect if something is crashing and restarting in a loop.I made an Icinga/Nagios check to also compare the created and start times. Maybe it's useful to someone else down the line:
How about this?
you list the processes, select the lines where "Up" is in column 4 and filter through for a match on the service name.
Here is a simple one liner that returns the current status of the service:
This is returning only the status of docker container. If you want to check for the actual state of your application you should add HEALTHCHECK to your Dockerfile (https://docs.docker.com/engine/reference/builder/#healthcheck). Afterwards you can inspect it with:
If you assume this scenario:
you can check if there is any stopped container due to an error with:
docker ps -a | grep 'Exited (255)'
.This check works correctly even in case of containers which are expected to stop immediately with no error (i.e. data containers), as their status (from
docker ps -a
) is marked asExited (0)
.For example, in our docker-compose.yml, we start our containers with:
command: sh -c 'node dotenv_check.js && pm2 start --no-daemon src/worker.js --watch'
For php-fpm, we use a similar command:
The
dotenv_check.js
anddotenv_check.php
are scripts which exit with an error code in case a required env variable is missing.The
set -e
command, tells the script to stop on error, which, in turns, will immediately stop the container. About set-eHere's a simplified one-liner based on almquista's answer:
We use grep's
-q
flag so a non-zero exit code indicates the service is not running. For example:You can grep for
(healthy)
or/and(unhealthy)
images to act properly.In this example, i'm probing docker-compose each 5 seconds for running service with
(healthy)
status. If script will find such service, it will break execution. If script will exceed 300 seconds, it will exit with error code.