mirror of
https://github.com/kreuzwerker/terraform-provider-docker.git
synced 2025-12-21 15:20:02 -05:00
138 lines
3.1 KiB
Markdown
138 lines
3.1 KiB
Markdown
|
|
---
|
||
|
|
# generated by https://github.com/hashicorp/terraform-plugin-docs
|
||
|
|
page_title: "docker_config Resource - terraform-provider-docker"
|
||
|
|
subcategory: ""
|
||
|
|
description: |-
|
||
|
|
Manages the configs of a Docker service in a swarm.
|
||
|
|
---
|
||
|
|
<!-- Bug: Type and Name are switched -->
|
||
|
|
# docker_config (Resource)
|
||
|
|
|
||
|
|
Manages the configs of a Docker service in a swarm.
|
||
|
|
|
||
|
|
## Example Usage
|
||
|
|
|
||
|
|
### Basic
|
||
|
|
|
||
|
|
```terraform
|
||
|
|
resource "docker_config" "foo_config" {
|
||
|
|
name = "foo_config"
|
||
|
|
data = base64encode("{\"a\": \"b\"}")
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
### Advanced
|
||
|
|
#### Dynamically set config with a template
|
||
|
|
In this example you can use the `${var.foo_port}` variable to dynamically
|
||
|
|
set the `${port}` variable in the `foo.configs.json.tpl` template and create
|
||
|
|
the data of the `foo_config` with the help of the `base64encode` interpolation
|
||
|
|
function.
|
||
|
|
|
||
|
|
The file `foo.config.json.tpl` has the following content:
|
||
|
|
|
||
|
|
```gojson
|
||
|
|
{
|
||
|
|
"server": {
|
||
|
|
"public_port": ${port}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
and the resource uses it as follows:
|
||
|
|
|
||
|
|
```terraform
|
||
|
|
resource "docker_config" "foo_config" {
|
||
|
|
name = "foo_config"
|
||
|
|
data = base64encode(
|
||
|
|
templatefile("${path.cwd}/foo.config.json.tpl",
|
||
|
|
{
|
||
|
|
port = 8080
|
||
|
|
}
|
||
|
|
)
|
||
|
|
)
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
#### Update config with no downtime
|
||
|
|
To update a `config`, Terraform will destroy the existing resource and create a replacement.
|
||
|
|
To effectively use a `docker_config` resource with a `docker_service` resource, it's recommended
|
||
|
|
to specify `create_before_destroy` in a `lifecycle` block. Provide a unique `name` attribute,
|
||
|
|
for example with one of the interpolation functions `uuid` or `timestamp` as shown
|
||
|
|
in the example below. The reason is this [issue](https://github.com/moby/moby/issues/35803).
|
||
|
|
|
||
|
|
```terraform
|
||
|
|
resource "docker_config" "service_config" {
|
||
|
|
name = "${var.service_name}-config-${replace(timestamp(), ":", ".")}"
|
||
|
|
data = base64encode(
|
||
|
|
templatefile("${path.cwd}/foo.config.json.tpl",
|
||
|
|
{
|
||
|
|
port = 8080
|
||
|
|
}
|
||
|
|
)
|
||
|
|
)
|
||
|
|
|
||
|
|
lifecycle {
|
||
|
|
ignore_changes = ["name"]
|
||
|
|
create_before_destroy = true
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
resource "docker_service" "service" {
|
||
|
|
# ... other attributes omitted for brevity
|
||
|
|
configs = [
|
||
|
|
{
|
||
|
|
config_id = docker_config.service_config.id
|
||
|
|
config_name = docker_config.service_config.name
|
||
|
|
file_name = "/root/configs/configs.json"
|
||
|
|
},
|
||
|
|
]
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
<!-- schema generated by tfplugindocs -->
|
||
|
|
## Schema
|
||
|
|
|
||
|
|
### Required
|
||
|
|
|
||
|
|
- **data** (String) Base64-url-safe-encoded config data
|
||
|
|
- **name** (String) User-defined name of the config
|
||
|
|
|
||
|
|
### Optional
|
||
|
|
|
||
|
|
- **id** (String) The ID of this resource.
|
||
|
|
|
||
|
|
## Import
|
||
|
|
|
||
|
|
Import is supported using the following syntax by providing the `id`:
|
||
|
|
|
||
|
|
```shell
|
||
|
|
#!/bin/bash
|
||
|
|
terraform import docker_config.foo id
|
||
|
|
```
|
||
|
|
|
||
|
|
### Example
|
||
|
|
|
||
|
|
Assuming you created a `config` as follows
|
||
|
|
|
||
|
|
```shell
|
||
|
|
#!/bin/bash
|
||
|
|
printf '{"a":"b"}' | docker config create foo -
|
||
|
|
# prints the id
|
||
|
|
08c26c477474478d971139f750984775a7f019dbe8a2e7f09d66a187c009e66d
|
||
|
|
```
|
||
|
|
|
||
|
|
you provide the definition for the resource as follows
|
||
|
|
|
||
|
|
```terraform
|
||
|
|
resource "docker_config" "foo" {
|
||
|
|
name = "foo"
|
||
|
|
data = base64encode("{\"a\": \"b\"}")
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
then the import command is as follows
|
||
|
|
|
||
|
|
```shell
|
||
|
|
#!/bin/bash
|
||
|
|
terraform import docker_config.foo 08c26c477474478d971139f750984775a7f019dbe8a2e7f09d66a187c009e66d
|
||
|
|
```
|