When building an image of a docker container I need to access a service on host localhost.
It looks like
--add-host option for
docker command updates
/etc/hosts both at image build time and container runtime.
The analogous option for
extra_hosts seems to work only at container runtime. How do I enable it at docker image build time?
Looks like there feature request for that: https://github.com/docker/compose/issues/7323, but not implemented yet. What would be workaround until this is solved?
Minimal working example
FROM python:3.8-slim-buster RUN grep "host.docker.internal" /etc/hosts || echo "Missing" CMD grep "host.docker.internal" /etc/hosts || echo "Missing"
version: "3.9" services: test: build: . extra_hosts: - "host.docker.internal:host-gateway"
docker build . --add-host host.docker.internal:host-gateway --no-cache -t test \ && docker run --add-host host.docker.internal:host-gateway test
172.17.0.1 host.docker.internal ... 172.17.0.1 host.docker.internal ...
docker-compose build --no-cache && docker-compose up
Missing ... test_1 | 172.17.0.1 host.docker.internal ...
I have a Flask app that depends on my private Python package that I install during docker image build. I store my private Python packages in my local devpi instance hosted on the same machine I use for building docker image. Hence, my Flask app
pyproject.toml points to the devpi instance:
url = "http://localhost:3141/testuser/dev/+simple"
url = "http://host.docker.internal:3141/testuser/dev/+simple"