GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add support for user-defined healthchecks by talex5 · Pull Request #22719 · moby/moby

Conversation

@talex5

@talex5 talex5 commented May 13, 2016

Copy link
Copy Markdown
Contributor

This PR adds support for user-defined health-check probes for Docker containers. It adds a HEALTHCHECK instruction to the Dockerfile syntax plus some corresponding "docker run" options. It can be used with a restart policy to automatically restart a container if the check fails.

The HEALTHCHECK instruction has two forms (more may be added later, e.g. HTTP):

  • HEALTHCHECK [OPTIONS] CMD command (check container health by running a command inside the container)
  • HEALTHCHECK NONE (disable any healthcheck inherited from the base image)

The HEALTHCHECK instruction tells Docker how to test a container to check that it is still working. This can detect cases such as a web server that is stuck in an infinite loop and unable to handle new connections, even though the server process is still running.

When a container has a healthcheck specified, it has a health status in addition to its normal status. This status is initially starting. When a health check passes, it becomes healthy. After a certain number of failures, it becomes unhealthy.

The options that can appear before CMD are:

  • --interval=DURATION (default: 30s)
  • --timeout=DURATION (default: 30s)
  • --grace=DURATION (default: 30s)
  • --retries=N (default: 1)
  • --exit-on-unhealthy=X (default: true)

The health check will first run interval seconds after the container is started, and then again interval seconds after each previous check completes.

If a single run of the check takes longer than timeout seconds then the check is considered to have failed.

If the health state is starting and a check started within grace seconds of the container's start time fails, the failure is ignored and the health state remains starting.

It takes retries consecutive failures of the health check for the container to be considered unhealthy.

If --exit-on-unhealthy is true then the container will exit as soon as it becomes unhealthy. The container may then be automatically restarted, depending on its restart policy.

For example, to check every five minutes or so that a web-server is able to serve the site's main page within three seconds:

HEALTHCHECK --interval=5m --grace=20s --timeout=3s --exit-on-unhealthy \
  CMD curl -f http://localhost/

(from https://github.com/talex5/docker/blob/healthcheck/docs/reference/builder.md#healthcheck)

The changes to "docker run" are described here:

https://github.com/talex5/docker/blob/healthcheck/docs/reference/run.md#healthcheck

Example

$ docker run --name=test -d \
    --health-cmd='stat /etc/passwd' \
    --health-interval=2s \
    --exit-on-unhealthy=false \
    busybox sleep 1d
$ sleep 2; docker inspect --format='{{.State.Health.Status}}' test
healthy
$ docker exec test rm /etc/passwd
$ sleep 2; docker inspect --format='{{json .State.Health}}' test
{
  "Status":"unhealthy",
  "FailingStreak":1,
  "LastCheckStart":"2016-05-09T11:09:09.673709108Z",
  "LastCheckEnd":"2016-05-09T11:09:09.786146142Z",
  "LastExitCode":1,
  "LastOutput":"stat: can't stat '/etc/passwd': No such file or directory\n"
}

The health status is also displayed in the docker ps output.

Description for the changelog: Add support for user-defined healthchecks

Closes #21142 and #21143.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

elgalu, icecrime, saulshanabrook, mkuzmin, OlivierBarbier, alvaroaleman, bluet, coolljt0725, FrenchBen, dongluochen, and 9 more reacted with thumbs up emoji saulshanabrook, OlivierBarbier, aaronlehmann, kimble, and TheRealTau reacted with hooray emoji bobrik and runcom reacted with confused emoji
All reactions
  • 👍 19 reactions
  • 🎉 5 reactions
  • 😕 2 reactions
@GordonTheTurtle GordonTheTurtle added area/distribution Image Distribution status/0-triage labels May 13, 2016
@thaJeztah thaJeztah added this to the 1.12.0 milestone May 13, 2016
@thaJeztah thaJeztah added status/1-design-review and removed status/0-triage labels May 13, 2016
This was referenced May 13, 2016
Proposal - Application-defined "alive probe" #21142
Closed
Proposal - Image defined probe #21143
Closed
@thaJeztah

thaJeztah commented May 13, 2016

Copy link
Copy Markdown
Member

thanks @talex5!

ping @icecrime 😄

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

cpuguy83
cpuguy83 reviewed May 13, 2016
View reviewed changes
Comment thread docs/reference/builder.md Outdated

@cpuguy83 cpuguy83 May 13, 2016

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These all seem to be runtime configurations that ought not be in the Dockerfile.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@justincormack justincormack May 13, 2016

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does seem a bit problematic. I might test my container on a fast unloaded machine with SSD and then you run it on a slow loaded machine with a hard drive and the startup time is much longer so it fails the health check (eg mysql seems to take a long time to start serving I noticed recently). It does seem odd to have values that are related to runtime performance in the dockerfile.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@stevvooe stevvooe May 13, 2016

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delay?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@cpuguy83 cpuguy83 May 16, 2016

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'm really a hard -1 on these flags in the Dockerfile.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

dnephin reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@cpuguy83

cpuguy83 commented May 13, 2016

Copy link
Copy Markdown
Member

There doesn't seem to be anything documented on the protocol between docker and the probe.
I imagine this is just exit statuses, but would be good to document.

Do we want this to be a simple 0 == OK, not zero == bad or more robust ala nagios-style checks (ok, warning, critical, and status messages for reporting to the user)?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@cpuguy83

cpuguy83 commented May 13, 2016

Copy link
Copy Markdown
Member

Images also often define the ports they listening on (EXPOSE), would we want to provide a flag that runs some default check on these ports (like open a TCP conn)? This would be similar to how -P automatically creates port forwards for all EXPOSE'd ports.

jdavisclark reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

crosbymichael
crosbymichael reviewed May 13, 2016
View reviewed changes
Comment thread daemon/health.go Outdated

@crosbymichael crosbymichael May 13, 2016

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In go, your second return value should be error here and not return strings

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@talex5 talex5 May 16, 2016

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error here is normally the error string returned by the probe rather than a Go error, although there are a few places where I handle the (unlikely) case of a Go error starting the probe by pretending that the error came from the probe itself. If we return these errors as a third return value, we'll just turn it into a string at the next opportunity anyway.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@stevvooe stevvooe May 16, 2016

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@talex5 Errors in Go are just values. You can add the error content to the error that makes sense for the application. I would expect these to build on exec.ExitError or define a special error type to encapsulate the error. Use a stringly-typed approach is just calling for bugs.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@crosbymichael crosbymichael May 16, 2016

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya, this return value should be an error 100%. Even if it is just a string you can use errors.New("my string") or fmt.Errorf("my %s", "string") but you should not return string especially for how you are using it.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@icecrime icecrime added status/2-code-review and removed area/distribution Image Distribution status/1-design-review labels May 13, 2016
@aluzzardi

aluzzardi commented May 31, 2016

Copy link
Copy Markdown
Member

You can have a []byte but it gets base64 encoded by the marshaler.

That's true in Go but it's very language specific.

What I meant by "you can't have a []byte there" is, at the end, that field is going to be a string no matter what. Whether we base64 encode or not is another question.

How do we currently encode output for other commands (e.g. logs)?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@stevvooe

stevvooe commented May 31, 2016

Copy link
Copy Markdown
Contributor

What if we did Raw with []byte and Output with string?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@thaJeztah

thaJeztah commented May 31, 2016

Copy link
Copy Markdown
Member

What if we did Raw with []byte and Output with string?

SGTM, would give both options; would Raw be limited in size?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@thaJeztah

thaJeztah commented Jun 1, 2016

Copy link
Copy Markdown
Member

ping @aluzzardi @crosbymichael WDYT on @stevvooe's proposal?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Thomas Leonard
Remove the arbitrary output limitation of 255 bytes
33ffb3a
Use 4096 instead.

Signed-off-by: Thomas Leonard <[email protected]>
@talex5

talex5 commented Jun 1, 2016 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

I'm not sure what you mean about having both. Would we include the same data twice in each message, or somehow know where to send which data?

Another possibility: if a probe wants to generate binary data then it base64 encodes it itself. That way, probes that only want text don't need to do anything and the messages are human-readable. Probes that return binary data would need something at the client end to interpret it anyway, so decoding it wouldn't be much extra work. The data on the wire would be the same as if we'd used []byte in that case.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@stevvooe

stevvooe commented Jun 1, 2016

Copy link
Copy Markdown
Contributor

@talex5 Yes, have both. One that is "human-readable", and hopefully properly sanitized, and one that is the raw, unprocessed output.

This will meet both the use cases of sending binary data and debugging problems with health check output during development. It is unreasonable to make users go through so much work just to get unprocessed command output.

Let's make sure this feature isn't limited to our imaginations but, instead, enables others'.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@cpuguy83

cpuguy83 commented Jun 2, 2016

Copy link
Copy Markdown
Member

I do not see the purpose of supporting more than just simple output from a healthcheck, and certainly not storing both binary and human-readable....

I think we should support only something very simple for a human to use as debug info, or potentially remove support for capturing output from the healthcheck from this PR and implement separately.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

cpuguy83
cpuguy83 reviewed Jun 2, 2016
View reviewed changes
Comment thread docs/reference/builder.md

- 0: success - the container is healthy and ready for use
- 1: unhealthy - the container is not working correctly
- 2: starting - the container is not ready for use yet, but is working correctly

@cpuguy83 cpuguy83 Jun 2, 2016

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this state really just any failure before the first success?
I would recommend removing it.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@talex5 talex5 Jun 2, 2016

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this, whatever is monitoring the status will need some timeout to decide that the container has failed to start. @justincormack (I think) pointed out that a simple timeout is not enough to cover e.g. a database that is busy performing recovery actions. So the idea of this state is to allow the container to tell the monitor that it needs more time, and avoid the monitor having to guess what the grace period should be.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

dannc reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@dannc dannc Jun 2, 2016 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A nit about the labels/description attached to the return codes. Some containers are "run once and die" in that they produce some artifact and exit. Take for example a container that generates a TLS certificate and writes it to a volume, which other containers subsequently mount in order to use the certificate.

I would propose something like:
0: succeeded - the container is healthy or has exited successfully
1: failed - the container is unhealthy or has exited unsuccessfully
2: starting (in-progress?) - the container is not ready for use yet or has not completed its task

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@crosbymichael

crosbymichael commented Jun 2, 2016

Copy link
Copy Markdown
Contributor

For the output, in this PR we should keep it as is. The size has already been increased to 4096 which is much better than what it was before. Most commands that do health checks should be encouraged to write a simple reason why they are reporting as they did. "I returned unhealthy BECAUSE i could not connect to the database." We don't want to encourage checks to do a yolo operation and puke up some stacktrace to decipher.

println("could not connect to database");
return 1;

In the future after this has some user and they are wanting some type of more structured response than a limited string it would be effortless to add an unbounded []byte array to the output for this. It is something that is safe to defer until we gain feedback.

For the three states, simple health checks don't have to use the starting state if they don't want to. To the normal developer they can just return 1 or 0. If the app has a long startup or some type of complexity on boot then it can take advantage of this state and we don't have to infer any type of guesses that we would otherwise be doing. Lets to write software that guesses things, let the things that know the state tell us. You are not forced to use this state if you don't want to.

After looking at the code again, I think all these things are already implemented.

LGTM

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@thaJeztah

thaJeztah commented Jun 2, 2016

Copy link
Copy Markdown
Member

ok, enough bike-shedding. Let's go for it LGTM

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

dongluochen
dongluochen reviewed Jun 2, 2016
View reviewed changes
Comment thread builder/dockerfile/parser/testfiles/health/Dockerfile
ADD check.sh main.sh /app/
CMD /app/main.sh
HEALTHCHECK
HEALTHCHECK --interval=5s --timeout=3s --retries=1 \

@dongluochen dongluochen Jun 2, 2016

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think a good practice is multiple failures like 3 to tolerate random failures. Giving an example of --retries=1 might not be appropriate.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@dongluochen dongluochen Jun 2, 2016

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest we set default retries to 3.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@thaJeztah thaJeztah mentioned this pull request Jun 2, 2016
[Carry 22719] healthcheck feature #23218
Merged
@dongluochen

dongluochen commented Jun 2, 2016

Copy link
Copy Markdown
Contributor

PR looks good to me.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@thaJeztah

thaJeztah commented Jun 2, 2016

Copy link
Copy Markdown
Member

@dongluochen I carried the PR, rebased/squashed and with vendor update in #23218. Let me know if you need those addressed in the PR, otherwise I'll do a follow-up

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@dongluochen

dongluochen commented Jun 2, 2016

Copy link
Copy Markdown
Contributor

Thanks @thaJeztah. Follow-up is good.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@crosbymichael crosbymichael closed this in #23218 Jun 2, 2016
crosbymichael added a commit that referenced this pull request Jun 2, 2016
@crosbymichael
Add User defined Healthchecks for Containers
ce255f7
Carry of #22719
@thaJeztah thaJeztah mentioned this pull request Jun 3, 2016
Healthcheck: set default retries to 3 #23232
Merged
@nishanttotla nishanttotla mentioned this pull request Jun 10, 2016
Healthcheck Support moby/swarmkit#641
Closed
@thaJeztah thaJeztah mentioned this pull request Jun 10, 2016
remove unused defaultExitOnUnhealthy constant #23442
Merged
@augi augi mentioned this pull request Jul 8, 2016
Check health state of containers Closes #31 avast/gradle-docker-compose-plugin#33
Merged
@thekid thekid mentioned this pull request Oct 23, 2016
More booted checks tueftler/boot#2
Open
@talex5 talex5 mentioned this pull request Nov 15, 2016
trigger restart from unhealthy status #28400
Open
@willfarrell willfarrell mentioned this pull request Feb 13, 2017
Add support for unhealthy status mcasimir/docker-autoheal#3
Open
@mageddo

mageddo commented Jul 26, 2017

Copy link
Copy Markdown

Guys what about the restart policy? I think that is very important, the reason was explained here. So will this feature be implemented?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@stevvooe

stevvooe commented Jul 26, 2017

Copy link
Copy Markdown
Contributor

@mageddo This is a long closed pull request, carried elsewhere. If you have questions or a feature request, please file a new issue.

thaJeztah reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@thaJeztah

thaJeztah commented Jul 26, 2017

Copy link
Copy Markdown
Member

Locking the conversation in this issue for the reason @stevvoe mentioned above; comments on closed issues and PRs easily go unnoticed - I'm locking the conversation to prevent that from happening

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@moby moby locked and limited conversation to collaborators Jul 26, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

status/needs-attention Calls for a collective discussion during a review session status/2-code-review

Projects

None yet

Milestone

1.12.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

16 participants

@talex5 @thaJeztah @cpuguy83 @tianon @stevvooe @aluzzardi @crosbymichael @dongluochen @dannc @mageddo @justincormack @jessfraz @HackToday @aaronlehmann @icecrime @GordonTheTurtle
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.