Use host.docker.internal on linux (docker-compose required)

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

firewall settings

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.

extra_hosts

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

Use host.docker.internal on linux (docker-compose required)
Use Github Desktop on Linux
Use sshpass on Amazon linux2
Use Linux on Windows 10 (WSL2)
Use Chrome Remote Desktop on Linux
Run docker-compose on Amazon Linux2 on ARM64
Use Linux commands on Android devices
Use Numpy, Scipy, scikit-learn on Amazon Linux
I want to use Linux on mac
Why use linux
Daemonizing processes on Linux
jblas on Arch Linux
How to use C216 Audio Controller on Arch Linux
Linux (WSL) on Windows
NAT router on Linux
Use pyvenv on Windows
Develop .NET on Linux
Wake on lan on Linux
Preparing to use Ansible on an existing Linux server
I want to use OpenJDK 11 on Ubuntu Linux 18.04 LTS / 18.10
Monitor traffic on Linux
Update vscode on linux
Use Ansible on Windows
Try NeosVR on Linux
Use QuTiP on Windows
Use pip on Windows
Check capacity on Linux
[Tips] How to use iPhone as webcam on Linux
LiveUSB creation on Linux
Linux operation on Win10
[For beginners] How to use for statements on Linux (variables, etc.)
When you want to use python2.x on modern Gentoo Linux
What to do if you can't use WiFi on Linux
NTP configuration memo on Linux
Install Minecraft on Arch Linux
Use matplotlib on Ubuntu 12 & Python
Install Linux on your Chromebox
Downgrade Mcomix on Arch Linux
Until docker-compose up on CentOS7
Use music21 on Google Colaboratory
Monitor disk usage on Linux
Install the JDK on Linux
Elixir = Comfortable on Linux Mint
Recording and playback on Linux
Check TTL on Linux router
Read core voltage on Linux
Put jenv on Amazon Linux
Easy df command on Linux
Linux on Windows -1-: debian introduction
Install tomcat 5.5 on Amazon Linux.
Introducing Elixir on Linux Mint
Use matplot libwidget on mac
Use WDC-433SU2M2 with Manjaro Linux
Install Homebrew on Amazon Linux 2
Paste the link on linux
Install strongSwan 5.9.1 on Amazon Linux 2
Use Python on Windows (PyCharm)
Linux environment construction (on WSL environment)
Run FreeBSD on Linux + qemu
Use NeoPixel on Raspberry Pi
I want to use shortcut translation like DeepL app on Linux