fix(start): give a started use_api_socket service its config file

A container created beforehand and only started by `compose start`
(`create` then `start`, `up --no-start` then `start`, or a dependency named
to `start`) came up with DOCKER_CONFIG pointing at /run/secrets/docker and no
file there: use_api_socket is applied by rewriting the project, and the
config file it adds is injected when a container starts, from the project the
start works on. Create and run applied the rewriting, start did not.

start() now applies it too, on the copy it already makes of the project, so
the caller's is left alone, and a project already rewritten (a dependency
started by run) is not rewritten twice. The rewriting tolerates a project
without environment or configs maps, as the one `compose start` loads.

up -d, a foreground up, scale and run already provided the file.

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2026-10-26 16:46:22 +01:00 • committed by Nicolas De loof
parent 4e552c69b2
commit 821153b2e4
5 changed files with 113 additions and 0 deletions

View file

@ -65,6 +65,11 @@ func (s *composeService) useAPISocket(project *types.Project) (*types.Project, e
return nil, fmt.Errorf("saving creds for API socket: %w", err)
}
// a project that did not go through environment resolution (the one
// `compose start` loads) has no maps to write into
if project.Configs == nil {
project.Configs = types.Configs{}
}
project.Configs["#apisocket"] = types.ConfigObjConfig{
Content: configBuf.String(),
}
@ -79,6 +84,9 @@ func (s *composeService) useAPISocket(project *types.Project) (*types.Project, e
Target: "/var/run/docker.sock",
})
if service.Environment == nil {
service.Environment = types.MappingWithEquals{}
}
_, envvarPresent := service.Environment["DOCKER_CONFIG"]
// If the DOCKER_CONFIG env var is already present, we assume the client knows

View file

@ -60,6 +60,17 @@ func (s *composeService) start(ctx context.Context, projectName string, options
// and the dependency waits read them
project = project.WithoutUnresolvedOptionalDependencies()
// use_api_socket is applied by rewriting the project (see useAPISocket),
// and the config file it adds is injected when a container starts, from the
// project given to the start: a container created beforehand and only
// started here would otherwise get DOCKER_CONFIG without the file it points
// to. The project is the copy made above, so the caller's is left alone, and
// one already transformed (a dependency started by `run`) is not done twice.
project, err := s.useAPISocket(project)
if err != nil {
return err
}
observed, err := s.collectObservedState(ctx, project, ScopeStart)
if err != nil {
return err

View file

@ -19,7 +19,9 @@
package compose
import (
"archive/tar"
"context"
"io"
"strconv"
"sync"
"testing"
@ -188,6 +190,51 @@ func TestStart_WaitTimeoutBoundsDependencyWaits(t *testing.T) {
assert.Error(t, err, "timeout waiting for dependencies")
}
// TestStart_UseAPISocketServiceGetsItsConfigFile pins that start() applies
// use_api_socket to the project it works on: the config file the option adds is
// injected when a container starts, from that project, so a container created
// beforehand and only started here (`create` then `start`) would otherwise get
// DOCKER_CONFIG pointing at a file that was never created. The caller's project
// is left as it was.
func TestStart_UseAPISocketServiceGetsItsConfigFile(t *testing.T) {
svc, apiClient, _ := newStartTestService(t)
svc.contextInfo = linuxContextInfo{}
// no environment and no configs map, as in the project `compose start`
// loads, which does not resolve the environment
web := types.ServiceConfig{Name: "web", ContainerSpec: types.ContainerSpec{Image: "alpine", UseAPISocket: true}}
project := &types.Project{Name: "prj", Services: types.Services{"web": web}}
ctr := serviceContainer("web")
ctr.State = container.StateCreated
ctr.Labels[api.ProjectLabel] = "prj"
apiClient.EXPECT().ContainerList(gomock.Any(), gomock.Any()).
Return(client.ContainerListResult{Items: []container.Summary{ctr}}, nil)
var injected []string
copied := apiClient.EXPECT().CopyToContainer(gomock.Any(), ctr.ID, gomock.Any()).
DoAndReturn(func(_ context.Context, _ string, opts client.CopyToContainerOptions) (client.CopyToContainerResult, error) {
tr := tar.NewReader(opts.Content)
for {
header, err := tr.Next()
if err == io.EOF {
break
}
assert.NilError(t, err)
injected = append(injected, header.Name)
}
return client.CopyToContainerResult{}, nil
})
apiClient.EXPECT().ContainerStart(gomock.Any(), ctr.ID, gomock.Any()).
Return(client.ContainerStartResult{}, nil).After(copied)
err := svc.start(t.Context(), "prj", api.StartOptions{Project: project})
assert.NilError(t, err)
assert.DeepEqual(t, injected, []string{"/run/secrets/docker/config.json"})
assert.Assert(t, project.Configs == nil && project.Services["web"].Environment == nil, "the caller's project must not be transformed")
}
// TestGetDependencyCondition locks the --wait condition selection: a service
// that others depend on with service_completed_successfully is waited on with
// that condition (or --wait would hang on one-shot services), anything else

View file

@ -0,0 +1,41 @@
//go:build e2e
/*
Copyright 2026 Docker Compose CLI authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package e2e
import (
"testing"
"time"
)
// Regression test for https://github.com/docker/compose/issues/14386: a
// container created beforehand and only started by `compose start` got
// DOCKER_CONFIG pointing at /run/secrets/docker, without the config file the
// use_api_socket option is meant to provide there. The file is injected when
// the container starts, from the project given to the start, which did not
// carry the option.
func TestStartCreatedUseAPISocketService(t *testing.T) {
NewScenario(t, "start must provide the use_api_socket config file to a container created beforehand").
Step("create makes the container without starting it",
ComposeCmd("create"),
ServiceState("app", "created")).
Step("start runs it with the config file in place",
ComposeCmd("start"),
Eventually(ServiceState("app", "running"), 10*time.Second),
ExecOutputContains("app", "[ -f /run/secrets/docker/config.json ] && echo apisocket-config-present", "apisocket-config-present"))
}

View file

@ -0,0 +1,6 @@
services:
app:
image: alpine
init: true
use_api_socket: true
command: sleep infinity