I am trying to automate the process of creating a docker instance of a pihole using TF.
For this, I’ve got the majority of my stuff working except for the volumes.
When I do create the volumes using volumes the contents of my host_path never end up on the actual docker instance. I’ve checked that by using docker exec -it XXX /bin/sh and inside /etc/pihole/ there is nothing.
# main.tf
resource "docker_container" "pihole" {
name = var.container_name
image = docker_image.pihole.latest
...
dynamic "volumes" {
for_each = var.volume_mappings
iterator = each
content {
volume_name = each.value.volume_name
host_path = each.value.host_path
container_path = each.value.container_path
read_only = each.value.read_only
}
}
...
# variables.tf
variable "volume_mappings" {
description = "List of all the volume mappings"
type = list(any)
default = [
{
volume_name = "data"
host_path = "/terraform/pihole/etc-pihole/"
container_path = "/etc/pihole/"
read_only = false
},
{
volume_name = "dnsmasq"
host_path = "/terraform/pihole/etc-dnsmasq.d/"
container_path = "/etc/dnsmasq.d/"
read_only = false
}
]
}
When I switch from using volumes to using mounts I cannot get it working, it always gives me this error
mounts {
target = "/home/darcey/Desktop/terraform/pihole/pihole/"
source = "/etc/pihole/"
type = "bind"
read_only = false
bind_options {
propagation = "rprivate"
}
}
╷
│ Error: Unable to create container: Error response from daemon: invalid mount config for type "bind": bind source path does not exist: /etc/pihole/
│
│ with docker_container.pihole,
│ on main.tf line 25, in resource "docker_container" "pihole":
│ 25: resource "docker_container" "pihole" {
What I find bizarre is that when I do the docker-compose up everything works like a charm. When I try to automate that pihole with TF and I set up the machine private IP and my DNS I lose internet connection.
Thoughts?