diff --git a/README.md b/README.md
index 83447fc..3a274d4 100644
--- a/README.md
+++ b/README.md
@@ -19,6 +19,7 @@ module "docker_image" {
- [Simple](https://github.com/redevaerk/terraform-docker-image/tree/main/examples/simple) - This example will build and push Docker image with default settings.
- [Build Image With Context](https://github.com/redevaerk/terraform-docker-image/tree/main/examples/build-image-with-context) - This example will build and push Docker image with a context in different folder.
- [Azure Container Registry](https://github.com/redevaerk/terraform-docker-image/tree/main/examples/build-image-with-context) - This example will build and push Docker image to Azure Container Registry.
+- [Multiple Tags](https://github.com/redevaerk/terraform-docker-image/tree/main/examples/multiple-tags) - This example will build and push Docker image with multiple tags.
## Requirements
@@ -49,6 +50,7 @@ No modules.
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
+| [add\_latest\_tag](#input\_add\_latest\_tag) | Add latest tag to the Image. If false, it's necessary to add at least one tag on 'variable.tags'. | `bool` | `true` | no |
| [context](#input\_context) | Value to specify the build context. Currently, only a PATH context is supported. | `string` | `null` | no |
| [dockerfile](#input\_dockerfile) | A directory containing your Dockerfile | `string` | `"Dockerfile"` | no |
| [dynamic\_build](#input\_dynamic\_build) | If true, will force the docker\_image resource to be replaced. This can be used to rebuild an image when contents of source code folders change. | `bool` | `true` | no |
@@ -59,17 +61,19 @@ No modules.
| [dynamic\_build\_source\_dir](#input\_dynamic\_build\_source\_dir) | A directory containing your source code. This variable will be used in dynamic build when var.dynamic\_build\_attach\_source is true. | `string` | `"src"` | no |
| [force\_remove](#input\_force\_remove) | If true, then the image is removed forcibly when the resource is destroyed. | `bool` | `false` | no |
| [keep\_locally](#input\_keep\_locally) | If true, then the Docker image won't be deleted on destroy operation. If this is false, it will delete the image from the docker local storage on destroy operation. | `bool` | `false` | no |
+| [keep\_remotely](#input\_keep\_remotely) | If true, then the Docker image won't be deleted on destroy operation. If this is false, it will delete the image from the docker registry on destroy operation. | `bool` | `false` | no |
| [labels](#input\_labels) | A map of labels to add to the Image | `map(string)` | `{}` | no |
| [name](#input\_name) | A unique name for your Image | `string` | n/a | yes |
| [push](#input\_push) | If true, then the Image will be pushed to the registry | `bool` | `true` | no |
| [registry](#input\_registry) | Registry Name to push the Image | `string` | `"registry-1.docker.io"` | no |
-| [tag](#input\_tag) | A tag for your Image | `string` | `"latest"` | no |
+| [tags](#input\_tags) | Optionally a tags to add to the Image | `list(string)` | `[]` | no |
## Outputs
| Name | Description |
|------|-------------|
-| [image](#output\_image) | Name of the created docker image |
+| [images](#output\_images) | List of all created docker images |
+| [map\_images](#output\_map\_images) | Map per tag corresponding docker images |
## License
diff --git a/examples/multiple-tags/.dockerignore b/examples/multiple-tags/.dockerignore
new file mode 100644
index 0000000..af996d0
--- /dev/null
+++ b/examples/multiple-tags/.dockerignore
@@ -0,0 +1 @@
+src/ignorefile.txt
diff --git a/examples/multiple-tags/Dockerfile b/examples/multiple-tags/Dockerfile
new file mode 100644
index 0000000..b29bb02
--- /dev/null
+++ b/examples/multiple-tags/Dockerfile
@@ -0,0 +1,2 @@
+FROM ubuntu:22.04
+COPY src /
diff --git a/examples/multiple-tags/main.tf b/examples/multiple-tags/main.tf
new file mode 100644
index 0000000..25b08eb
--- /dev/null
+++ b/examples/multiple-tags/main.tf
@@ -0,0 +1,14 @@
+provider "docker" {
+ host = "unix:///var/run/docker.sock"
+ registry_auth {
+ address = "registry-1.docker.io"
+ config_file = pathexpand("~/.docker/config.json")
+ }
+}
+
+module "docker_image" {
+ source = "../../"
+ name = var.name
+ tags = ["v1", "beta"]
+ keep_remotely = false
+}
diff --git a/examples/multiple-tags/outputs.tf b/examples/multiple-tags/outputs.tf
new file mode 100644
index 0000000..eb9b8b7
--- /dev/null
+++ b/examples/multiple-tags/outputs.tf
@@ -0,0 +1,9 @@
+output "images" {
+ description = "List of all created docker images"
+ value = module.docker_image.images
+}
+
+output "map_images" {
+ description = "Map per tag corresponding docker images"
+ value = module.docker_image.map_images
+}
diff --git a/examples/multiple-tags/src/demofile.txt b/examples/multiple-tags/src/demofile.txt
new file mode 100644
index 0000000..557db03
--- /dev/null
+++ b/examples/multiple-tags/src/demofile.txt
@@ -0,0 +1 @@
+Hello World
diff --git a/examples/multiple-tags/src/ignorefile.txt b/examples/multiple-tags/src/ignorefile.txt
new file mode 100644
index 0000000..e69de29
diff --git a/examples/multiple-tags/variables.tf b/examples/multiple-tags/variables.tf
new file mode 100644
index 0000000..5c442aa
--- /dev/null
+++ b/examples/multiple-tags/variables.tf
@@ -0,0 +1,4 @@
+variable "name" {
+ description = "A unique name for your Image"
+ type = string
+}
diff --git a/examples/multiple-tags/versions.tf b/examples/multiple-tags/versions.tf
new file mode 100644
index 0000000..76166e5
--- /dev/null
+++ b/examples/multiple-tags/versions.tf
@@ -0,0 +1,10 @@
+terraform {
+ required_version = ">= 1.0"
+
+ required_providers {
+ docker = {
+ source = "kreuzwerker/docker"
+ version = ">=3.0.0"
+ }
+ }
+}
diff --git a/main.tf b/main.tf
index e0036a7..98c58ed 100644
--- a/main.tf
+++ b/main.tf
@@ -1,5 +1,6 @@
locals {
- image_name = "${var.registry}/${var.name}:${var.tag}"
+ tags = var.add_latest_tag && !contains(var.tags, "latest") ? compact(concat(["latest"], var.tags)) : compact(var.tags)
+ image_name = "${var.registry}/${var.name}"
context = coalesce(var.context, path.cwd)
build_triggers_source = var.dynamic_build && var.dynamic_build_attach_source ? {
source_dir = sha1(join("", [for f in fileset(local.context, "${var.dynamic_build_source_dir}/*") : filesha1("${local.context}/${f}")]))
@@ -20,20 +21,20 @@ locals {
}
resource "docker_registry_image" "this" {
- count = var.push ? 1 : 0
- name = docker_image.this.name
- keep_remotely = true
+ for_each = var.push ? toset(docker_image.this.build[*].tag[0]) : toset([])
+ name = each.value
+ keep_remotely = var.keep_remotely
triggers = local.build_triggers
}
resource "docker_image" "this" {
- name = local.image_name
+ name = "${local.image_name}:${local.tags[0]}"
force_remove = var.force_remove
keep_locally = var.keep_locally
build {
context = local.context
dockerfile = var.dockerfile
- tag = [local.image_name]
+ tag = [for tag in local.tags : "${local.image_name}:${tag}"]
labels = var.labels
}
triggers = local.build_triggers
diff --git a/outputs.tf b/outputs.tf
index dc5c76e..7c89e4b 100644
--- a/outputs.tf
+++ b/outputs.tf
@@ -1,4 +1,9 @@
-output "image" {
- description = "Name of the created docker image"
- value = local.image_name
+output "images" {
+ description = "List of all created docker images"
+ value = docker_image.this.build[*].tag[0]
+}
+
+output "map_images" {
+ description = "Map per tag corresponding docker images"
+ value = { for tag in local.tags : tag => "${local.image_name}:${tag}" }
}
diff --git a/variables.tf b/variables.tf
index 882ecaf..c53fac6 100644
--- a/variables.tf
+++ b/variables.tf
@@ -12,10 +12,16 @@ variable "name" {
type = string
}
-variable "tag" {
- description = "A tag for your Image"
- type = string
- default = "latest"
+variable "add_latest_tag" {
+ description = "Add latest tag to the Image. If false, it's necessary to add at least one tag on 'variable.tags'."
+ type = bool
+ default = true
+}
+
+variable "tags" {
+ description = "Optionally a tags to add to the Image"
+ type = list(string)
+ default = []
}
variable "keep_locally" {
@@ -24,6 +30,12 @@ variable "keep_locally" {
default = false
}
+variable "keep_remotely" {
+ description = "If true, then the Docker image won't be deleted on destroy operation. If this is false, it will delete the image from the docker registry on destroy operation."
+ type = bool
+ default = false
+}
+
variable "force_remove" {
description = " If true, then the image is removed forcibly when the resource is destroyed."
type = bool