1 – Download terraform cli binary and put it on /usr/local/bin
2 – create the main.tf file
cat > ~/main.tf <<'EOF' # configure docker provider provider "docker" { version = "~> 2.7" host = "tcp://127.0.0.1:2375" } # create a image resource "docker_image" "alpine" { name = "alpine" } # create a container (it will run for 1 day) resource "docker_container" "foo" { image = "alpine" name = "foo" command = ["sleep", "1d"] hostname = "foo" volumes { host_path = "/path/to/virtual_disk" container_path = "/mnt" read_only = false } } EOF
3 – create a working directory “.terraform” and get the docker provider binary
terraform init
4 – show the execution plan (optional)
terraform plan
5 – create or change the infrastructure
terraform apply
6 – see the state of docker container
docker ps -a
7 – execute commands in the container
docker exec foo ip -4 addr docker exec foo sh -c 'ip -4 addr; hostname'
8 – enter in the container
docker exec -it foo sh
Links:
https://www.terraform.io/docs/providers/docker/index.html
https://www.terraform.io/docs/providers/docker/r/container.html