fix: correctly set docker_container devices (#843)
Some checks failed
Acc Tests / acc-test (TestAccDockerConfig, 0.15.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerConfig, 1.8.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerNetwork, 0.15.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerNetwork, 1.8.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerPlugin, 0.15.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerPlugin, 1.8.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerSecret, 0.15.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerSecret, 1.8.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerTag, 0.15.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerTag, 1.8.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerVolume, 0.15.x) (push) Has been cancelled
Acc Tests / acc-test (TestAccDockerVolume, 1.8.x) (push) Has been cancelled
Acc Tests / acc-test (true, TestAccDockerContainer, 0.15.x) (push) Has been cancelled
Acc Tests / acc-test (true, TestAccDockerContainer, 1.8.x) (push) Has been cancelled
Acc Tests / acc-test (true, TestAccDockerImage, 0.15.x) (push) Has been cancelled
Acc Tests / acc-test (true, TestAccDockerImage, 1.8.x) (push) Has been cancelled
Acc Tests / acc-test (true, TestAccDockerRegistryImage, 0.15.x) (push) Has been cancelled
Acc Tests / acc-test (true, TestAccDockerRegistryImage, 1.8.x) (push) Has been cancelled
Acc Tests / acc-test (true, TestAccDockerService, 0.15.x) (push) Has been cancelled
Acc Tests / acc-test (true, TestAccDockerService, 1.8.x) (push) Has been cancelled
Compile Binaries / compile-fast (push) Has been cancelled
Compile Binaries / compile (push) Has been cancelled
golangci-lint / lint (push) Has been cancelled
Unit Tests / unit-test (push) Has been cancelled
Website Checks / markdown-link-check (push) Has been cancelled
Docs and Website Lint / website-generation (push) Has been cancelled
Docs and Website Lint / website-lint-spellcheck-tffmt (push) Has been cancelled
Docs and Website Lint / markdown-lint (push) Has been cancelled

This commit is contained in:
Martin 2026-01-27 22:35:31 +01:00 committed by GitHub
parent d575856f27
commit 0254446f95
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 29 additions and 16 deletions

View file

@ -127,7 +127,7 @@ Required:
Optional:
- `container_path` (String) The path in the container where the device will be bound.
- `container_path` (String) The path in the container where the device will be bound. If not set, it defaults to the value of `host_path`.
- `permissions` (String) The cgroup permissions given to the container to access the device. Defaults to `rwm`.

View file

@ -7,10 +7,12 @@ Bump of minimum terraform version to `1.1.5` or newer. This is done as part of i
## `docker_container`
Reworked handling of stopped containers: If a container is stopped (or exists for some other reason), Terraform now correctly shows a change on `plan` and restarts the container on `apply`. To trigger the change, the `must_run` attribute is exploited. `must_run` defaults to `true` and when a container is in a not running state, the provider sets `must_run` to `false` to trigger a state change.
This fixes the cases where a stopped container gets deleted during a `plan`
**Reworked handling of stopped containers:** If a container is stopped (or exists for some other reason), Terraform now correctly shows a change on `plan` and restarts the container on `apply`. To trigger the change, the `must_run` attribute is exploited. `must_run` defaults to `true` and when a container is in a not running state, the provider sets `must_run` to `false` to trigger a state change. This fixes the cases where a stopped container gets deleted during a `plan`. No migration needed.
`ports` - **Ports on stopped container force replacement:** This is now fixed through https://github.com/kreuzwerker/terraform-provider-docker/pull/842, no migration needed.
`devices` - **Fix the replacement of devices** Using `devices` blocks with not all 3 attributes now does not trigger resource replacements anymore. This fixes https://github.com/kreuzwerker/terraform-provider-docker/issues/603.
* Fixes Ports on stopped container force replacement bug (https://github.com/kreuzwerker/terraform-provider-docker/issues/77)
## `docker_network`

View file

@ -649,7 +649,7 @@ func resourceDockerContainer() *schema.Resource {
},
"container_path": {
Type: schema.TypeString,
Description: "The path in the container where the device will be bound.",
Description: "The path in the container where the device will be bound. If not set, it defaults to the value of `host_path`.",
Optional: true,
ForceNew: true,
},
@ -658,6 +658,7 @@ func resourceDockerContainer() *schema.Resource {
Description: "The cgroup permissions given to the container to access the device. Defaults to `rwm`.",
Optional: true,
ForceNew: true,
Default: "rwm",
},
},
},

View file

@ -797,7 +797,7 @@ func resourceDockerContainerRead(ctx context.Context, d *schema.ResourceData, me
// https://github.com/terraform-providers/terraform-provider-docker/pull/269
d.Set("privileged", container.HostConfig.Privileged)
if err = d.Set("devices", flattenDevices(container.HostConfig.Devices)); err != nil {
if err = d.Set("devices", flattenDevices(container.HostConfig.Devices, d.Get("devices").(*schema.Set))); err != nil {
log.Printf("[WARN] failed to set container hostconfig devices from API: %s", err)
}
// "destroy_grace_seconds" can't be imported

View file

@ -241,12 +241,9 @@ func deviceSetToDockerDevices(devices *schema.Set) []container.DeviceMapping {
containerPath := deviceMap["container_path"].(string)
permissions := deviceMap["permissions"].(string)
switch {
case len(containerPath) == 0:
// Default container_path to host_path if not specified
if len(containerPath) == 0 {
containerPath = hostPath
fallthrough
case len(permissions) == 0:
permissions = "rwm"
}
device := container.DeviceMapping{
@ -338,14 +335,23 @@ func flattenUlimits(in []*units.Ulimit) []interface{} {
return ulimits
}
func flattenDevices(in []container.DeviceMapping) []interface{} {
func flattenDevices(in []container.DeviceMapping, configuredDevices *schema.Set) []interface{} {
devices := make([]interface{}, len(in))
list := configuredDevices.List()
for i, device := range in {
devices[i] = map[string]interface{}{
"host_path": device.PathOnHost,
"container_path": device.PathInContainer,
"permissions": device.CgroupPermissions,
configuredDevice := list[i].(map[string]interface{})
deviceMap := map[string]interface{}{
"host_path": device.PathOnHost,
"permissions": device.CgroupPermissions,
}
// Only set container_path if it was explicitly configured by the user
if value, ok := configuredDevice["container_path"].(string); ok && value != "" {
deviceMap["container_path"] = device.PathInContainer
}
devices[i] = deviceMap
}
return devices

View file

@ -11,4 +11,8 @@ resource "docker_container" "foo" {
container_path = "/dev/zero_test"
permissions = "rwm"
}
devices {
host_path = "/dev/null"
}
}