Skip to main content

How does Docker access the host MYSQL from the project in the container?

How does Docker access the host MYSQL from the project in the container?

1. Description

Using Docker you can containerize services and communicate between them using inter-container networking. Sometimes you may need a container to communicate with non-containerized services on the host machine. Here's how to access localhost or 127.0.0.1 from within a Docker container.

2. Method 1: Simple choice

Docker Desktop 18.03+ for Windows and Mac supports host.[docker](/search?q=docker).internal as a functional alias for localhost. Use this string in your container to access your host. Note that the way to distinguish entities here is

Designated communication entityAlias
interpreted as a containerlocalhost或127.0.0.1
interpreted as hosthost.docker.internal

For example, if you run the MySQL server on the host, the Docker container can access the mysql connected to the host through the network. The specific name is host.docker.internal:3306. This is the simplest technique when you are working on a Windows or Mac computer.

Docker engine users on Linux can also enable the host's default name of host.docker.internal via the --add-host flag of docker run. Start the container with this flag to expose the host string:

Exhibition example:

 
docker run -d --add-host host.docker.internal:host-gateway -p 80:80 -p 443:443 dzq:latest

Use (connect to host database):

host.docker.internal:3306

The --add-host flag adds an entry to the container's /etc/hosts file. The value shown above maps host.docker.internal to the container's host gateway, which matches the real localhost value. If you prefer, you can replace host.docker.internal with your host's real name.

Method 2: Connect to the host network

Docker provides a host network that allows containers to share the host's network stack. This approach means that localhost within the container resolves to the physical host, not the container itself.

Start the container with the host network by adding the --network=host flag:

 
docker run -d --network=host my-container:latest

Now your container can directly reference localhost or 127.0.0.1.

If you are using Docker Compose, modify the container's service definition to include the network_mode field:

 
services:my-service:network_mode: host

There are several caveats to this approach. It's important to consider all the implications before using it. Containers typically get their own private network, which is independent of the host's stack. Settings from the host.

Any ports exposed by the container will be exposed on the host, even if they are not explicitly declared with the -p flag.

Host networking can be a security issue that breaks the isolation model of Docker containers. It's still useful in situations where you're confident that running containers won't conflict with each other or cause problems in the host environment. Host networking mode is also faster than the default bridge mode because there is no virtualization layer for traffic to pass through.

Method 3: Access the host using default bridge mode

Your host will still be accessible from the container in default bridged networking mode. You just need to reference it by its Docker network IP, not localhost or 127.0.0.1.

Most Docker engine installations will represent the host as 172.17.0.1 on the default docker0 bridge network. You can check your own IP by running the following command on your host machine:

 
ip addr show docker0

Your host's Docker IP will appear on the inet line. Connect to this IP address from your container to successfully access the service running on your host.

How does Docker access the host MYSQL from the project in the container?

One drawback to this approach is that you may not be able to connect to services that are bound directly to localhost. You will see connection refused or similar errors in the container.