Skip to content

docs(r/container): fix the example to keep container running #363

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

suzuki-shunsuke
Copy link
Contributor

@suzuki-shunsuke suzuki-shunsuke commented Mar 24, 2022

Problem to solve

When you apply the example usage of docker_container, the container exists soon and the apply fails.

https://registry.terraform.io/providers/kreuzwerker/docker/latest/docs/resources/container#example-usage

So you have to fix the example usage.

How to reproduce

$ terraform version
Terraform v1.1.7
on darwin_amd64

main.tf

terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "2.16.0"
    }
  }
}

provider "docker" {}

resource "docker_container" "ubuntu" {
  name    = "foo"
  image   = docker_image.ubuntu.latest
}

resource "docker_image" "ubuntu" {
  name = "ubuntu:precise"
}
$ terraform apply

Then the following error occurs.

docker_image.ubuntu: Creating...
docker_image.ubuntu: Still creating... [10s elapsed]
docker_image.ubuntu: Creation complete after 12s [id=sha256:5b117edd0b767986092e9f721ba2364951b0a271f53f1f41aff9dd1861c2d4feubuntu:precise]
docker_container.ubuntu: Creating...
╷
│ Error: container exited immediately
│ 
│   with docker_container.ubuntu,
│   on main.tf line 12, in resource "docker_container" "ubuntu":
│   12: resource "docker_container" "ubuntu" {
│ 
╵
$ terraform apply

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # docker_container.ubuntu will be created
  + resource "docker_container" "ubuntu" {
      + attach           = false
      + bridge           = (known after apply)
      + command          = (known after apply)
      + container_logs   = (known after apply)
      + entrypoint       = (known after apply)
      + env              = (known after apply)
      + exit_code        = (known after apply)
      + gateway          = (known after apply)
      + hostname         = (known after apply)
      + id               = (known after apply)
      + image            = (known after apply)
      + init             = (known after apply)
      + ip_address       = (known after apply)
      + ip_prefix_length = (known after apply)
      + ipc_mode         = (known after apply)
      + log_driver       = (known after apply)
      + logs             = false
      + must_run         = true
      + name             = "foo"
      + network_data     = (known after apply)
      + read_only        = false
      + remove_volumes   = true
      + restart          = "no"
      + rm               = false
      + security_opts    = (known after apply)
      + shm_size         = (known after apply)
      + start            = true
      + stdin_open       = false
      + tty              = false

      + healthcheck {
          + interval     = (known after apply)
          + retries      = (known after apply)
          + start_period = (known after apply)
          + test         = (known after apply)
          + timeout      = (known after apply)
        }

      + labels {
          + label = (known after apply)
          + value = (known after apply)
        }
    }

  # docker_image.ubuntu will be created
  + resource "docker_image" "ubuntu" {
      + id          = (known after apply)
      + latest      = (known after apply)
      + name        = "ubuntu:precise"
      + output      = (known after apply)
      + repo_digest = (known after apply)
    }

Plan: 2 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

docker_image.ubuntu: Creating...
docker_image.ubuntu: Still creating... [10s elapsed]
docker_image.ubuntu: Creation complete after 12s [id=sha256:5b117edd0b767986092e9f721ba2364951b0a271f53f1f41aff9dd1861c2d4feubuntu:precise]
docker_container.ubuntu: Creating...

│ Error: container exited immediately

│   with docker_container.ubuntu,
│   on main.tf line 12, in resource "docker_container" "ubuntu":
│   12: resource "docker_container" "ubuntu" {

How to solve

To fix the above error, you have to fix the example code.
By setting tty, you can fix the error.

Also, you can fix the error by changing command too.

e.g.

resource "docker_container" "ubuntu" {
  name  = "foo"
  image = docker_image.ubuntu.latest
  command = ["tail", "-f", "/dev/null"]
}

Reference

@suzuki-shunsuke suzuki-shunsuke marked this pull request as ready for review March 24, 2022 00:16
@suzuki-shunsuke suzuki-shunsuke requested a review from mavogel March 24, 2022 00:16
@suzuki-shunsuke suzuki-shunsuke added documentation Improvements or additions to documentation r/container Relates to the container resource labels Mar 24, 2022
@MrBenGriffin
Copy link

This does not resolve the problem - which is that both "must_run" and "start" are ignored despite being (by default) set to true - and even when explicitly set to true.

Meanwhile the command/tty lines are documented to do other things altogether, and this 'fix' is (AFAIK) effectively engineering a side effect of tty/command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation r/container Relates to the container resource
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants