diff --git a/README.md b/README.md index 70a7b85..234f6f5 100644 --- a/README.md +++ b/README.md @@ -151,10 +151,11 @@ No modules. | [docker\_networks](#input\_docker\_networks) | List of custom networks to create
hcl| `any` | `[]` | no | | [entrypoint](#input\_entrypoint) | Override the default entrypoint | `list(string)` | `null` | no | | [environment](#input\_environment) | Add environment variables | `map(string)` | `null` | no | +| [existing\_image](#input\_existing\_image) | Specify an existing image from another module | `string` | `null` | no | | [healthcheck](#input\_healthcheck) | Test to check if container is healthy |
docker_networks = [
{
name = "proxy-tier"
ipam_config = {
aux_address = {}
gateway = "10.0.0.1"
subnet = "10.0.0.0/24"
}
}
]
object({| `null` | no | | [host\_paths](#input\_host\_paths) | Mount host paths |
interval = string
retries = number
start_period = string
test = list(string)
timeout = string
})
map(object({| `{}` | no | | [hostname](#input\_hostname) | Set docker hostname | `string` | `null` | no | -| [image](#input\_image) | Specify the image to start the container from. Can either be a repository/tag or a partial image ID | `string` | n/a | yes | +| [image](#input\_image) | Specify the image to start the container from. Can either be a repository/tag or a partial image ID | `string` | `null` | no | | [named\_volumes](#input\_named\_volumes) | Mount named volumes |
container_path = string
read_only = bool
}))
map(object({| `{}` | no | | [network\_mode](#input\_network\_mode) | Specify a custom network mode | `string` | `null` | no | | [networks\_advanced](#input\_networks\_advanced) | Advanced network options for the container
container_path = string
read_only = bool
create = bool
}))
hcl| `any` | `null` | no | @@ -179,7 +180,8 @@ No modules. | [environment](#output\_environment) | n/a | | [healthcheck](#output\_healthcheck) | n/a | | [hostname](#output\_hostname) | n/a | -| [image](#output\_image) | n/a | +| [image\_id](#output\_image\_id) | n/a | +| [image\_name](#output\_image\_name) | n/a | | [network\_mode](#output\_network\_mode) | n/a | | [networks\_advanced](#output\_networks\_advanced) | n/a | | [ports](#output\_ports) | n/a | diff --git a/examples/basic/README.md b/examples/basic/README.md index 40ede30..cb7ec18 100644 --- a/examples/basic/README.md +++ b/examples/basic/README.md @@ -29,10 +29,11 @@ No resources. | [docker\_networks](#input\_docker\_networks) | List of custom networks to create
networks_advanced = [
{
name = "proxy-tier"
ipv4_address = "10.0.0.14"
},
{
name = "media-tier"
ipv4_address = "172.0.0.14"
}
]
hcl| `any` | `[]` | no | | [entrypoint](#input\_entrypoint) | Override the default entrypoint | `list(string)` | `null` | no | | [environment](#input\_environment) | Add environment variables | `map(string)` | `null` | no | +| [existing\_image](#input\_existing\_image) | Specify an existing image from another module | `string` | `null` | no | | [healthcheck](#input\_healthcheck) | Test to check if container is healthy |
docker_networks = [
{
name = "proxy-tier"
ipam_config = {
aux_address = {}
gateway = "10.0.0.1"
subnet = "10.0.0.0/24"
}
}
]
object({| `null` | no | | [host\_paths](#input\_host\_paths) | Mount host paths |
interval = string
retries = number
start_period = string
test = list(string)
timeout = string
})
map(object({| `{}` | no | | [hostname](#input\_hostname) | Set docker hostname | `string` | `null` | no | -| [image](#input\_image) | Specify the image to start the container from. Can either be a repository/tag or a partial image ID | `string` | n/a | yes | +| [image](#input\_image) | Specify the image to start the container from. Can either be a repository/tag or a partial image ID | `string` | `null` | no | | [named\_volumes](#input\_named\_volumes) | Mount named volumes |
container_path = string
read_only = bool
}))
map(object({| `{}` | no | | [network\_mode](#input\_network\_mode) | Specify a custom network mode | `string` | `null` | no | | [networks\_advanced](#input\_networks\_advanced) | Advanced network options for the container
container_path = string
read_only = bool
create = bool
}))
hcl| `any` | `null` | no | @@ -57,7 +58,7 @@ No resources. | [environment](#output\_environment) | n/a | | [healthcheck](#output\_healthcheck) | n/a | | [hostname](#output\_hostname) | n/a | -| [image](#output\_image) | n/a | +| [image\_name](#output\_image\_name) | n/a | | [network\_mode](#output\_network\_mode) | n/a | | [networks\_advanced](#output\_networks\_advanced) | n/a | | [ports](#output\_ports) | n/a | diff --git a/examples/basic/outputs.tf b/examples/basic/outputs.tf index 79a5baf..de3d2b9 100644 --- a/examples/basic/outputs.tf +++ b/examples/basic/outputs.tf @@ -1,5 +1,5 @@ -output "image" { - value = module.docker.image +output "image_name" { + value = module.docker.image_name } output "container_name" { diff --git a/examples/basic/variables.tf b/examples/basic/variables.tf index 1cc6a88..8d9f46b 100644 --- a/examples/basic/variables.tf +++ b/examples/basic/variables.tf @@ -1,6 +1,12 @@ variable "image" { description = "Specify the image to start the container from. Can either be a repository/tag or a partial image ID" type = string + default = null +} +variable "existing_image" { + description = "Specify an existing image from another module" + type = string + default = null } variable "container_name" { description = "Custom container name" diff --git a/main.tf b/main.tf index 5bd552b..cd6d30c 100644 --- a/main.tf +++ b/main.tf @@ -1,16 +1,18 @@ locals { - image = var.image + image = var.image == null ? var.existing_image : var.image img_nouser = replace(local.image, "/", "") != local.image ? split("/", local.image)[1] : split("/", local.image)[0] container_name = split(":", local.img_nouser)[0] } data "docker_registry_image" "default" { - name = var.image + for_each = var.existing_image == null ? toset([var.image]) : toset([]) + name = var.image } resource "docker_image" "default" { - name = data.docker_registry_image.default.name - pull_triggers = [data.docker_registry_image.default.sha256_digest] + for_each = var.existing_image == null ? toset([var.image]) : toset([]) + name = data.docker_registry_image.default[var.image].name + pull_triggers = [data.docker_registry_image.default[var.image].sha256_digest] } resource "docker_volume" "default" { @@ -36,7 +38,7 @@ resource "docker_network" "default" { resource "docker_container" "default" { name = var.container_name != null ? var.container_name : local.container_name - image = docker_image.default.image_id + image = var.existing_image != null ? var.existing_image : docker_image.default[var.image].image_id hostname = var.hostname restart = var.restart_policy privileged = var.privileged diff --git a/outputs.tf b/outputs.tf index fc6f22f..491def5 100644 --- a/outputs.tf +++ b/outputs.tf @@ -1,5 +1,9 @@ -output "image" { - value = docker_image.default.name +output "image_name" { + value = var.existing_image != null ? var.existing_image : docker_image.default[var.image].name +} + +output "image_id" { + value = var.existing_image != null ? var.existing_image : docker_image.default[var.image].image_id } output "container_name" { diff --git a/tests/basic_test.go b/tests/basic_test.go index 67d26e3..0b98aca 100644 --- a/tests/basic_test.go +++ b/tests/basic_test.go @@ -126,7 +126,7 @@ func validateOutputs(t *testing.T, opts *terraform.Options) { } // Image - image, _ := jsonParsed.JSONPointer("/image/value") + image, _ := jsonParsed.JSONPointer("/image_name/value") assert.Equal(t, "nginx:latest", image.Data().(string)) // Container name diff --git a/variables.tf b/variables.tf index 1cc6a88..8d9f46b 100644 --- a/variables.tf +++ b/variables.tf @@ -1,6 +1,12 @@ variable "image" { description = "Specify the image to start the container from. Can either be a repository/tag or a partial image ID" type = string + default = null +} +variable "existing_image" { + description = "Specify an existing image from another module" + type = string + default = null } variable "container_name" { description = "Custom container name"
networks_advanced = [
{
name = "proxy-tier"
ipv4_address = "10.0.0.14"
},
{
name = "media-tier"
ipv4_address = "172.0.0.14"
}
]