Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit d872884

Browse files
authored
Merge pull request #352 from thaJeztah/19.03_backport_detect_invalid_linked_container
[19.03 backport] Return "invalid parameter" when linking to non-existing container Upstream-commit: 2aa5322638a6a8dc0f5d1c8e37dc6ca3904328b5 Component: engine
2 parents f878290 + 6b737ad commit d872884

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

components/engine/daemon/daemon_unix.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/docker/docker/container"
2525
"github.com/docker/docker/daemon/config"
2626
"github.com/docker/docker/daemon/initlayer"
27+
"github.com/docker/docker/errdefs"
2728
"github.com/docker/docker/opts"
2829
"github.com/docker/docker/pkg/containerfs"
2930
"github.com/docker/docker/pkg/idtools"
@@ -1294,12 +1295,26 @@ func (daemon *Daemon) registerLinks(container *container.Container, hostConfig *
12941295
}
12951296
child, err := daemon.GetContainer(name)
12961297
if err != nil {
1298+
if errdefs.IsNotFound(err) {
1299+
// Trying to link to a non-existing container is not valid, and
1300+
// should return an "invalid parameter" error. Returning a "not
1301+
// found" error here would make the client report the container's
1302+
// image could not be found (see moby/moby#39823)
1303+
err = errdefs.InvalidParameter(err)
1304+
}
12971305
return errors.Wrapf(err, "could not get container for %s", name)
12981306
}
12991307
for child.HostConfig.NetworkMode.IsContainer() {
13001308
parts := strings.SplitN(string(child.HostConfig.NetworkMode), ":", 2)
13011309
child, err = daemon.GetContainer(parts[1])
13021310
if err != nil {
1311+
if errdefs.IsNotFound(err) {
1312+
// Trying to link to a non-existing container is not valid, and
1313+
// should return an "invalid parameter" error. Returning a "not
1314+
// found" error here would make the client report the container's
1315+
// image could not be found (see moby/moby#39823)
1316+
err = errdefs.InvalidParameter(err)
1317+
}
13031318
return errors.Wrapf(err, "Could not get container for %s", parts[1])
13041319
}
13051320
}

components/engine/integration/container/create_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,28 @@ func TestCreateFailsWhenIdentifierDoesNotExist(t *testing.T) {
6565
}
6666
}
6767

68+
// TestCreateLinkToNonExistingContainer verifies that linking to a non-existing
69+
// container returns an "invalid parameter" (400) status, and not the underlying
70+
// "non exists" (404).
71+
func TestCreateLinkToNonExistingContainer(t *testing.T) {
72+
skip.If(t, testEnv.DaemonInfo.OSType == "windows", "legacy links are not supported on windows")
73+
defer setupTest(t)()
74+
c := testEnv.APIClient()
75+
76+
_, err := c.ContainerCreate(context.Background(),
77+
&container.Config{
78+
Image: "busybox",
79+
},
80+
&container.HostConfig{
81+
Links: []string{"no-such-container"},
82+
},
83+
&network.NetworkingConfig{},
84+
"",
85+
)
86+
assert.Check(t, is.ErrorContains(err, "could not get container for no-such-container"))
87+
assert.Check(t, errdefs.IsInvalidParameter(err))
88+
}
89+
6890
func TestCreateWithInvalidEnv(t *testing.T) {
6991
defer setupTest(t)()
7092
client := testEnv.APIClient()

0 commit comments

Comments
 (0)