Akshay khairnar
Akshay khairnar

Reputation: 21

Check docker-compose services are running or not

I am writing a script which will check docker service but I want check services which are inside the docker-compose without getting into it. For ex: we have custom services like tass and inception basically we check it's status by this command "service tass status"

Is there any way to check this services in Docker-compose

Upvotes: 2

Views: 4761

Answers (3)

Antonio Petricca
Antonio Petricca

Reputation: 10996

Docker compose is only a tool to build docker images.

You should rely on docker commands in order to check each service health, for example:

  • docker ps
  • docker stat
  • docker inspect
  • docker container ls

In this How to check if the docker engine and a docker container are running? thread you can find a lot of alternatives about container checking.

Upvotes: 1

ugate
ugate

Reputation: 101

Checking for .State.Status, .State.Running, etc. will tell you if it's running, but it's better to ensure that the health of your containers. Below is a script that you can run that will make sure that two containers are in good health before executing a command in the 2nd container. It prints out the docker logs if the wait time/attempts threshold has been reached.

Example taken from npm sql-mdb.


    #!/bin/bash
    # Wait for two docker healthchecks to be in a "healthy" state before executing a "docker exec -it $2 bash $3"
    ##############################################################################################################################
    # $1 Docker container name that will wait for a "healthy" healthcheck (required)
    # $2 Docker container name that will wait for a "healthy" healthcheck and will be used to run the execution command (required)
    # $3 The actual execution command that will be ran (required)
    attempt=0
    health1=checking
    health2=checking
    while [ $attempt -le 79 ]; do
      attempt=$(( $attempt + 1 ))
      echo "Waiting for docker healthcheck on services $1 ($health1) and $2 ($health2): attempt: $attempt..."
      if [[ health1 != "healthy" ]]; then
        health1=$(docker inspect -f {{.State.Health.Status}} $1)
      fi
      if [[ $health2 != "healthy" ]]; then
        health2=$(docker inspect -f {{.State.Health.Status}} $2)
      fi
      if [[ $health1 == "healthy" && $health2 == "healthy"  ]]; then
        echo "Docker healthcheck on services $1 ($health1) and $2 ($health2) - executing: $3"
        docker exec -it $2 bash -c "$3"
        [[ $? != 0 ]] && { echo "Failed to execute \"$3\" in docker container \"$2\"" >&2; exit 1; }
        break
      fi
      sleep 2
    done
    if [[ $health1 != "healthy" || $health2 != "healthy"  ]]; then
      echo "Failed to wait for docker healthcheck on services $1 ($health1) and $2 ($health2) after $attempt attempts"
      docker logs --details $1
      docker logs --details $2
      exit 1
    fi

Upvotes: 0

KamilCuk
KamilCuk

Reputation: 140840

You can use:

docker-compose ps <name>
docker-compose top <name>
docker-compose logs <name>

To "check" the service with name <name>. You can learn many more commands by doing `docker-compose --help.

Finally, you can run docker-compose exec <name> <shell> and get an interactive shell inside the cont inaner, which with some unix-utilities will allow you to "check" the container further with ease.

Finally, you can extract the name of the running container as in How do I retrieve the exact container name started by docker-compose.yml , and use any of the docker commands mentioned in the other answer to "check". From docker inspect <the container name> you can get the cgroup name and mounted filesystem, which you can "check".

Upvotes: 4

Related Questions