Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update /etc/hosts when linked container is restarted #7677

Merged
merged 4 commits into from
Aug 28, 2014
Merged

Update /etc/hosts when linked container is restarted #7677

merged 4 commits into from
Aug 28, 2014

Conversation

erikh
Copy link
Contributor

@erikh erikh commented Aug 22, 2014

Carry of #7092

closes #7092
fixes #6350

/cc @vieux

@@ -297,6 +297,9 @@ func (container *Container) Start() (err error) {
if err := container.initializeNetworking(); err != nil {
return err
}
if err := container.updateParentsHosts(); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we need to remove records from hosts on container stop.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think that’s necessary; the next time the container starts the file will be rewritten anyhow.

-Erik

On Aug 22, 2014, at 1:48 AM, Alexandr Morozov notifications@github.com wrote:

In daemon/container.go:

@@ -297,6 +297,9 @@ func (container *Container) Start() (err error) {
if err := container.initializeNetworking(); err != nil {
return err
}

  • if err := container.updateParentsHosts(); err != nil {
    I wonder if we need to remove records from hosts on container stop.


Reply to this email directly or view it on GitHub https://github.com/docker/docker/pull/7677/files#r16587815.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with @erikh

@LK4D4
Copy link
Contributor

LK4D4 commented Aug 22, 2014

TestHostsLinkedContainerUpdate fails every time for me.

}
defer os.RemoveAll(tmpdir)

_, err = runCommand(exec.Command(dockerBinary, "run", "-d", "--name", "c1", "busybox", "sleep", "5"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use runCommandWithOutput and print output in Fatal. Because now test failing for me and I have no idea why.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed

return err
}
for _, cid := range parents {
if cid != "0" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is some super deep nesting.
Could we change these ifs to be the other way

if cid == "0" {
  return nil
}

etc, on down the line.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You meant continue instead or return nil I think.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for if cid == "0" { continue }

@SvenDowideit
Copy link
Contributor

DocsLGTM - @jamtur01 @fredlf @ostezer

[Communication between containers](#between-containers). If the linked
container is restarted, it is likely to get a different IP address; to
cater for this, Docker then updates the child container's `/etc/hosts` file
`ALIAS` entry.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd reword [this] to something like this:

Communication between containers.
On restart, Docker may assign a different IP address to the linked containers.
To keep the link, Docker then updates the ALIAS entry in the recipient
(i.e., child) container's /etc/hosts file.

Or:

Communication between containers.
Docker updates the ALIAS entry in the /etc/hosts file of the recipient
containers (i.e., children) in order to keep the link since Docker may
assign a different IP address to the linked containers on restart.

Please note that "to keep the link", however, might not be the correct term to use here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the second one better, I'll be using that.

@erikh
Copy link
Contributor Author

erikh commented Aug 25, 2014

@LK4D4, can you verify the errors are still happening?

Otherwise, I believe I have addressed all issues docs and otherwise.

@LK4D4
Copy link
Contributor

LK4D4 commented Aug 25, 2014

I'll try in a few minutes.

@LK4D4
Copy link
Contributor

LK4D4 commented Aug 25, 2014

@erikh all working OK now. Thank you.
LGTM

c := container.daemon.Get(cid)
if c != nil && !container.daemon.config.DisableNetwork && !container.hostConfig.NetworkMode.IsContainer() && !container.hostConfig.NetworkMode.IsHost() {
if err := etchosts.Update(c.HostsPath, container.NetworkSettings.IPAddress, container.Name[1:]); err != nil {
return fmt.Errorf("Failed to update parent: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error message could be more descriptive: Failed to update /etc/hosts of parent container.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fixed now. Thanks!

-Erik

On Aug 25, 2014, at 11:32 AM, Tibor Vass notifications@github.com wrote:

In daemon/container.go:

@@ -878,6 +886,26 @@ func (container *Container) setupContainerDns() error {
return ioutil.WriteFile(container.ResolvConfPath, resolvConf, 0644)
}

+func (container *Container) updateParentsHosts() error {

  • parents, err := container.daemon.Parents(container.Name)
  • if err != nil {
  •   return err
    
  • }
  • for _, cid := range parents {
  •   if cid == "0" {
    
  •       continue
    
  •   }
    
  •   c := container.daemon.Get(cid)
    
  •   if c != nil && !container.daemon.config.DisableNetwork && !container.hostConfig.NetworkMode.IsContainer() && !container.hostConfig.NetworkMode.IsHost() {
    
  •       if err := etchosts.Update(c.HostsPath, container.NetworkSettings.IPAddress, container.Name[1:]); err != nil {
    
  •           return fmt.Errorf("Failed to update parent: %v", err)
    
    Error message could be more descriptive: Failed to update /etc/hosts of parent container.


Reply to this email directly or view it on GitHub.

@tiborvass
Copy link
Contributor

@erikh Your DCO has vieux's name and email :)

@erikh
Copy link
Contributor Author

erikh commented Aug 25, 2014

It's also committed by @vieux :) It's his patch, I tried to preserve it.

@tiborvass
Copy link
Contributor

@erikh Sorry, I meant that, there are 2 DCOs (1 for vieux and 1 for you), and the 2nd one (yours) has still vieux's name and email.

@erikh
Copy link
Contributor Author

erikh commented Aug 25, 2014

Should be fixed now @tiborvass. Thanks.

@tiborvass
Copy link
Contributor

LGTM

[Communication between containers](#between-containers).
[Communication between containers](#between-containers). Docker updates
the ALIAS entry in the /etc/hosts file of the recipient containers (i.e.,
children) in order to keep the link since Docker may assign a different IP
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we want to remove references to parent/child in docs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

@crosbymichael
Copy link
Contributor

Functionally looks good. Just need another commit with some unit tests for the Update and Parents functions in graphd and the network code

@erikh
Copy link
Contributor Author

erikh commented Aug 26, 2014

Updated, PTAL.

On Aug 25, 2014, at 4:23 PM, Michael Crosby notifications@github.com wrote:

Functionally looks good. Just need another commit with some unit tests for the Update and Parents functions in graphd and the network code


Reply to this email directly or view it on GitHub.

[Communication between containers](#between-containers). Docker updates
the ALIAS entry in the /etc/hosts file of the recipient containers
in order to keep the link since Docker may assign a different IP
address to the linked containers on restart.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should flip this sentence so the context is at the beginning where it can be more helpful. So,

Because Docker may assign a different IP address to the linked containers on restart, Docker updates the ALIAS entry in the /etc/hosts file of the recipient containers.

@fredlf
Copy link
Contributor

fredlf commented Aug 26, 2014

Docs LGTM once my minor comment is addressed.

@erikh
Copy link
Contributor Author

erikh commented Aug 26, 2014

@fredlf, @crosbymichael, PTAL

On Aug 26, 2014, at 10:06 AM, Fred Lifton notifications@github.com wrote:

Docs LGTM once my minor comment is addressed.


Reply to this email directly or view it on GitHub.

@crosbymichael
Copy link
Contributor

@erikh needs rebased

Docker-DCO-1.1-Signed-off-by: Victor Vieux <vieux@docker.com> (github: vieux)
Erik Hollensbe added 3 commits August 27, 2014 18:23
Docker-DCO-1.1-Signed-off-by: Erik Hollensbe <github@hollensbe.org> (github: erikh)
Docker-DCO-1.1-Signed-off-by: Erik Hollensbe <github@hollensbe.org> (github: erikh)
Docker-DCO-1.1-Signed-off-by: Erik Hollensbe <github@hollensbe.org> (github: erikh)
This was referenced Aug 28, 2014
@vieux
Copy link
Contributor

vieux commented Aug 28, 2014

@erikh, @crosbymichael said it needs rebased

@vieux
Copy link
Contributor

vieux commented Aug 28, 2014

👿

@erikh
Copy link
Contributor Author

erikh commented Aug 28, 2014

rebased
On Aug 27, 2014, at 7:37 PM, Victor Vieux notifications@github.com wrote:


Reply to this email directly or view it on GitHub.

@vieux
Copy link
Contributor

vieux commented Aug 28, 2014

LGTM

@vieux
Copy link
Contributor

vieux commented Aug 28, 2014

@crosbymichael please merge

@crosbymichael
Copy link
Contributor

LGTM

crosbymichael added a commit that referenced this pull request Aug 28, 2014
Update /etc/hosts when linked container is restarted
@crosbymichael crosbymichael merged commit 2a5e29a into moby:master Aug 28, 2014
@vieux
Copy link
Contributor

vieux commented Aug 29, 2014

In our next release, 1.3

On Fri, Aug 29, 2014 at 12:44 AM, omeid notifications@github.com wrote:

When is this going to be tagged and released?


Reply to this email directly or view it on GitHub
#7677 (comment).

Victor VIEUX
http://vvieux.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

/etc/hosts not updated after a linked container is restarted
9 participants