2

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

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
  • 1
    Containers don't usually run "services"; to a first approximation, commands like `service` don't work in Docker at all. Do you have an example of something that you want to monitor, where "the container" could be alive but "the service" isn't? – David Maze May 27 '21 at 21:44
  • Please define what you mean exactly by "check", as in "check the services in docker-compose"? What research did you do? like doing `docker-compose --help` and reading the ouptut - didn't any of the commands interest you? – KamilCuk Nov 02 '21 at 16:25

3 Answers3

3

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".

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
2

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.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
0

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
ugate
  • 81
  • 2