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:
if [ -z `docker ps -q --no-trunc | grep $(docker-compose ps -q <service_name>)` ]; then
echo "No, it's not running."
else
echo "Yes, it's running."
fi
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.
if [ -z `docker-compose ps -q <service_name>` ] || [ -z `docker ps -q --no-trunc | grep $(docker-compose ps -q <service_name>)` ]; then
echo "No, it's not running."
else
echo "Yes, it's running."
fi