diff --git a/daemon/mgr/container.go b/daemon/mgr/container.go index 4d22f55bb..3ea14938f 100644 --- a/daemon/mgr/container.go +++ b/daemon/mgr/container.go @@ -583,8 +583,7 @@ func (mgr *ContainerManager) prepareContainerNetwork(ctx context.Context, c *Con } for name, endpointSetting := range c.NetworkSettings.Networks { - endpoint := mgr.buildContainerEndpoint(c) - endpoint.Name = name + endpoint := mgr.buildContainerEndpoint(c, name) endpoint.EndpointConfig = endpointSetting if _, err := mgr.NetworkMgr.EndpointCreate(ctx, endpoint); err != nil { logrus.Errorf("failed to create endpoint: %v", err) @@ -1372,9 +1371,8 @@ func (mgr *ContainerManager) Disconnect(ctx context.Context, containerName, netw } c.Lock() - endpoint := mgr.buildContainerEndpoint(c) + endpoint := mgr.buildContainerEndpoint(c, network.Name) c.Unlock() - endpoint.Name = network.Name endpoint.EndpointConfig = epConfig if err := mgr.NetworkMgr.EndpointRemove(ctx, endpoint); err != nil { // TODO(ziren): it is a trick, we should wrapper sanbox @@ -1484,9 +1482,8 @@ func (mgr *ContainerManager) connectToNetwork(ctx context.Context, container *Co } container.Lock() - endpoint := mgr.buildContainerEndpoint(container) + endpoint := mgr.buildContainerEndpoint(container, network.Name) container.Unlock() - endpoint.Name = network.Name endpoint.EndpointConfig = epConfig if _, err := mgr.NetworkMgr.EndpointCreate(ctx, endpoint); err != nil { logrus.Errorf("failed to create endpoint: %v", err) @@ -1780,8 +1777,7 @@ func (mgr *ContainerManager) releaseContainerNetwork(c *Container) error { } for name, epConfig := range c.NetworkSettings.Networks { - endpoint := mgr.buildContainerEndpoint(c) - endpoint.Name = name + endpoint := mgr.buildContainerEndpoint(c, name) endpoint.EndpointConfig = epConfig if err := mgr.NetworkMgr.EndpointRemove(context.Background(), endpoint); err != nil { // TODO(ziren): it is a trick, we should wrapper "sanbox @@ -1811,8 +1807,13 @@ func (mgr *ContainerManager) releaseContainerIOs(containerID string) { // buildContainerEndpoint builds Endpoints according to container // caller should lock container when calling this func. -func (mgr *ContainerManager) buildContainerEndpoint(c *Container) *networktypes.Endpoint { +func (mgr *ContainerManager) buildContainerEndpoint(c *Container, name string) *networktypes.Endpoint { ep := BuildContainerEndpoint(c) + ep.Name = name + + if !IsUserDefined(name) { + ep.DisableResolver = true + } if mgr.containerPlugin != nil { ep.Priority, ep.DisableResolver, ep.GenericParams = mgr.containerPlugin.PreCreateEndpoint(c.ID, c.Config.Env) diff --git a/daemon/mgr/network_utils.go b/daemon/mgr/network_utils.go index ab0ccbbd7..9b0092be5 100644 --- a/daemon/mgr/network_utils.go +++ b/daemon/mgr/network_utils.go @@ -30,9 +30,9 @@ func IsBridge(mode string) bool { return mode == "bridge" } -// IsUserDefined is used to check if network mode is bridge mode. +// IsUserDefined is used to check if network mode is user-created. func IsUserDefined(mode string) bool { - return !isContainer(mode) && !isHost(mode) && isNone(mode) + return !IsBridge(mode) && !IsContainer(mode) && !IsHost(mode) && !IsNone(mode) } // IsDefault indicates whether container uses the default network stack. diff --git a/test/cli_run_dns_test.go b/test/cli_run_dns_test.go new file mode 100644 index 000000000..53eabd733 --- /dev/null +++ b/test/cli_run_dns_test.go @@ -0,0 +1,68 @@ +package main + +import ( + "strings" + + "github.com/alibaba/pouch/test/command" + "github.com/alibaba/pouch/test/environment" + + "github.com/go-check/check" + "github.com/gotestyourself/gotestyourself/icmd" +) + +// PouchRunDNSSuite is the test suite for run CLI. +type PouchRunDNSSuite struct{} + +func init() { + check.Suite(&PouchRunDNSSuite{}) +} + +// SetUpSuite does common setup in the beginning of each test suite. +func (suite *PouchRunDNSSuite) SetUpSuite(c *check.C) { + SkipIfFalse(c, environment.IsLinux) + + environment.PruneAllContainers(apiClient) + + PullImage(c, busyboxImage) +} + +// TearDownTest does cleanup work in the end of each test. +func (suite *PouchRunDNSSuite) TearDownTest(c *check.C) { +} + +// TestRunWithUserDefinedNetwork tests enabling libnetwork resolver if user-defined network. +func (suite *PouchRunSuite) TestRunWithUserDefinedNetwork(c *check.C) { + cname := "TestRunWithUserDefinedNetwork" + + // Create a user-defined network + command.PouchRun("network", "create", "--name", cname, + "-d", "bridge", + "--gateway", GateWay, + "--subnet", Subnet).Assert(c, icmd.Success) + defer command.PouchRun("network", "remove", cname) + + // Assign the user-defined network to a container + res := command.PouchRun("run", "--name", cname, + "--net", cname, busyboxImage, + "cat", "/etc/resolv.conf") + res.Assert(c, icmd.Success) + defer DelContainerForceMultyTime(c, cname) + + c.Assert(strings.Contains(res.Stdout(), "nameserver 127.0.0.11"), check.Equals, true) +} + +// TestRunWithBridgeNetwork tests disabling libnetwork resolver if not user-defined network. +func (suite *PouchRunSuite) TestRunWithBridgeNetwork(c *check.C) { + cname := "TestRunWithBridgeNetwork" + + // Use bridge network if not set --net. + res := command.PouchRun("run", "--name", cname, busyboxImage, + "cat", "/etc/resolv.conf") + res.Assert(c, icmd.Success) + defer DelContainerForceMultyTime(c, cname) + + hostRes := icmd.RunCommand("cat", "/etc/resolv.conf") + hostRes.Assert(c, icmd.Success) + + c.Assert(res.Stdout(), check.Equals, hostRes.Stdout()) +}