Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support docker container healthy state #467

Merged
merged 1 commit into from
Nov 2, 2022
Merged

feat: support docker container healthy state #467

merged 1 commit into from
Nov 2, 2022

Conversation

bhuisgen
Copy link
Contributor

@bhuisgen bhuisgen commented Oct 2, 2022

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]

Copy link
Contributor

@Junkern Junkern left a comment

Choose a reason for hiding this comment

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

Great contribution, thanks a lot!

@@ -64,6 +64,20 @@ func resourceDockerContainer() *schema.Resource {
Optional: true,
},

"wait": {
Type: schema.TypeBool,
Description: "If `true`, then the Docker container is waited for being healthy state after creation. If `false`, then the container health state is not checked. Defaults to `false`.",
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe mention, that even with false, the container will be spun up, but terraform exists without waiting.

internal/provider/resource_docker_container_funcs.go Outdated Show resolved Hide resolved
@Junkern
Copy link
Contributor

Junkern commented Oct 17, 2022

Could you also update the documentation?

@Junkern
Copy link
Contributor

Junkern commented Oct 21, 2022

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

@Junkern
Copy link
Contributor

Junkern commented Oct 27, 2022

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 Junkern merged commit e1cf415 into kreuzwerker:master Nov 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants