HiveBrain v1.2.0
Get Started
← Back to all entries
patterndockerMinor

Referencing locally built Docker images in Terraform

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
builtreferencingdockerlocallyimagesterraform

Problem

Is it possible to use docker images in Terraform that are not hosted in a registry but in the local Docker storage?

I'd like to have a resource like the following where no pull is performed on the image if it exists locally.

resource "docker_container" "my_service" {
  image = "my_locally_built_image_name"
  name  = "my-service"
}

Solution

Yes, As we write terraform template is correct.
When you use terraform apply it will check local docker image list if it doesn't exit it will go to find registry as you define docker image.

If you don't want a pull image from the internet you can try the below steps. But at least you should have a base docker image of any Linux distribution.

  • Create a Dockerfile.



  • Build Dockerfile as below command



# docker build -t image_name -f Dockerfile

  • Define the docker image as a template



resource "docker_container" "my_service" {
image = "image_name"
name = "my-service"
}


  • By terraform init install terraform module and apply the terraform template.



# terraform init

# terraform apply -auto-approve

Context

StackExchange DevOps Q#11331, answer score: 3

Revisions (0)

No revisions yet.