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.

I have two files one for provider and one for main to run docker image.But its not working.it shows access denied even i logged into docker form console.I was trying to pull the image from docker hub.can you tell me what will be the issue and how can i resolve this?

provider.tf


terraform {
required_providers {
docker = {
source = “kreuzwerker/docker”
version = “~> 2.22.0”
}
}
}

provider “docker” {}

main.tf


resource “docker_image” “linuxserver-sickchill” {
name = “linuxserver-sickchill:latest”
keep_locally = false
}

resource “docker_container” “linuxserver-sickchill” {
image = docker_image.linuxserver-sickchill.latest
name = “tutorial”
ports {
internal = 8081
external = 8081
}
}