Skip to content

Running containers report unhealthy after docker stop #36233

@adw1n

Description

@adw1n

Description
Immediately after I try to stop a container that has a healthcheck using docker stop container_name, that container reports unhealthy while it still running. Is it the intended behavior? Could we document it here: https://docs.docker.com/engine/reference/builder/#healthcheck?

Steps to reproduce the issue:

  1. use docker stop on a container which:
  • needs some time to stop
  • has a healthcheck
  1. run docker inspect --format "{{json .State }}" container_name | python3 -m json.tool

Minimal example
Dockerfile

FROM python

HEALTHCHECK --interval=30s --timeout=3s --retries=3 CMD ["curl", "-f", "http://localhost:8000/"]

COPY ./main.py /root/main.py

CMD ["python3", "-u", "/root/main.py"]

main.py

import http.server
import socketserver
import signal
import time
import sys


def handler(*args):
    print('docker stop')
    time.sleep(5)
    print('exiting...')
    sys.exit(0)

signal.signal(signal.SIGTERM, handler)
signal.signal(signal.SIGINT, handler)

PORT = 8000

Handler = http.server.SimpleHTTPRequestHandler

with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print("serving at port", PORT)
    httpd.serve_forever()

Run:

docker build -t unhealthy_issue .
docker run --name unhealthy_issue -it unhealthy_issue

watch -n 0.1 'docker inspect --format "{{json .State }}" unhealthy_issue | python3 -m json.tool'

time docker stop unhealthy_issue # look at how health status changes in the other terminal window where you have the 'docker inspect' command running

Describe the results you received:

{
  "Status": "running",
  "Running": true,
  "Paused": false,
  "Restarting": false,
  "OOMKilled": false,
  "Dead": false,
  "Pid": 4356,
  "ExitCode": 0,
  "Error": "",
  "StartedAt": "2018-02-07T12:03:39.178466103Z",
  "FinishedAt": "0001-01-01T00:00:00Z",
  "Health": {
    "Status": "unhealthy",
    "FailingStreak": 0,
    "Log": [
      {
        "Start": "2018-02-07T13:04:09.178814464+01:00",
        "End": "2018-02-07T13:04:09.253941843+01:00",
        "ExitCode": 0,
        "Output": "irrelevant"
      }
    ]
  }
}

Describe the results you expected:

{
  "Status": "running",
  "Running": true,
  "Paused": false,
  "Restarting": false,
  "OOMKilled": false,
  "Dead": false,
  "Pid": 4356,
  "ExitCode": 0,
  "Error": "",
  "StartedAt": "2018-02-07T12:03:39.178466103Z",
  "FinishedAt": "0001-01-01T00:00:00Z",
  "Health": {
    "Status": "healthy",
    "FailingStreak": 0,
    "Log": [
      {
        "Start": "2018-02-07T13:04:09.178814464+01:00",
        "End": "2018-02-07T13:04:09.253941843+01:00",
        "ExitCode": 0,
        "Output": "irrelevant"
      }
    ]
  }
}

Output of docker version:

Client:
 Version:	17.12.0-ce
 API version:	1.35
 Go version:	go1.9.2
 Git commit:	c97c6d6
 Built:	Wed Dec 27 20:11:19 2017
 OS/Arch:	linux/amd64

Server:
 Engine:
  Version:	17.12.0-ce
  API version:	1.35 (minimum version 1.12)
  Go version:	go1.9.2
  Git commit:	c97c6d6
  Built:	Wed Dec 27 20:09:53 2017
  OS/Arch:	linux/amd64
  Experimental:	false

Output of docker info:

Containers: 2
 Running: 0
 Paused: 0
 Stopped: 2
Images: 166
Server Version: 17.12.0-ce
Storage Driver: overlay2
 Backing Filesystem: extfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 89623f28b87a6004d4b785663257362d1658a729
runc version: b2567b37d7b75eb4cf325b77297b140ea686ce8f
init version: 949e6fa
Security Options:
 seccomp
  Profile: default
Operating System: Ubuntu 16.04.3 LTS
OSType: linux
Architecture: x86_64
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false

WARNING: No swap limit support

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions