host-gateway variable does not work on build time when defined in the docker-compose file
Description
I defined the host mapping "host.docker.internal:host-gateway" in the extra_hosts definition in the build definition of my docker-compose.yaml file (see below).
The problem is that docker compose does not convert the host-gateway variable when starting the build.
Instead, I get the error invalid host host.docker.internal:host-gateway.
The same host mapping works when using it in the service definition outside the build definition (so that it applies at runtime instead of during build time; see also in the docker-compose.yaml below).
Using the docker build command to define the host works on the other hand (see the command below).
In the following are all my files and commands to reproduce the issue:
docker-compose.yaml:
services:
debian:
build:
context: .
extra_hosts:
- "host.docker.internal:host-gateway" # Does not work
# - "host.docker.internal:172.17.0.1" # (host IP); works but I have to define the IP statically
extra_hosts:
- "host.docker.internal:host-gateway" # Works
Dockerfile:
FROM debian:bullseye
# Just to print some debug output
RUN cat /etc/hosts
CMD tail -f /dev/null
Command to build using docker compose:
sudo docker compose build --no-cache --progress=plain
Command to build using docker (this one works as expected):
sudo docker build . --no-cache --add-host "host.docker.internal:host-gateway"
Steps to reproduce the issue:
- Set up a directory containing above files
- Run the above
docker compose buildcommand and see the error - Use the
docker buildcommand above and see it working (the hosts file is printed during build where you can see the correct IP address)
Describe the results you received:
docker compose fails with the error invalid host host.docker.internal:host-gateway instead of replacing host-gateway with the host IP address.
Describe the results you expected:
I would expect that the image builds without errors and with host.docker.internal populated with the correct IP address
Additional information you deem important (e.g. issue happens only occasionally):
None
Output of docker compose version:
Docker Compose version v2.6.0
Output of docker info:
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Docker Buildx (Docker Inc., v0.8.2-docker)
compose: Docker Compose (Docker Inc., v2.6.0)
scan: Docker Scan (Docker Inc., v0.17.0)
Server:
Containers: 4
Running: 2
Paused: 0
Stopped: 2
Images: 27
Server Version: 20.10.17
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
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.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 0197261a30bf81f1ee8e6a4dd2dea0ef95d67ccb
runc version: v1.1.3-0-g6724737
init version: de40ad0
Security Options:
apparmor
seccomp
Profile: default
Kernel Version: 5.4.0-124-generic
Operating System: Ubuntu 20.04.4 LTS
OSType: linux
Architecture: x86_64
CPUs: 16
Total Memory: 22.93GiB
Name: <removed>
ID: 7I44:XIU7:6NQ7:DMGQ:MRYN:FW7S:LBLX:JYV5:CQ7T:T6NU:KKLP:3JT7
Docker Root Dir: /var/lib/docker
Debug Mode: false
HTTP Proxy: socks5://127.0.0.1:7777
HTTPS Proxy: socks5://127.0.0.1:7777
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
WARNING: No swap limit support
Additional environment details:
I have to use a SOCKS proxy (a port on the localhost forwards all requests through SSH to the proxy running on my client), that's why I need to know the host IP address during build time.