Skip to content

Commit 711fcae

Browse files
committed
cli/command/container: --use-api-socket: don't write empty credentials
Before this patch, a valid, but empty set of credentials would still write a config-file to the container and set `DOCKER_CONFIG`: mkdir -p tmpConfig export DOCKER_CONFIG=$PWD/tmpConfig echo '{}' > "${DOCKER_CONFIG}/config.json" docker run --rm --use-api-socket alpine cat /run/secrets/docker/config.json { "auths": {} } echo '{"auths": {}}' > "${DOCKER_CONFIG}/config.json" docker run --rm --use-api-socket alpine cat /run/secrets/docker/config.json { "auths": {} } echo '{"auths": {"https://index.docker.io/v1/": {"auth": "am9lam9lOmhlbGxv"}}}' > "${DOCKER_CONFIG}/config.json" docker run --rm --use-api-socket alpine cat /run/secrets/docker/config.json { "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv" } } } With this patch, the `DOCKER_CONFIG` env-var and config-file are only created if we have credentials to set; mkdir -p tmpConfig export DOCKER_CONFIG=$PWD/tmpConfig echo '{}' > "${DOCKER_CONFIG}/config.json" docker run --rm --use-api-socket alpine cat /run/secrets/docker/config.json cat: can't open '/run/secrets/docker/config.json': No such file or directory echo '{"auths": {}}' > "${DOCKER_CONFIG}/config.json" docker run --rm --use-api-socket alpine cat /run/secrets/docker/config.json cat: can't open '/run/secrets/docker/config.json': No such file or directory echo '{"auths": {"https://index.docker.io/v1/": {"auth": "am9lam9lOmhlbGxv"}}}' > "${DOCKER_CONFIG}/config.json" docker run --rm --use-api-socket alpine cat /run/secrets/docker/config.json { "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv" } } } Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 79ab3cb commit 711fcae

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

cli/command/container/create.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -304,16 +304,17 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
304304
// If the DOCKER_CONFIG env var is already present, we assume the client knows
305305
// what they're doing and don't inject the creds.
306306
if !envvarPresent {
307-
// Set our special little location for the config file.
308-
containerCfg.Config.Env = append(containerCfg.Config.Env,
309-
"DOCKER_CONFIG="+path.Dir(dockerConfigPathInContainer))
310-
311307
// Resolve this here for later, ensuring we error our before we create the container.
312308
creds, err := dockerCli.ConfigFile().GetAllCredentials()
313309
if err != nil {
314310
return "", fmt.Errorf("resolving credentials failed: %w", err)
315311
}
316-
apiSocketCreds = creds // inject these after container creation.
312+
if len(creds) > 0 {
313+
// Set our special little location for the config file.
314+
containerCfg.Config.Env = append(containerCfg.Config.Env, "DOCKER_CONFIG="+path.Dir(dockerConfigPathInContainer))
315+
316+
apiSocketCreds = creds // inject these after container creation.
317+
}
317318
}
318319
}
319320

@@ -371,7 +372,7 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
371372
}
372373
err = containerIDFile.Write(containerID)
373374

374-
if options.useAPISocket && apiSocketCreds != nil {
375+
if options.useAPISocket && len(apiSocketCreds) > 0 {
375376
// Create a new config file with just the auth.
376377
newConfig := &configfile.ConfigFile{
377378
AuthConfigs: apiSocketCreds,

0 commit comments

Comments
 (0)