terraform-provider-docker
terraform-provider-docker copied to clipboard
docs(r/container): fix the example to keep container running
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
- https://github.com/kreuzwerker/terraform-provider-docker/issues/362
- https://registry.terraform.io/providers/kreuzwerker/docker/latest/docs/resources/container
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.