Skip to content

Commit bc90bb6

Browse files
committed
container create: combine client-side warning with daemon-side
Use a consistent approach for producing warnings, but add a TODO for moving this warning to the daemon, which can make a better call if it will work or not (depending on networking mode). This warning was originally added in [moby@afa92a9], before integration with libnetwork, and this warning may be incorrect in many scenarios. While updating, also removing the custom regular expression used to detect if the IP is a loopback address, and using go's netip package instead. [moby@afa92a9]: moby/moby@afa92a9 Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 58a3569 commit bc90bb6

3 files changed

Lines changed: 13 additions & 22 deletions

File tree

cli/command/container/create.go

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"context"
55
"fmt"
66
"io"
7+
"net/netip"
78
"os"
8-
"regexp"
99

1010
"github.com/containerd/platforms"
1111
"github.com/distribution/reference"
@@ -207,8 +207,6 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
207207
hostConfig := containerCfg.HostConfig
208208
networkingConfig := containerCfg.NetworkingConfig
209209

210-
warnOnLocalhostDNS(*hostConfig, dockerCli.Err())
211-
212210
var (
213211
trustedRef reference.Canonical
214212
namedRef reference.Named
@@ -291,6 +289,9 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
291289
}
292290
}
293291

292+
if warn := localhostDNSWarning(*hostConfig); warn != "" {
293+
response.Warnings = append(response.Warnings, warn)
294+
}
294295
for _, w := range response.Warnings {
295296
_, _ = fmt.Fprintln(dockerCli.Err(), "WARNING:", w)
296297
}
@@ -299,26 +300,16 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
299300
}
300301

301302
// check the DNS settings passed via --dns against localhost regexp to warn if
302-
// they are trying to set a DNS to a localhost address
303-
func warnOnLocalhostDNS(hostConfig container.HostConfig, stderr io.Writer) {
303+
// they are trying to set a DNS to a localhost address.
304+
//
305+
// TODO(thaJeztah): move this to the daemon, which can make a better call if it will work or not (depending on networking mode).
306+
func localhostDNSWarning(hostConfig container.HostConfig) string {
304307
for _, dnsIP := range hostConfig.DNS {
305-
if isLocalhost(dnsIP) {
306-
_, _ = fmt.Fprintf(stderr, "WARNING: Localhost DNS setting (--dns=%s) may fail in containers.\n", dnsIP)
307-
return
308+
if addr, err := netip.ParseAddr(dnsIP); err == nil && addr.IsLoopback() {
309+
return fmt.Sprintf("Localhost DNS (%s) may fail in containers.", addr)
308310
}
309311
}
310-
}
311-
312-
// IPLocalhost is a regex pattern for IPv4 or IPv6 loopback range.
313-
const ipLocalhost = `((127\.([0-9]{1,3}\.){2}[0-9]{1,3})|(::1)$)`
314-
315-
var localhostIPRegexp = regexp.MustCompile(ipLocalhost)
316-
317-
// IsLocalhost returns true if ip matches the localhost IP regular expression.
318-
// Used for determining if nameserver settings are being passed which are
319-
// localhost addresses
320-
func isLocalhost(ip string) bool {
321-
return localhostIPRegexp.MatchString(ip)
312+
return ""
322313
}
323314

324315
func validatePullOpt(val string) error {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
WARNING: Localhost DNS setting (--dns=::1) may fail in containers.
1+
WARNING: Localhost DNS (::1) may fail in containers.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
WARNING: Localhost DNS setting (--dns=127.0.0.11) may fail in containers.
1+
WARNING: Localhost DNS (127.0.0.11) may fail in containers.

0 commit comments

Comments
 (0)