harrier
harrier

Reputation: 1

How to mount file into docker container using terraform?

I'm trying to create with terraform docker container with nginx. I want to mount custom default.conf into /etc/nginx/conf.d/default.conf. I spend some time to read documentation, and examples, but it does not work for me.

I probably missed something, can't figure out what... could You give me a clue? It would me marvellous. :)

Below is my main.tf file:

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

provider "docker" {}

data "docker_registry_image" "nginx" {
  name = "nginx:1.25.2-alpine"
}

resource "docker_image" "nginx" {
  name          = data.docker_registry_image.nginx.name
  pull_triggers = [data.docker_registry_image.nginx.sha256_digest]
}

resource "docker_container" "test_nginx" {
  name  = "frontend"
  image = docker_image.nginx.image_id

  volumes {
    container_path = "/etc/nginx/conf.d/default.conf"
    host_path      = "/tmp/nginx_conf.d/default.conf"
  }

  ports {
    internal = 80
  }
}

When I try to apply this code, I get this:

data.docker_registry_image.nginx: Reading...
data.docker_registry_image.nginx: Read complete after 2s [id=sha256:4c93a3bd8bf95412889dd84213570102176b6052d88bb828eaf449c56aca55ef]

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.test_nginx will be created
  + resource "docker_container" "test_nginx" {
      + attach                                      = false
      + bridge                                      = (known after apply)
      + command                                     = (known after apply)
      + container_logs                              = (known after apply)
      + container_read_refresh_timeout_milliseconds = 15000
      + entrypoint                                  = (known after apply)
      + env                                         = (known after apply)
      + exit_code                                   = (known after apply)
      + hostname                                    = (known after apply)
      + id                                          = (known after apply)
      + image                                       = (known after apply)
      + init                                        = (known after apply)
      + ipc_mode                                    = (known after apply)
      + log_driver                                  = (known after apply)
      + logs                                        = false
      + must_run                                    = true
      + name                                        = "frontend"
      + network_data                                = (known after apply)
      + read_only                                   = false
      + remove_volumes                              = true
      + restart                                     = "no"
      + rm                                          = false
      + runtime                                     = (known after apply)
      + security_opts                               = (known after apply)
      + shm_size                                    = (known after apply)
      + start                                       = true
      + stdin_open                                  = false
      + stop_signal                                 = (known after apply)
      + stop_timeout                                = (known after apply)
      + tty                                         = false
      + wait                                        = false
      + wait_timeout                                = 60

      + ports {
          + external = (known after apply)
          + internal = 80
          + ip       = "0.0.0.0"
          + protocol = "tcp"
        }

      + volumes {
          + container_path = "/etc/nginx/conf.d/default.conf"
          + host_path      = "/tmp/nginx_conf.d/default.conf"
        }
    }

  # docker_image.nginx will be created
  + resource "docker_image" "nginx" {
      + id            = (known after apply)
      + image_id      = (known after apply)
      + name          = "nginx:1.25.2-alpine"
      + pull_triggers = [
          + "sha256:4c93a3bd8bf95412889dd84213570102176b6052d88bb828eaf449c56aca55ef",
        ]
      + 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.nginx: Creating...
docker_image.nginx: Creation complete after 7s [id=sha256:d571254277f6a0ba9d0c4a08f29b94476dcd4a95275bd484ece060ee4ff847e4nginx:1.25.2-alpine]
docker_container.test_nginx: Creating...
╷
│ Error: container exited immediately
│ 
│   with docker_container.test_nginx,
│   on main.tf line 21, in resource "docker_container" "test_nginx":
│   21: resource "docker_container" "test_nginx" {
│ 
╵

I tried also mount /tmp/nginx_conf.d into /etc/nginx/conf.d in container, with same result.

My terraform version:

Terraform v1.6.0
on linux_amd64
+ provider registry.terraform.io/kreuzwerker/docker v3.0.2

Upvotes: 0

Views: 953

Answers (1)

harrier
harrier

Reputation: 1

I found the problem by myself. When I tried to run docker container outside terraform, it turns out that I had a error in nginx configuration, which I was trying to mount into container.

Upvotes: -1

Related Questions