summaryrefslogtreecommitdiff
path: root/libpod
diff options
context:
space:
mode:
Diffstat (limited to 'libpod')
-rw-r--r--libpod/container_internal_linux.go16
-rw-r--r--libpod/container_internal_linux_test.go29
-rw-r--r--libpod/container_log.go16
3 files changed, 54 insertions, 7 deletions
diff --git a/libpod/container_internal_linux.go b/libpod/container_internal_linux.go
index 847122929..cafa3c642 100644
--- a/libpod/container_internal_linux.go
+++ b/libpod/container_internal_linux.go
@@ -1,3 +1,4 @@
+//go:build linux
// +build linux
package libpod
@@ -1942,9 +1943,24 @@ func (c *Container) generateHosts(path string) (string, error) {
}
hosts := string(orig)
hosts += c.getHosts()
+
+ hosts = c.appendLocalhost(hosts)
+
return c.writeStringToRundir("hosts", hosts)
}
+// based on networking mode we may want to append the localhost
+// if there isn't any record for it and also this shoud happen
+// in slirp4netns and similar network modes.
+func (c *Container) appendLocalhost(hosts string) string {
+ if !strings.Contains(hosts, "localhost") &&
+ !c.config.NetMode.IsHost() {
+ hosts += "127.0.0.1\tlocalhost\n::1\tlocalhost\n"
+ }
+
+ return hosts
+}
+
// appendHosts appends a container's config and state pertaining to hosts to a container's
// local hosts file. netCtr is the container from which the netNS information is
// taken.
diff --git a/libpod/container_internal_linux_test.go b/libpod/container_internal_linux_test.go
index 1465ffbea..899f9bffd 100644
--- a/libpod/container_internal_linux_test.go
+++ b/libpod/container_internal_linux_test.go
@@ -1,3 +1,4 @@
+//go:build linux
// +build linux
package libpod
@@ -7,6 +8,7 @@ import (
"os"
"testing"
+ "github.com/containers/podman/v3/pkg/namespaces"
spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/stretchr/testify/assert"
)
@@ -68,3 +70,30 @@ func TestGenerateUserGroupEntry(t *testing.T) {
}
assert.Equal(t, group, "567:x:567:567\n")
}
+
+func TestAppendLocalhost(t *testing.T) {
+ {
+ c := Container{
+ config: &ContainerConfig{
+ ContainerNetworkConfig: ContainerNetworkConfig{
+ NetMode: namespaces.NetworkMode("slirp4netns"),
+ },
+ },
+ }
+
+ assert.Equal(t, "127.0.0.1\tlocalhost\n::1\tlocalhost\n", c.appendLocalhost(""))
+ assert.Equal(t, "127.0.0.1\tlocalhost", c.appendLocalhost("127.0.0.1\tlocalhost"))
+ }
+ {
+ c := Container{
+ config: &ContainerConfig{
+ ContainerNetworkConfig: ContainerNetworkConfig{
+ NetMode: namespaces.NetworkMode("host"),
+ },
+ },
+ }
+
+ assert.Equal(t, "", c.appendLocalhost(""))
+ assert.Equal(t, "127.0.0.1\tlocalhost", c.appendLocalhost("127.0.0.1\tlocalhost"))
+ }
+}
diff --git a/libpod/container_log.go b/libpod/container_log.go
index 3988bb654..89dd5e8b0 100644
--- a/libpod/container_log.go
+++ b/libpod/container_log.go
@@ -107,16 +107,18 @@ func (c *Container) readFromLogFile(ctx context.Context, options *logs.LogOption
// until EOF.
state, err := c.State()
if err != nil || state != define.ContainerStateRunning {
- // Make sure to wait at least for the poll duration
- // before stopping the file logger (see #10675).
- time.Sleep(watch.POLL_DURATION)
- tailError := t.StopAtEOF()
- if tailError != nil && fmt.Sprintf("%v", tailError) != "tail: stop at eof" {
- logrus.Errorf("Error stopping logger: %v", tailError)
- }
if err != nil && errors.Cause(err) != define.ErrNoSuchCtr {
logrus.Errorf("Error getting container state: %v", err)
}
+ go func() {
+ // Make sure to wait at least for the poll duration
+ // before stopping the file logger (see #10675).
+ time.Sleep(watch.POLL_DURATION)
+ tailError := t.StopAtEOF()
+ if tailError != nil && tailError.Error() != "tail: stop at eof" {
+ logrus.Errorf("Error stopping logger: %v", tailError)
+ }
+ }()
return nil
}