0

Is there a way to specify a healthcheck command when creating a new service using the docker-py SDK?

This options is available in the docker cli using the flag '--health-cmd' as well as in the SDK when creating a new container run but I couldn't find a way to specify healthcheck commands for services even when using the low level API.

brunograz
  • 13
  • 5

1 Answers1

0

There is no functionality in the SDK. So open the issue on their git repo and ask them to add the feature. Till that time you can use the below workaround i built

from docker import client
from docker.models.services import CONTAINER_SPEC_KWARGS
from docker.types.services import ContainerSpec

init_spec = ContainerSpec.__init__


def override_init(self, *args, **kwargs):
    self['HealthCheck'] = kwargs.pop("HealthCheck")
    init_spec(self, *args, **kwargs)

ContainerSpec.__init__ = override_init

if 'HealthCheck' not in CONTAINER_SPEC_KWARGS:
    CONTAINER_SPEC_KWARGS.append('HealthCheck')


c = client.from_env()

result = c.services.create(name="nginx", image="nginx",
                           HealthCheck={
                               "Test": ["CMD", "ls", "-alh"],
                               "Interval": 1000000 * 500, # 500 ms
                               "Timeout": 1000000 * 5 * 1000, # 5 seconds
                               "Retries": 3,
                               "StartPeriod": 1000000 * 5 * 1000 # 5 seconds
                           })

print(result)

And output as below

CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                        PORTS               NAMES
f617eaf4d211        nginx:latest        "nginx -g 'daemon ..."   About a minute ago   Up About a minute (healthy)   80/tcp              nginx.1.htgg23ysze00qr0kb790lyxnv
Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
  • Thanks for the information. I just checked the repository and it seems there is a [pull request](https://github.com/docker/docker-py/pull/1733) exactly on this. – brunograz Sep 28 '17 at 12:20