Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to docker inspect State.Health gives `map has no entry for key "State"`

I'm trying to use a Docker HEALTHCHECK in a container. I'm setting that up in a version 3 docker-compose file, which also supports healthcheck:

version: '3'

services:
  api:
  [...]
    healthcheck:
      test: test ! -e /unhealthy
      interval: 10s
      timeout: 1s
      retries: 1

In every place I can find, including on stackoverflow, the way to check the health of a container is to use docker inspect and look at State.Health, but no such entry exists:

$ docker inspect --format='{{json .State}}' api
Template parsing error: template: :1:7: executing "" at <.State>: map has no entry for key "State"

I googled around for quite a while but cannot see where this information moved to or any alternate ways to check health status.

Version info:

$ docker-compose --version
docker-compose version 1.16.1, build 6d1ac219
$ docker --version
Docker version 17.09.0-ce, build afdb6d4
like image 844
djanderson Avatar asked Nov 22 '17 00:11

djanderson


1 Answers

The service name api is a compose construct. Docker isn't able to map the name api to a container. I suspect you have another object named api that docker inspect it is looking up.

Compose containers are normally named {{parent_directory}}_{{service}}_{{n}}. To find the name use:

docker-compose ps
docker ps -a --filter name=SERVICE_NAME

Then you can use inspect on that name

docker inspect --format='{{json .State}}' CONTAINER_ID_OR_NAME

or specifically inspect containers to avoid non container objects:

docker container inspect --format='{{json .State}}' CONTAINER_ID_OR_NAME

All containers should have the .State object. Only containers with a healthcheck defined will have the .State.Health data.

like image 85
Matt Avatar answered Nov 12 '22 15:11

Matt