Skip to content

Commit

Permalink
feat: allow existing image to be used from another module (#62)
Browse files Browse the repository at this point in the history
* feat: allow existing image to be used from another module

* fix: use name as output. Also add image_id as output
  • Loading branch information
alinefr authored Jul 4, 2023
1 parent 1bb3109 commit c4377f2
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 14 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,11 @@ No modules.
| <a name="input_docker_networks"></a> [docker\_networks](#input\_docker\_networks) | List of custom networks to create<pre>hcl<br>docker_networks = [<br> {<br> name = "proxy-tier"<br> ipam_config = {<br> aux_address = {}<br> gateway = "10.0.0.1"<br> subnet = "10.0.0.0/24"<br> }<br> }<br>]</pre> | `any` | `[]` | no |
| <a name="input_entrypoint"></a> [entrypoint](#input\_entrypoint) | Override the default entrypoint | `list(string)` | `null` | no |
| <a name="input_environment"></a> [environment](#input\_environment) | Add environment variables | `map(string)` | `null` | no |
| <a name="input_existing_image"></a> [existing\_image](#input\_existing\_image) | Specify an existing image from another module | `string` | `null` | no |
| <a name="input_healthcheck"></a> [healthcheck](#input\_healthcheck) | Test to check if container is healthy | <pre>object({<br> interval = string<br> retries = number<br> start_period = string<br> test = list(string)<br> timeout = string<br> })</pre> | `null` | no |
| <a name="input_host_paths"></a> [host\_paths](#input\_host\_paths) | Mount host paths | <pre>map(object({<br> container_path = string<br> read_only = bool<br> }))</pre> | `{}` | no |
| <a name="input_hostname"></a> [hostname](#input\_hostname) | Set docker hostname | `string` | `null` | no |
| <a name="input_image"></a> [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 |
| <a name="input_image"></a> [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 |
| <a name="input_named_volumes"></a> [named\_volumes](#input\_named\_volumes) | Mount named volumes | <pre>map(object({<br> container_path = string<br> read_only = bool<br> create = bool<br> }))</pre> | `{}` | no |
| <a name="input_network_mode"></a> [network\_mode](#input\_network\_mode) | Specify a custom network mode | `string` | `null` | no |
| <a name="input_networks_advanced"></a> [networks\_advanced](#input\_networks\_advanced) | Advanced network options for the container<pre>hcl<br>networks_advanced = [<br> {<br> name = "proxy-tier"<br> ipv4_address = "10.0.0.14"<br> },<br> {<br> name = "media-tier"<br> ipv4_address = "172.0.0.14"<br> }<br>]</pre> | `any` | `null` | no |
Expand All @@ -179,7 +180,8 @@ No modules.
| <a name="output_environment"></a> [environment](#output\_environment) | n/a |
| <a name="output_healthcheck"></a> [healthcheck](#output\_healthcheck) | n/a |
| <a name="output_hostname"></a> [hostname](#output\_hostname) | n/a |
| <a name="output_image"></a> [image](#output\_image) | n/a |
| <a name="output_image_id"></a> [image\_id](#output\_image\_id) | n/a |
| <a name="output_image_name"></a> [image\_name](#output\_image\_name) | n/a |
| <a name="output_network_mode"></a> [network\_mode](#output\_network\_mode) | n/a |
| <a name="output_networks_advanced"></a> [networks\_advanced](#output\_networks\_advanced) | n/a |
| <a name="output_ports"></a> [ports](#output\_ports) | n/a |
Expand Down
5 changes: 3 additions & 2 deletions examples/basic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ No resources.
| <a name="input_docker_networks"></a> [docker\_networks](#input\_docker\_networks) | List of custom networks to create<pre>hcl<br>docker_networks = [<br> {<br> name = "proxy-tier"<br> ipam_config = {<br> aux_address = {}<br> gateway = "10.0.0.1"<br> subnet = "10.0.0.0/24"<br> }<br> }<br>]</pre> | `any` | `[]` | no |
| <a name="input_entrypoint"></a> [entrypoint](#input\_entrypoint) | Override the default entrypoint | `list(string)` | `null` | no |
| <a name="input_environment"></a> [environment](#input\_environment) | Add environment variables | `map(string)` | `null` | no |
| <a name="input_existing_image"></a> [existing\_image](#input\_existing\_image) | Specify an existing image from another module | `string` | `null` | no |
| <a name="input_healthcheck"></a> [healthcheck](#input\_healthcheck) | Test to check if container is healthy | <pre>object({<br> interval = string<br> retries = number<br> start_period = string<br> test = list(string)<br> timeout = string<br> })</pre> | `null` | no |
| <a name="input_host_paths"></a> [host\_paths](#input\_host\_paths) | Mount host paths | <pre>map(object({<br> container_path = string<br> read_only = bool<br> }))</pre> | `{}` | no |
| <a name="input_hostname"></a> [hostname](#input\_hostname) | Set docker hostname | `string` | `null` | no |
| <a name="input_image"></a> [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 |
| <a name="input_image"></a> [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 |
| <a name="input_named_volumes"></a> [named\_volumes](#input\_named\_volumes) | Mount named volumes | <pre>map(object({<br> container_path = string<br> read_only = bool<br> create = bool<br> }))</pre> | `{}` | no |
| <a name="input_network_mode"></a> [network\_mode](#input\_network\_mode) | Specify a custom network mode | `string` | `null` | no |
| <a name="input_networks_advanced"></a> [networks\_advanced](#input\_networks\_advanced) | Advanced network options for the container<pre>hcl<br>networks_advanced = [<br> {<br> name = "proxy-tier"<br> ipv4_address = "10.0.0.14"<br> },<br> {<br> name = "media-tier"<br> ipv4_address = "172.0.0.14"<br> }<br>]</pre> | `any` | `null` | no |
Expand All @@ -57,7 +58,7 @@ No resources.
| <a name="output_environment"></a> [environment](#output\_environment) | n/a |
| <a name="output_healthcheck"></a> [healthcheck](#output\_healthcheck) | n/a |
| <a name="output_hostname"></a> [hostname](#output\_hostname) | n/a |
| <a name="output_image"></a> [image](#output\_image) | n/a |
| <a name="output_image_name"></a> [image\_name](#output\_image\_name) | n/a |
| <a name="output_network_mode"></a> [network\_mode](#output\_network\_mode) | n/a |
| <a name="output_networks_advanced"></a> [networks\_advanced](#output\_networks\_advanced) | n/a |
| <a name="output_ports"></a> [ports](#output\_ports) | n/a |
Expand Down
4 changes: 2 additions & 2 deletions examples/basic/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
output "image" {
value = module.docker.image
output "image_name" {
value = module.docker.image_name
}

output "container_name" {
Expand Down
6 changes: 6 additions & 0 deletions examples/basic/variables.tf
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
12 changes: 7 additions & 5 deletions main.tf
Original file line number Diff line number Diff line change
@@ -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" {
Expand All @@ -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
Expand Down
8 changes: 6 additions & 2 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -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" {
Expand Down
2 changes: 1 addition & 1 deletion tests/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit c4377f2

Please sign in to comment.