mirror of https://github.com/docker/docs.git
Merge pull request #21901 from mavenugo/sid
Add container's short-id as default network alias
This commit is contained in:
commit
8adc8c3a68
|
@ -12,6 +12,7 @@ import (
|
||||||
"github.com/docker/docker/container"
|
"github.com/docker/docker/container"
|
||||||
"github.com/docker/docker/daemon/network"
|
"github.com/docker/docker/daemon/network"
|
||||||
derr "github.com/docker/docker/errors"
|
derr "github.com/docker/docker/errors"
|
||||||
|
"github.com/docker/docker/pkg/stringid"
|
||||||
"github.com/docker/docker/runconfig"
|
"github.com/docker/docker/runconfig"
|
||||||
containertypes "github.com/docker/engine-api/types/container"
|
containertypes "github.com/docker/engine-api/types/container"
|
||||||
networktypes "github.com/docker/engine-api/types/network"
|
networktypes "github.com/docker/engine-api/types/network"
|
||||||
|
@ -485,6 +486,18 @@ func (daemon *Daemon) updateNetworkConfig(container *container.Container, idOrNa
|
||||||
if endpointConfig != nil && len(endpointConfig.Aliases) > 0 {
|
if endpointConfig != nil && len(endpointConfig.Aliases) > 0 {
|
||||||
return nil, runconfig.ErrUnsupportedNetworkAndAlias
|
return nil, runconfig.ErrUnsupportedNetworkAndAlias
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
addShortID := true
|
||||||
|
shortID := stringid.TruncateID(container.ID)
|
||||||
|
for _, alias := range endpointConfig.Aliases {
|
||||||
|
if alias == shortID {
|
||||||
|
addShortID = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if addShortID {
|
||||||
|
endpointConfig.Aliases = append(endpointConfig.Aliases, shortID)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
n, err := daemon.FindNetwork(idOrName)
|
n, err := daemon.FindNetwork(idOrName)
|
||||||
|
@ -505,6 +518,9 @@ func (daemon *Daemon) updateNetworkConfig(container *container.Container, idOrNa
|
||||||
}
|
}
|
||||||
|
|
||||||
func (daemon *Daemon) connectToNetwork(container *container.Container, idOrName string, endpointConfig *networktypes.EndpointSettings, updateSettings bool) (err error) {
|
func (daemon *Daemon) connectToNetwork(container *container.Container, idOrName string, endpointConfig *networktypes.EndpointSettings, updateSettings bool) (err error) {
|
||||||
|
if endpointConfig == nil {
|
||||||
|
endpointConfig = &networktypes.EndpointSettings{}
|
||||||
|
}
|
||||||
n, err := daemon.updateNetworkConfig(container, idOrName, endpointConfig, updateSettings)
|
n, err := daemon.updateNetworkConfig(container, idOrName, endpointConfig, updateSettings)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -533,10 +549,7 @@ func (daemon *Daemon) connectToNetwork(container *container.Container, idOrName
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
container.NetworkSettings.Networks[n.Name()] = endpointConfig
|
||||||
if endpointConfig != nil {
|
|
||||||
container.NetworkSettings.Networks[n.Name()] = endpointConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := daemon.updateEndpointNetworkSettings(container, n, ep); err != nil {
|
if err := daemon.updateEndpointNetworkSettings(container, n, ep); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -101,6 +101,9 @@ func (daemon *Daemon) getSize(container *container.Container) (int64, int64) {
|
||||||
|
|
||||||
// ConnectToNetwork connects a container to a network
|
// ConnectToNetwork connects a container to a network
|
||||||
func (daemon *Daemon) ConnectToNetwork(container *container.Container, idOrName string, endpointConfig *networktypes.EndpointSettings) error {
|
func (daemon *Daemon) ConnectToNetwork(container *container.Container, idOrName string, endpointConfig *networktypes.EndpointSettings) error {
|
||||||
|
if endpointConfig == nil {
|
||||||
|
endpointConfig = &networktypes.EndpointSettings{}
|
||||||
|
}
|
||||||
if !container.Running {
|
if !container.Running {
|
||||||
if container.RemovalInProgress || container.Dead {
|
if container.RemovalInProgress || container.Dead {
|
||||||
return errRemovalContainer(container.ID)
|
return errRemovalContainer(container.ID)
|
||||||
|
@ -108,9 +111,7 @@ func (daemon *Daemon) ConnectToNetwork(container *container.Container, idOrName
|
||||||
if _, err := daemon.updateNetworkConfig(container, idOrName, endpointConfig, true); err != nil {
|
if _, err := daemon.updateNetworkConfig(container, idOrName, endpointConfig, true); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if endpointConfig != nil {
|
container.NetworkSettings.Networks[idOrName] = endpointConfig
|
||||||
container.NetworkSettings.Networks[idOrName] = endpointConfig
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if err := daemon.connectToNetwork(container, idOrName, endpointConfig, true); err != nil {
|
if err := daemon.connectToNetwork(container, idOrName, endpointConfig, true); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -14,6 +14,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/docker/docker/pkg/integration/checker"
|
"github.com/docker/docker/pkg/integration/checker"
|
||||||
|
"github.com/docker/docker/pkg/stringid"
|
||||||
"github.com/docker/docker/runconfig"
|
"github.com/docker/docker/runconfig"
|
||||||
"github.com/docker/engine-api/types"
|
"github.com/docker/engine-api/types"
|
||||||
"github.com/docker/engine-api/types/versions/v1p20"
|
"github.com/docker/engine-api/types/versions/v1p20"
|
||||||
|
@ -1348,7 +1349,7 @@ func (s *DockerSuite) TestUserDefinedNetworkConnectDisconnectAlias(c *check.C) {
|
||||||
dockerCmd(c, "network", "create", "-d", "bridge", "net1")
|
dockerCmd(c, "network", "create", "-d", "bridge", "net1")
|
||||||
dockerCmd(c, "network", "create", "-d", "bridge", "net2")
|
dockerCmd(c, "network", "create", "-d", "bridge", "net2")
|
||||||
|
|
||||||
dockerCmd(c, "run", "-d", "--net=net1", "--name=first", "--net-alias=foo", "busybox", "top")
|
cid, _ := dockerCmd(c, "run", "-d", "--net=net1", "--name=first", "--net-alias=foo", "busybox", "top")
|
||||||
c.Assert(waitRun("first"), check.IsNil)
|
c.Assert(waitRun("first"), check.IsNil)
|
||||||
|
|
||||||
dockerCmd(c, "run", "-d", "--net=net1", "--name=second", "busybox", "top")
|
dockerCmd(c, "run", "-d", "--net=net1", "--name=second", "busybox", "top")
|
||||||
|
@ -1360,6 +1361,10 @@ func (s *DockerSuite) TestUserDefinedNetworkConnectDisconnectAlias(c *check.C) {
|
||||||
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
|
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, check.IsNil)
|
||||||
|
|
||||||
|
// ping first container's short-id alias
|
||||||
|
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", stringid.TruncateID(cid))
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
|
||||||
// connect first container to net2 network
|
// connect first container to net2 network
|
||||||
dockerCmd(c, "network", "connect", "--alias=bar", "net2", "first")
|
dockerCmd(c, "network", "connect", "--alias=bar", "net2", "first")
|
||||||
// connect second container to foo2 network with a different alias for first container
|
// connect second container to foo2 network with a different alias for first container
|
||||||
|
@ -1379,6 +1384,9 @@ func (s *DockerSuite) TestUserDefinedNetworkConnectDisconnectAlias(c *check.C) {
|
||||||
// ping to net2 scoped alias "bar" must still succeed
|
// ping to net2 scoped alias "bar" must still succeed
|
||||||
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "bar")
|
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "bar")
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, check.IsNil)
|
||||||
|
// ping to net2 scoped alias short-id must still succeed
|
||||||
|
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", stringid.TruncateID(cid))
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
|
||||||
// verify the alias option is rejected when running on predefined network
|
// verify the alias option is rejected when running on predefined network
|
||||||
out, _, err := dockerCmdWithError("run", "--rm", "--name=any", "--net-alias=any", "busybox", "top")
|
out, _, err := dockerCmdWithError("run", "--rm", "--name=any", "--net-alias=any", "busybox", "top")
|
||||||
|
|
|
@ -20,6 +20,7 @@ import (
|
||||||
|
|
||||||
"github.com/docker/docker/pkg/integration/checker"
|
"github.com/docker/docker/pkg/integration/checker"
|
||||||
"github.com/docker/docker/pkg/mount"
|
"github.com/docker/docker/pkg/mount"
|
||||||
|
"github.com/docker/docker/pkg/stringid"
|
||||||
"github.com/docker/docker/pkg/stringutils"
|
"github.com/docker/docker/pkg/stringutils"
|
||||||
"github.com/docker/docker/runconfig"
|
"github.com/docker/docker/runconfig"
|
||||||
"github.com/docker/go-connections/nat"
|
"github.com/docker/go-connections/nat"
|
||||||
|
@ -285,12 +286,22 @@ func (s *DockerSuite) TestUserDefinedNetworkAlias(c *check.C) {
|
||||||
testRequires(c, DaemonIsLinux, NotUserNamespace, NotArm)
|
testRequires(c, DaemonIsLinux, NotUserNamespace, NotArm)
|
||||||
dockerCmd(c, "network", "create", "-d", "bridge", "net1")
|
dockerCmd(c, "network", "create", "-d", "bridge", "net1")
|
||||||
|
|
||||||
dockerCmd(c, "run", "-d", "--net=net1", "--name=first", "--net-alias=foo1", "--net-alias=foo2", "busybox", "top")
|
cid1, _ := dockerCmd(c, "run", "-d", "--net=net1", "--name=first", "--net-alias=foo1", "--net-alias=foo2", "busybox", "top")
|
||||||
c.Assert(waitRun("first"), check.IsNil)
|
c.Assert(waitRun("first"), check.IsNil)
|
||||||
|
|
||||||
dockerCmd(c, "run", "-d", "--net=net1", "--name=second", "busybox", "top")
|
// Check if default short-id alias is added automatically
|
||||||
|
id := strings.TrimSpace(cid1)
|
||||||
|
aliases := inspectField(c, id, "NetworkSettings.Networks.net1.Aliases")
|
||||||
|
c.Assert(aliases, checker.Contains, stringid.TruncateID(id))
|
||||||
|
|
||||||
|
cid2, _ := dockerCmd(c, "run", "-d", "--net=net1", "--name=second", "busybox", "top")
|
||||||
c.Assert(waitRun("second"), check.IsNil)
|
c.Assert(waitRun("second"), check.IsNil)
|
||||||
|
|
||||||
|
// Check if default short-id alias is added automatically
|
||||||
|
id = strings.TrimSpace(cid2)
|
||||||
|
aliases = inspectField(c, id, "NetworkSettings.Networks.net1.Aliases")
|
||||||
|
c.Assert(aliases, checker.Contains, stringid.TruncateID(id))
|
||||||
|
|
||||||
// ping to first and its network-scoped aliases
|
// ping to first and its network-scoped aliases
|
||||||
_, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
|
_, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, check.IsNil)
|
||||||
|
@ -298,6 +309,9 @@ func (s *DockerSuite) TestUserDefinedNetworkAlias(c *check.C) {
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, check.IsNil)
|
||||||
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo2")
|
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo2")
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, check.IsNil)
|
||||||
|
// ping first container's short-id alias
|
||||||
|
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", stringid.TruncateID(cid1))
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
|
||||||
// Restart first container
|
// Restart first container
|
||||||
dockerCmd(c, "restart", "first")
|
dockerCmd(c, "restart", "first")
|
||||||
|
@ -310,6 +324,9 @@ func (s *DockerSuite) TestUserDefinedNetworkAlias(c *check.C) {
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, check.IsNil)
|
||||||
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo2")
|
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo2")
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, check.IsNil)
|
||||||
|
// ping first container's short-id alias
|
||||||
|
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", stringid.TruncateID(cid1))
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Issue 9677.
|
// Issue 9677.
|
||||||
|
|
Loading…
Reference in New Issue