Skip to content

Commit fa157f4

Browse files
committed
In host networking mode, unconditionally use "/etc/resolv.conf"
Signed-off-by: Kai Takac <[email protected]>
1 parent bc92b63 commit fa157f4

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

executor/oci/resolvconf.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,18 @@ var notFirstRun bool
1717
var lastNotEmpty bool
1818

1919
// overridden by tests
20-
var resolvconfPath = resolvconf.Path
20+
var resolvconfPath = func(netMode pb.NetMode) string {
21+
// The implementation of resolvconf.Path checks if systemd resolved is activated and chooses the internal
22+
// resolv.conf (/run/systemd/resolve/resolv.conf) in such a case - see resolvconf_path.go of libnetwork.
23+
// This, however, can be problematic, see https://github.com/moby/buildkit/issues/2404 and is not necessary
24+
// in case the networking mode is set to host since the locally (127.0.0.53) running resolved daemon is
25+
// accessible from inside a host networked container.
26+
// For details of the implementation see https://github.com/moby/buildkit/pull/5207#discussion_r1705362230.
27+
if netMode == pb.NetMode_HOST {
28+
return "/etc/resolv.conf"
29+
}
30+
return resolvconf.Path()
31+
}
2132

2233
type DNSConfig struct {
2334
Nameservers []string
@@ -44,7 +55,7 @@ func GetResolvConf(ctx context.Context, stateDir string, idmap *idtools.Identity
4455
generate = true
4556
}
4657
if !generate {
47-
fiMain, err := os.Stat(resolvconfPath())
58+
fiMain, err := os.Stat(resolvconfPath(netMode))
4859
if err != nil {
4960
if !errors.Is(err, os.ErrNotExist) {
5061
return struct{}{}, err
@@ -63,7 +74,7 @@ func GetResolvConf(ctx context.Context, stateDir string, idmap *idtools.Identity
6374
return struct{}{}, nil
6475
}
6576

66-
dt, err := os.ReadFile(resolvconfPath())
77+
dt, err := os.ReadFile(resolvconfPath(netMode))
6778
if err != nil && !errors.Is(err, os.ErrNotExist) {
6879
return struct{}{}, err
6980
}

executor/oci/resolvconf_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,16 @@ func TestResolvConf(t *testing.T) {
111111
t.Cleanup(func() {
112112
resolvconfPath = oldResolvconfPath
113113
})
114-
resolvconfPath = func() string {
115-
if tt.dt == nil {
116-
return "no-such-file"
117-
}
118-
rpath := path.Join(t.TempDir(), "resolv.conf")
119-
require.NoError(t, os.WriteFile(rpath, tt.dt, 0600))
120-
return rpath
121-
}
122114
for i := 0; i < tt.execution; i++ {
115+
resolvconfPath = func(netMode pb.NetMode) string {
116+
if tt.dt == nil {
117+
return "no-such-file"
118+
}
119+
rpath := path.Join(t.TempDir(), "resolv.conf")
120+
require.NoError(t, os.WriteFile(rpath, tt.dt, 0600))
121+
require.Equal(t, tt.networkMode[i], netMode)
122+
return rpath
123+
}
123124
if i > 0 {
124125
time.Sleep(100 * time.Millisecond)
125126
}

0 commit comments

Comments
 (0)