Skip to content

stashconsulting/terraform-docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

terraform-docker

Integration between Terraform and Docker.

This project is intended for people who want to build their docker images from terraform.

Some advise

Now the latest version images will come by default with Terraform v0.13.0 or higher for the correct operation of the command terraform login.

Some previous versions of the images remain available on Docker Hub with their respective tags.

Terraform v0.12.18

Getting Started

Usage

Here is a simple example of main.tf file that build an image named test:latest based on the Dockerfile specification.

locals {
  region  = "my-region"
  zone    = "my-zone"
  project  = var.project
}

# Specify the GCP Provider
provider "google" {
  project  = local.project
  region   = local.region
  zone     = local.zone
}

# local-exec for building the docker image
resource "null_resource" "building_docker_image" {
  triggers = {
    image_id = var.image_id
  }
  provisioner "local-exec" {
    command = <<EOF
      docker build -t test:latest .
    EOF
  }
}

When you use null_resource changing something to the command does not trigger the re-execution if the command worked and the first place. To solve this problem we have to change the identifier or change the image value.

# local-exec for building the docker image
resource "null_resource" "building_docker_image" {
  triggers = {
    image_id = var.image_id
  }
  provisioner "local-exec" {
    command = <<EOF
      docker build -t test:latest .
    EOF
  }
}

Here is a simple example of variables.tf file, you can specify another image value changing the default value or running the following command terraform apply -auto-approve -var image_id=kong_dbless

variable "image_id" {
    type = string
    default = "test"
}

Steps to use the entrypoint version

Only accepts terraform commands

You have to create a Docker volume to store the files that we will use to build the container. Let run init for terraform first.

docker run -it -v $(pwd)/:/workpace -w /workpace stashconsulting/terraform-docker:entrypoint-latest init

Then we have to mount docker socket as volume in docker container.

docker run -it -v $(pwd)/:/workpace -w /workpace -v /var/run/docker.sock:/var/run/docker.sock 
 stashconsulting/terraform-docker:entrypoint-latest apply -auto-approve

Steps to use the standard version

You can run bash based in ubuntu and terraform commands

Run init

docker run -it -v $(pwd)/:/workpace -w /workpace stashconsulting/terraform-docker:standard-latest "terraform init"

Run apply

docker run -it -v $(pwd)/:/workpace -w /workpace -v /var/run/docker.sock:/var/run/docker.sock 
 stashconsulting/terraform-docker:standard-latest "terraform apply -auto-approve"

Another example

docker run -it -v $(pwd)/:/workpace -w /workpace stashconsulting/terraform-docker:standard-latest "ls"

Steps to use the gcloud version

You can run bash based in ubuntu, terraform and gcloud commands.

Example

docker run -it -v $(pwd)/:/workpace -w /workpace stashconsulting/terraform-docker:gcloud-latest "gcloud init"

Cloud Build

Here is a simple example of cloudbuil.yaml file that invokes the terraform-docker image to execute your tasks.

- name: 'gcr.io/project-test-270001/terraform-docker:entrypoint-latest'
  id: init
  args: ['init']

Built With 🛠️

Contributing 🖇️

There are a few ways to contribute to the terraform-docker repo. Please read CONTRIBUTING.md for additional information.

Versioning. 📌

For the versions available, see the tags on this repository.

Authors ✒️

License 📄

First-class integrations between Terraform and Docker licensed under the Server Side Public License (SSPL). Please read the LICENSE file.

Acknowledgments

This image was born when we wanted to manage the infrastructure with terraform and we required to build a docker image for a project but it was not possible because the terraform image does not have docker.

Please tell others about this project. 📢



⌨️ with ❤️ by Alvarez

About

First-class integrations between Terraform and Docker.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published