TL;DR
Describe the following in docker-compose.override.yaml
yaml:docker-compose.override.yaml
version: "3.5" # docker-compose.Match with yaml version
services:
app: # host.docker.Service name for which you want to use internal
extra_hosts:
- "host.docker.internal:172.101.0.1" #Match to the subnet specification below
networks:
default:
driver: bridge
ipam:
config:
- subnet: 172.101.0.0/16 #Specify not to overlap with other networks
If the firewall is running, you may need to allow access from the above subnet. Here is a command example of firewalld.
sudo firewall-cmd --permanent --zone=trusted --add-source=172.101.0.0/16
host.docker.internal
A special domain for accessing the host's network from a container, supported by docker for Mac and docker for Windows. Since mac has become the mainstream of developers, it is easy to use in projects, and linux users have troubles every time.
Support host.docker.internal DNS name to host Support host.docker.internal in dockerd on Linux [RFD] add configuration option to add host.docker.internal by default
With these issues, host.docker.internal
will eventually be available on linux, but it will take some time before it is released, so I'll mention Workaround.
Rather, the work content is as described above, so I will write a commentary.
docker-compose.override.yaml
This file is automatically read when you execute the docker-compose command, so it is useful when writing your own settings that you do not want to include in the repository. However, when you use the -f option, it is not read automatically, so you need to specify this file as well.
Add the contents to / etc / hosts
of the container.
networks
All containers belong to the network named default unless otherwise specified, so if you write the settings here, they will be automatically reflected in all containers. The subnet set here is the default network subnet. Normally, the network subnet is automatically selected so that it does not overlap with the existing network, but this time I want to fix it, so I manually select and set it. The host belongs to all networks, and its IP seems to be "the lowest bit of the subnet mask is set to 1". (I didn't find such a statement in the documentation just because I tried it and said it was, so maybe it doesn't happen in some cases.) So, if you specify this IP in extra_hosts, you can assign a domain.
Recommended Posts