Skip to content

Commit

Permalink
docs: Add Azure Container Registry example
Browse files Browse the repository at this point in the history
  • Loading branch information
fdmsantos committed Aug 8, 2023
1 parent 96dcb49 commit c49b401
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 16 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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.

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Requirements
Expand Down Expand Up @@ -49,7 +50,6 @@ No modules.
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_context"></a> [context](#input\_context) | Value to specify the build context. Currently, only a PATH context is supported. | `string` | `null` | no |
| <a name="input_create"></a> [create](#input\_create) | Controls whether resources should be created | `bool` | `true` | no |
| <a name="input_dockerfile"></a> [dockerfile](#input\_dockerfile) | A directory containing your Dockerfile | `string` | `"Dockerfile"` | no |
| <a name="input_dynamic_build"></a> [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 |
| <a name="input_dynamic_build_attach_dockerfile"></a> [dynamic\_build\_attach\_dockerfile](#input\_dynamic\_build\_attach\_dockerfile) | If true, will force the docker\_image resource to be replaced when dockerfile changes. | `bool` | `true` | no |
Expand All @@ -67,7 +67,9 @@ No modules.

## Outputs

No outputs.
| Name | Description |
|------|-------------|
| <a name="output_image"></a> [image](#output\_image) | Name of the created docker image |
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

## License
Expand Down
1 change: 1 addition & 0 deletions examples/azure-container-registry/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/ignorefile.txt
2 changes: 2 additions & 0 deletions examples/azure-container-registry/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM ubuntu:22.04
COPY src /
31 changes: 31 additions & 0 deletions examples/azure-container-registry/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
provider "docker" {
host = "unix:///var/run/docker.sock"
registry_auth {
address = azurerm_container_registry.acr.login_server
username = azurerm_container_registry.acr.admin_username
password = azurerm_container_registry.acr.admin_password
}
}

provider "azurerm" {
features {}
}

resource "random_password" "this" {
length = 16
special = false
}

resource "azurerm_container_registry" "acr" {
name = "${var.name}Registry${random_password.this.result}"
resource_group_name = var.resource_group_name
location = var.location
sku = "Basic"
admin_enabled = true
}

module "docker_image" {
source = "../../"
name = var.name
registry = azurerm_container_registry.acr.login_server
}
Empty file.
1 change: 1 addition & 0 deletions examples/azure-container-registry/src/demofile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World
Empty file.
15 changes: 15 additions & 0 deletions examples/azure-container-registry/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
variable "name" {
description = "A unique name for your Image"
type = string
}

variable "resource_group_name" {
description = "Azure Resource Group to deploy platform. This Resource should be already created"
type = string
}

variable "location" {
description = "Region to deploy the resources"
type = string
default = "westeurope"
}
18 changes: 18 additions & 0 deletions examples/azure-container-registry/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
terraform {
required_version = ">= 1.0"

required_providers {
docker = {
source = "kreuzwerker/docker"
version = ">=3.0.0"
}
azurerm = {
source = "hashicorp/azurerm"
version = ">=3.0.0"
}
random = {
source = "hashicorp/random"
version = ">=3.0.0"
}
}
}
15 changes: 7 additions & 8 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
locals {
image_name = "${var.registry}/${var.name}:${var.tag}"
context = coalesce(var.context, path.cwd)
build_triggers_source = var.create && var.dynamic_build && var.dynamic_build_attach_source ? {
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}")]))
} : {}
build_triggers_dockerfile = var.create && var.dynamic_build && var.dynamic_build_attach_dockerfile ? {
build_triggers_dockerfile = var.dynamic_build && var.dynamic_build_attach_dockerfile ? {
dockerfile = filesha1("${local.context}/${var.dockerfile}")
} : {}
build_triggers_dockerignorefile = var.create && var.dynamic_build && var.dynamic_build_attach_dockerignore ? {
build_triggers_dockerignorefile = var.dynamic_build && var.dynamic_build_attach_dockerignore ? {
dockerignorefile = filesha1("${local.context}/.dockerignore")
} : {}
build_triggers_extra = var.create && var.dynamic_build && var.dynamic_build_extra != null ? var.dynamic_build_extra : null
build_triggers = var.create && var.dynamic_build ? merge(
build_triggers_extra = var.dynamic_build && var.dynamic_build_extra != null ? var.dynamic_build_extra : null
build_triggers = var.dynamic_build ? merge(
local.build_triggers_source,
local.build_triggers_dockerfile,
local.build_triggers_dockerignorefile,
Expand All @@ -20,14 +20,13 @@ locals {
}

resource "docker_registry_image" "this" {
count = var.create && var.push ? 1 : 0
name = docker_image.this[0].name
count = var.push ? 1 : 0
name = docker_image.this.name
keep_remotely = true
triggers = local.build_triggers
}

resource "docker_image" "this" {
count = var.create ? 1 : 0
name = local.image_name
force_remove = var.force_remove
keep_locally = var.keep_locally
Expand Down
4 changes: 4 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "image" {
description = "Name of the created docker image"
value = local.image_name
}
6 changes: 0 additions & 6 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
variable "create" {
description = "Controls whether resources should be created"
type = bool
default = true
}

###########
# Docker
###########
Expand Down

0 comments on commit c49b401

Please sign in to comment.