terraform-provider-docker icon indicating copy to clipboard operation
terraform-provider-docker copied to clipboard

feat: support docker container healthy state

Open bhuisgen opened this issue 3 years ago • 1 comments

This feature adds the ability to check and wait for the container healthy state at creation.

Two options are added to the docker_containerresource:

  • wait : flag to wait for the healthy state at creation (default false)
  • wait_timeout: the maximal timeout in seconds to wait for (default 60)

The wait option is disabled by default as a healthcheck block is required to successfully create the resource. It works too with the count option but all containers will be started and checked at the same time for the given resource.

Note that the healthy state is only checked at resource creation not afterwards.

Here is an example:

resource "docker_network" "test" {
  name = "test"
}

resource "docker_image" "nginx" {
  name = "bhuisgen/alpine-nginx:prod"
}

resource "docker_container" "test" {
  count = 3

  wait = true
  # wait_timeout = 60

  image       = docker_image.nginx.image_id
  name        = "test_${count.index + 1}"

  healthcheck {
    test         = ["CMD", "/command/s6-svstat", "-u", "/run/service/nginx"]
    interval     = "15s"
    timeout      = "10s"
    start_period = "15s"
    retries      = 3
  }
}
docker_image.nginx: Creating...
docker_network.test: Creating...
docker_image.nginx: Creation complete after 0s [id=sha256:d83818098350e91cda406e064774182d5d10d217e8bf185e9e0fed6c6abc8ef3bhuisgen/alpine-nginx:prod]
docker_network.test: Creation complete after 2s [id=27dfa73218d0e66d91b9ee4ed34351287a57b060931258e9e60a4a7fe75de032]
docker_container.test[1]: Creating...
docker_container.test[2]: Creating...
docker_container.test[0]: Creating...
docker_container.test[0]: Still creating... [10s elapsed]
docker_container.test[2]: Still creating... [10s elapsed]
docker_container.test[1]: Still creating... [10s elapsed]
docker_container.test[2]: Creation complete after 16s [id=97b821b3dd6a318dc29863305a531eb5c5092c14da16ca8649bdd05e37a88b35]
docker_container.test[1]: Creation complete after 16s [id=f14291b8970041d7575e230f7441eb450df0e6b12429894f1ab729391f2fc7fc]
docker_container.test[0]: Creation complete after 17s [id=42cc700e10246a185f310c781e5627cdd0ff325ffdd7349e5d7a77e59be958fb]

bhuisgen avatar Oct 02 '22 10:10 bhuisgen

Could you also update the documentation?

Junkern avatar Oct 17 '22 06:10 Junkern

@bhuisgen could you rebase and update the documentation? Thanks!

Junkern avatar Oct 21 '22 14:10 Junkern

The failing tests of TestAccDockerImage are not your fault, they are flaky and I wanted to fix them since a long time. Please fix the documentation. You do not have to change anything in .md files. Revert your changes to the .md files and execute the steps of https://github.com/kreuzwerker/terraform-provider-docker/blob/master/CONTRIBUTING.md#update-the-documentation

This will automatically update all the documentation and then then Docs and Website Lint / website-generation (pull_request) will also pass.

Junkern avatar Oct 27 '22 14:10 Junkern