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

Commit 6b737ad

Browse files
committed
Return "invalid parameter" when linking to non-existing container
Trying to link to a non-existing container is not valid, and should return an "invalid parameter" (400) error. Returning a "not found" error in this situation would make the client report the container's image could not be found. Signed-off-by: Sebastiaan van Stijn <github@gone.nl> (cherry picked from commit 422067ba7b9177753642f6a784b2e68f8bc9549e) Signed-off-by: Sebastiaan van Stijn <github@gone.nl> Upstream-commit: 1e0234ddc6c6df66f371addcb3494faa73f69264 Component: engine
1 parent 54f4ad7 commit 6b737ad

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"
@@ -1290,12 +1291,26 @@ func (daemon *Daemon) registerLinks(container *container.Container, hostConfig *
12901291
}
12911292
child, err := daemon.GetContainer(name)
12921293
if err != nil {
1294+
if errdefs.IsNotFound(err) {
1295+
// Trying to link to a non-existing container is not valid, and
1296+
// should return an "invalid parameter" error. Returning a "not
1297+
// found" error here would make the client report the container's
1298+
// image could not be found (see moby/moby#39823)
1299+
err = errdefs.InvalidParameter(err)
1300+
}
12931301
return errors.Wrapf(err, "could not get container for %s", name)
12941302
}
12951303
for child.HostConfig.NetworkMode.IsContainer() {
12961304
parts := strings.SplitN(string(child.HostConfig.NetworkMode), ":", 2)
12971305
child, err = daemon.GetContainer(parts[1])
12981306
if err != nil {
1307+
if errdefs.IsNotFound(err) {
1308+
// Trying to link to a non-existing container is not valid, and
1309+
// should return an "invalid parameter" error. Returning a "not
1310+
// found" error here would make the client report the container's
1311+
// image could not be found (see moby/moby#39823)
1312+
err = errdefs.InvalidParameter(err)
1313+
}
12991314
return errors.Wrapf(err, "Could not get container for %s", parts[1])
13001315
}
13011316
}

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)