Stable Networking: Keep the same network settings across container restarts.

This change will allocate network settings (IP and public ports) at
container creation rather than start and keep them throughout the
lifetime of the container (i.e. until it gets destroyed) instead of
discarding them when the container is stopped.

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi
2014-10-03 13:46:24 -07:00
parent deffc572ce
commit a487593729
4 changed files with 87 additions and 47 deletions
+9 -5
View File
@@ -552,7 +552,7 @@ func (container *Container) RestoreNetwork() error {
}
// Re-allocate any previously allocated ports.
for port, _ := range container.NetworkSettings.Ports {
for port := range container.NetworkSettings.Ports {
if err := container.allocatePort(eng, port, container.NetworkSettings.Ports); err != nil {
return err
}
@@ -563,8 +563,6 @@ func (container *Container) RestoreNetwork() error {
// cleanup releases any network resources allocated to the container along with any rules
// around how containers are linked together. It also unmounts the container's root filesystem.
func (container *Container) cleanup() {
container.ReleaseNetwork()
// Disable all active links
if container.activeLinks != nil {
for _, link := range container.activeLinks {
@@ -1008,8 +1006,14 @@ func (container *Container) initializeNetworking() error {
container.Config.NetworkDisabled = true
return container.buildHostnameAndHostsFiles("127.0.1.1")
}
if err := container.AllocateNetwork(); err != nil {
return err
// Backward compatibility:
// Network allocation used to be done when containers started, not when they
// were created, therefore we might be starting a legacy container that
// doesn't have networking.
if !container.isNetworkAllocated() {
if err := container.AllocateNetwork(); err != nil {
return err
}
}
return container.buildHostnameAndHostsFiles(container.NetworkSettings.IPAddress)
}
+7 -3
View File
@@ -83,6 +83,9 @@ func (daemon *Daemon) Create(config *runconfig.Config, hostConfig *runconfig.Hos
if container, err = daemon.newContainer(name, config, img); err != nil {
return nil, nil, err
}
if err := daemon.Register(container); err != nil {
return nil, nil, err
}
if err := daemon.createRootfs(container, img); err != nil {
return nil, nil, err
}
@@ -90,12 +93,13 @@ func (daemon *Daemon) Create(config *runconfig.Config, hostConfig *runconfig.Hos
if err := daemon.setHostConfig(container, hostConfig); err != nil {
return nil, nil, err
}
// We may only allocate the network if a host config was passed, otherwise we'll miss port mappings.
if err := container.AllocateNetwork(); err != nil {
return nil, nil, err
}
}
if err := container.ToDisk(); err != nil {
return nil, nil, err
}
if err := daemon.Register(container); err != nil {
return nil, nil, err
}
return container, warnings, nil
}
+2
View File
@@ -94,6 +94,8 @@ func (daemon *Daemon) Destroy(container *Container) error {
return err
}
container.ReleaseNetwork()
// Deregister the container before removing its directory, to avoid race conditions
daemon.idIndex.Delete(container.ID)
daemon.containers.Delete(container.ID)