1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Fix endpoint leave failure for --net=host mode

When a container is started with `--net=host` with
a particular name and it is subsequently destroyed,
then all subsequent creations of the container with
the same name will fail. This is because in `--net=host`
the namespace is shared i.e the host namespace so
trying to destroy the host namespace by calling
`LeaveAll` will fail and the endpoint is left with
the dangling state. So the fix is, for this mode, do
not attempt to destroy the namespace but just cleanup
the endpoint state and return.

Signed-off-by: Jana Radhakrishnan <mrjana@docker.com>
This commit is contained in:
Jana Radhakrishnan 2015-06-24 10:23:00 -07:00
parent afd4d544cf
commit 9bb69f9726
5 changed files with 66 additions and 23 deletions

View file

@ -1003,33 +1003,47 @@ func (container *Container) ReleaseNetwork() {
return
}
err := container.daemon.netController.LeaveAll(container.ID)
if err != nil {
logrus.Errorf("Leave all failed for %s: %v", container.ID, err)
return
}
eid := container.NetworkSettings.EndpointID
nid := container.NetworkSettings.NetworkID
container.NetworkSettings = &network.Settings{}
// In addition to leaving all endpoints, delete implicitly created endpoint
if container.Config.PublishService == "" && eid != "" && nid != "" {
if nid == "" || eid == "" {
return
}
n, err := container.daemon.netController.NetworkByID(nid)
if err != nil {
logrus.Errorf("error locating network id %s: %v", nid, err)
return
}
ep, err := n.EndpointByID(eid)
if err != nil {
logrus.Errorf("error locating endpoint id %s: %v", eid, err)
return
}
switch {
case container.hostConfig.NetworkMode.IsHost():
if err := ep.Leave(container.ID); err != nil {
logrus.Errorf("Error leaving endpoint id %s for container %s: %v", eid, container.ID, err)
return
}
default:
if err := container.daemon.netController.LeaveAll(container.ID); err != nil {
logrus.Errorf("Leave all failed for %s: %v", container.ID, err)
return
}
}
// In addition to leaving all endpoints, delete implicitly created endpoint
if container.Config.PublishService == "" {
if err := ep.Delete(); err != nil {
logrus.Errorf("deleting endpoint failed: %v", err)
}
}
}
func disableAllActiveLinks(container *Container) {

View file

@ -18,7 +18,7 @@ clone git golang.org/x/net 3cffabab72adf04f8e3b01c5baf775361837b5fe https://gith
clone hg code.google.com/p/gosqlite 74691fb6f837
#get libnetwork packages
clone git github.com/docker/libnetwork 1aaf1047fd48345619a875184538a0eb6c6cfb2a
clone git github.com/docker/libnetwork 82a1f5634904b57e619fd715ded6903727e00143
clone git github.com/armon/go-metrics eb0af217e5e9747e41dd5303755356b62d28e3ec
clone git github.com/hashicorp/go-msgpack 71c2886f5a673a35f909803f38ece5810165097b
clone git github.com/hashicorp/memberlist 9a1e242e454d2443df330bdd51a436d5a9058fc4

View file

@ -2820,6 +2820,22 @@ func (s *DockerSuite) TestRunNetHost(c *check.C) {
}
}
func (s *DockerSuite) TestRunNetHostTwiceSameName(c *check.C) {
testRequires(c, SameHostDaemon)
cmd := exec.Command(dockerBinary, "run", "--rm", "--name=thost", "--net=host", "busybox", "true")
out2, _, err := runCommandWithOutput(cmd)
if err != nil {
c.Fatal(err, out2)
}
cmd = exec.Command(dockerBinary, "run", "--rm", "--name=thost", "--net=host", "busybox", "true")
out2, _, err = runCommandWithOutput(cmd)
if err != nil {
c.Fatal(err, out2)
}
}
func (s *DockerSuite) TestRunNetContainerWhichHost(c *check.C) {
testRequires(c, SameHostDaemon)

View file

@ -68,6 +68,10 @@ func Build(path, IP, hostname, domainname string, extraContent []Record) error {
// Add adds an arbitrary number of Records to an already existing /etc/hosts file
func Add(path string, recs []Record) error {
if len(recs) == 0 {
return nil
}
f, err := os.Open(path)
if err != nil {
return err
@ -91,6 +95,10 @@ func Add(path string, recs []Record) error {
// Delete deletes an arbitrary number of Records already existing in /etc/hosts file
func Delete(path string, recs []Record) error {
if len(recs) == 0 {
return nil
}
old, err := ioutil.ReadFile(path)
if err != nil {
return err

View file

@ -416,6 +416,11 @@ func (n *network) updateSvcRecord(ep *endpoint, isAdd bool) {
}
n.Unlock()
// If there are no records to add or delete then simply return here
if len(recs) == 0 {
return
}
var epList []*endpoint
n.WalkEndpoints(func(e Endpoint) bool {
cEp := e.(*endpoint)