Skip to content

Commit a04f0ff

Browse files
authored
fix: Use DOCKER_CONFIG env same way as with docker cli (#849)
1 parent 30deab2 commit a04f0ff

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

.github/workflows/acc-test.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ jobs:
2525
fail-fast: false
2626
matrix:
2727
terraform_version:
28-
- "0.15.x"
2928
- "1.8.x"
3029
resource_type:
3130
- "TestAccDockerConfig"

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ Required:
172172
Optional:
173173

174174
- `auth_disabled` (Boolean) Setting this to `true` will tell the provider that this registry does not need authentication. Due to the docker internals, the provider will use dummy credentials (see https://github.com/kreuzwerker/terraform-provider-docker/issues/470 for more information). Defaults to `false`.
175-
- `config_file` (String) Path to docker json file for registry auth. Defaults to `~/.docker/config.json`. If `DOCKER_CONFIG` is set, the value of `DOCKER_CONFIG` is used as the path. `config_file` has predencen over all other options.
175+
- `config_file` (String) Path to docker json file for registry auth. Defaults to `~/.docker/config.json`. If `DOCKER_CONFIG` env variable is set, the value of `DOCKER_CONFIG` is used as the path. `DOCKER_CONFIG` can be set to a directory (as per Docker CLI) or a file path directly. `config_file` has precedence over all other options.
176176
- `config_file_content` (String) Plain content of the docker json file for registry auth. `config_file_content` has precedence over username/password.
177177
- `password` (String, Sensitive) Password for the registry. Defaults to `DOCKER_REGISTRY_PASS` env variable if set.
178178
- `username` (String) Username for the registry. Defaults to `DOCKER_REGISTRY_USER` env variable if set.

internal/provider/framework_provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func (p *frameworkProvider) Schema(ctx context.Context, req provider.SchemaReque
9696
Sensitive: true,
9797
},
9898
"config_file": schema.StringAttribute{
99-
MarkdownDescription: "Path to docker json file for registry auth. Defaults to `~/.docker/config.json`. If `DOCKER_CONFIG` is set, the value of `DOCKER_CONFIG` is used as the path. `config_file` has predencen over all other options.",
99+
MarkdownDescription: "Path to docker json file for registry auth. Defaults to `~/.docker/config.json`. If `DOCKER_CONFIG` env variable is set, the value of `DOCKER_CONFIG` is used as the path. `DOCKER_CONFIG` can be set to a directory (as per Docker CLI) or a file path directly. `config_file` has precedence over all other options.",
100100
Optional: true,
101101
},
102102
"config_file_content": schema.StringAttribute{

internal/provider/provider.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"log"
99
"os"
1010
"os/user"
11+
"path/filepath"
1112
"runtime"
1213
"strings"
1314
"sync"
@@ -128,12 +129,23 @@ func New(version string) func() *schema.Provider {
128129
},
129130

130131
"config_file": {
131-
Type: schema.TypeString,
132-
Optional: true,
133-
DefaultFunc: schema.EnvDefaultFunc("DOCKER_CONFIG", "~/.docker/config.json"),
134-
Description: "Path to docker json file for registry auth. Defaults to `~/.docker/config.json`. If `DOCKER_CONFIG` is set, the value of `DOCKER_CONFIG` is used as the path. `config_file` has predencen over all other options.",
132+
Type: schema.TypeString,
133+
Optional: true,
134+
DefaultFunc: func() (interface{}, error) {
135+
if v := os.Getenv("DOCKER_CONFIG"); v != "" {
136+
// Docker CLI expects DOCKER_CONFIG to be a directory containing config.json
137+
// Check if it's a directory and append config.json if needed
138+
info, err := os.Stat(v)
139+
if err == nil && info.IsDir() {
140+
return filepath.Join(v, "config.json"), nil
141+
}
142+
// If it's a file or doesn't exist, use it as-is for backwards compatibility
143+
return v, nil
144+
}
145+
return "~/.docker/config.json", nil
146+
},
147+
Description: "Path to docker json file for registry auth. Defaults to `~/.docker/config.json`. If `DOCKER_CONFIG` env variable is set, the value of `DOCKER_CONFIG` is used as the path. `DOCKER_CONFIG` can be set to a directory (as per Docker CLI) or a file path directly. `config_file` has precedence over all other options.",
135148
},
136-
137149
"config_file_content": {
138150
Type: schema.TypeString,
139151
Optional: true,

0 commit comments

Comments
 (0)