Skip to content

docker buildx fails to validate "--add-host=host.docker.internal:host-gateway" #44525

Description

@ferrarimarco

Description

When building an image with docker buildx on Linux and adding the --add-host=host.docker.internal:host-gateway option, it fails because the validation of the host-gateway is not an IP address, as required by

if len(parts) != 2 || parts[0] == "" || net.ParseIP(parts[1]) == nil {
return "", errors.Errorf("invalid host %s", h)
}

docker buildx output:

$ docker build --add-host=host.docker.internal:host-gateway .

[+] Building 0.0s (0/0)
ERROR: invalid host host.docker.internal:host-gateway

docker build is able to build the image correctly. In fact, it checks for the host-gateway special case:

moby/opts/hosts.go

Lines 180 to 184 in f1dd6bf

if arr[1] != HostGatewayName {
if _, err := ValidateIPAddress(arr[1]); err != nil {
return "", errors.Errorf("invalid IP address in add-host: %q", arr[1])
}
}

docker build output:

$ docker build --add-host=host.docker.internal:host-gateway --tag whatever . 

Sending build context to Docker daemon  1.38MB
Step 1/1 : FROM ubuntu:22.04
22.04: Pulling from library/ubuntu
e96e057aae67: Pull complete
Digest: sha256:4b1d0c4a2d2aaf63b37111f34eb9fa89fa1bf53dd6e4ca954d47caebca4005c2
Status: Downloaded newer image for ubuntu:22.04
 ---> a8780b506fa4
Successfully built a8780b506fa4

Reproduce

  1. Create a Dockerfile with the following contents:
FROM ubuntu:22.04
  1. Run docker buildx --add-host=host.docker.internal:host-gateway .

Expected behavior

  • docker buildx should be able to build the image
  • The host.docker.internal name should point to the address of the machine running the container

docker version

Client: Docker Engine - Community
 Version:           20.10.21
 API version:       1.41
 Go version:        go1.18.7
 Git commit:        baeda1f
 Built:             Tue Oct 25 18:02:28 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.21
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.18.7
  Git commit:       3056208
  Built:            Tue Oct 25 18:00:19 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.9
  GitCommit:        1c90a442489720eec95342e1789ee8a5e1b9536f
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

docker info

Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Docker Buildx (Docker Inc., v0.9.1-docker)

Server:
 Containers: 14
  Running: 1
  Paused: 0
  Stopped: 13
 Images: 549
 Server Version: 20.10.21
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: systemd
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runtime.v1.linux runc io.containerd.runc.v2
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 1c90a442489720eec95342e1789ee8a5e1b9536f
 runc version: v1.1.4-0-g5fd4c4d
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: default
  cgroupns
 Kernel Version: 5.18.16-amd64
 Operating System: Debian GNU/Linux
 OSType: linux
 Architecture: x86_64
 CPUs: 48
 Total Memory: 177.1GiB
 Name: [redacted]
 ID: [redacted]
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

Additional Info

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/builderBuildarea/builder/buildkitBuildkind/bugBugs are bugs. The cause may or may not be known at triage time so debugging may be needed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions