Having trouble with docker provider creating containers

I’m trying to build a simple ubuntu container with terraform and can’t seem to get it working. The error is : `Error: Container 73c1affdcf3e9c2769557629b9fe2cef512a62987c8f89e611703c7d362d242b exited after creation, error was:

on 5-node-topology_hosts.tf line 6, in resource “docker_container” “ubuntu”:
6: resource “docker_container” “ubuntu” {`

I have a single .tf file.

provider "docker" { host = "tcp://127.0.0.1:2376" } resource "docker_container" "host1" { image = "${docker_image.ubuntu.latest}" name = "host1" attach = "true" logs = "true" must_run = "true" } data "docker_registry_image" "ubuntu" { name = "ubuntu:18.04" } resource "docker_image" "alpine" { name = "alpine:latest" } resource "docker_image" "ubuntu" { name = "${data.docker_registry_image.ubuntu.name}" pull_triggers = ["${data.docker_registry_image.ubuntu.sha256_digest}"] }

Does anyone have any ideas?

edit: Sorry about the formatting

Hello @rmcmilli,

I wonder if the error is happening because Docker images like Ubuntu have a default command of/bin/bash and then the container exits. As a result, running with the attach = true means that the Terraform Docker provider will wait until the container ends execution and then completely detach, which conflicts with the must_run directive that expects the container to continue running.

When I use an Ubuntu container for docker exec, I usually add a sleep command to keep my container running:

provider "docker" {}

resource "docker_container" "host1" {
  image = "${docker_image.ubuntu.latest}"
  name = "host1"
  attach = false
  must_run = true
  command = ["sleep", "600"]
}

data "docker_registry_image" "ubuntu" {
  name = "ubuntu:18.04"
}

resource "docker_image" "alpine" {
  name = "alpine:latest"
}

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

Depending on what you’re using with your Ubuntu image, you may need to test out if must_run and attach work for your use.