从Docker容器的内部,如何连接到机器的本地主机?

发布于 2025-01-30 16:16:50 字数 1751 浏览 5 评论 0 原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

忆梦 2025-02-06 16:16:51

我不同意托马斯利维尔的答案。

使MySQL绑定到172.17.42.1将防止使用主机上的数据库来达到其他程序。这只有在所有数据库用户都被停靠时才有效。

使MySQL绑定到0.0.0.0将向外界打开DB,这不仅是一件非常糟糕的事情,而且与原始问题作者想要做的事情相反。他明确地说:“ MySQL正在Localhost上运行,而不是向外界展开港口,因此它限制了Localhost”,

以回答Ivant的评论

“为什么不将mysql绑定到docker0?”

这是不可能的。 MySQL/Mariadb文档明确表示无法绑定到几个接口。您只能绑定到0、1或所有接口。

总而言之,我尚未找到任何方法可以从Docker容器上到达主机上的(仅本地主机)数据库。这绝对是一种非常非常常见的模式,但我不知道该怎么做。

I disagree with the answer from Thomasleveil.

Making mysql bind to 172.17.42.1 will prevent other programs using the database on the host to reach it. This will only work if all your database users are dockerized.

Making mysql bind to 0.0.0.0 will open the db to outside world, which is not only a very bad thing to do, but also contrary to what the original question author wants to do. He explicitly says "The MySql is running on localhost and not exposing a port to the outside world, so its bound on localhost"

To answer the comment from ivant

"Why not bind mysql to docker0 as well?"

This is not possible. The mysql/mariadb documentation explicitly says it is not possible to bind to several interfaces. You can only bind to 0, 1, or all interfaces.

As a conclusion, I have NOT found any way to reach the (localhost only) database on the host from a docker container. That definitely seems like a very very common pattern, but I don't know how to do it.

殤城〤 2025-02-06 16:16:51

在7年中,提出了问题,要么是Docker已更改,要么没有人尝试过。因此,我将包括我自己的答案。

我发现所有答案都使用复杂的方法。今天,我需要这个,并找到了2种非常简单的方法:

  • 使用 ipconfig ifconfig 在您的主机上,并记下所有IP地址。容器至少可以使用其中两个。

    • 我在WiFi LAN适配器上有一个固定的本地网络地址: 192.168.1.101 。这可以是 10.0.1.101 。结果将根据您的路由器而改变
    • 我在Windows上使用WSL,它有其自己的 vethernet 地址: 172.19.19.19.19.19.19.19.19.19.19.19.19.19.19.19.19.19.19.19.19.19.19.192.1
  • 使用 host.docker。内部。大多数答案根据OS具有此或另一种形式。该名称表明它现在由Docker全球使用。

第三个选项是使用机器的WAN地址,换句话说,服务提供商给出的IP。但是,如果IP不是静态的,则可能不起作用,并且需要路由和防火墙设置。

In 7 years the question was asked, it is either docker has changed, or no one tried this way. So I will include my own answer.

I have found all answers use complex methods. Today, I have needed this, and found 2 very simple ways:

  • use ipconfig or ifconfig on your host and make note of all IP addresses. At least two of them can be used by the container.

    • I have a fixed local network address on WiFi LAN Adapter: 192.168.1.101. This could be 10.0.1.101. the result will change depending on your router
    • I use WSL on windows, and it has its own vEthernet address: 172.19.192.1
  • use host.docker.internal. Most answers have this or another form of it depending on OS. The name suggests it is now globally used by docker.

A third option is to use WAN address of the machine, or in other words IP given by the service provider. However, this may not work if IP is not static, and requires routing and firewall settings.

醉生梦死 2025-02-06 16:16:51

您需要知道 gateway !我对本地服务器的解决方案是在 0.0.0.0:8000 下公开它,然后使用 subnet 运行容器喜欢

docker network create --subnet=172.35.0.0/16 --gateway 172.35.0.1 SUBNET35
docker run -d -p 4444:4444 --net SUBNET35 <container-you-want-run-place-here>

:您可以通过 http://172.35.0.1:8000访问环回

You need to know the gateway! My solution with local server was to expose it under 0.0.0.0:8000, then run docker with subnet and run container like:

docker network create --subnet=172.35.0.0/16 --gateway 172.35.0.1 SUBNET35
docker run -d -p 4444:4444 --net SUBNET35 <container-you-want-run-place-here>

So, now you can access your loopback through http://172.35.0.1:8000

以酷 2025-02-06 16:16:51

连接到网关地址。

❯ docker network inspect bridge | grep Gateway
                    "Gateway": "172.17.0.1"

确保主机上的过程在此界面或所有接口上都在侦听,并在Docker之后启动。如果使用SystemD,则可以添加以下内容以确保Docker之后启动。

[Unit]
After=docker.service

例子

❯ python -m http.server &> /dev/null &
[1] 149976

❯ docker run --rm python python -c  "from urllib.request import urlopen;print(b'Directory listing for' in urlopen('http://172.17.0.1:8000').read())" 
True

Connect to the gateway address.

❯ docker network inspect bridge | grep Gateway
                    "Gateway": "172.17.0.1"

Make sure the process on the host is listening on this interface or on all interfaces and is started after docker. If using systemd, you can add the below to make sure it is started after docker.

[Unit]
After=docker.service

Example

❯ python -m http.server &> /dev/null &
[1] 149976

❯ docker run --rm python python -c  "from urllib.request import urlopen;print(b'Directory listing for' in urlopen('http://172.17.0.1:8000').read())" 
True
只涨不跌 2025-02-06 16:16:51

这是我的解决方案:它适用于我的案例

  • 通过注释将本地mysql Server设置为公共访问

    #bind-address = 127.0.0.1

    在/etc/mysql/mysql.conf.d

  • 重新启动mysql Server
    sudo /etc/init.d/mysql restart

  • 运行以下命令以打开用户根访问任何主机
    mysql -uroot -poot
    将 *。
    赠款选项;
    冲洗特权;

  • 创建sh脚本:run_docker.sh

    #!bin/bash

    HOSTIP=`ip -4 addr show scope global dev eth0 | grep inet | awk '{print \$2}' | cut -d / -f 1`


      docker run -it -d --name web-app \
                  --add-host=local:${HOSTIP} \
                  -p 8080:8080 \
                  -e DATABASE_HOST=${HOSTIP} \
                  -e DATABASE_PORT=3306 \
                  -e DATABASE_NAME=demo \
                  -e DATABASE_USER=root \
                  -e DATABASE_PASSWORD=root \
                  sopheamak/springboot_docker_mysql

  
  • 使用docker-composer

    运行

    版本:'2.1'

    服务:
    Tomcatwar: extra_host: - “本地:10.1.2.232” 图像:Sopheamak/springboot_docker_mysql
    端口: -8080:8080 环境: -Database_host = local -Database_user = root -Database_password = root -Database_name =演示 -Database_port = 3306

Here is my solution : it works for my case

  • set local mysql server to public access by comment

    #bind-address = 127.0.0.1

    in /etc/mysql/mysql.conf.d

  • restart mysql server
    sudo /etc/init.d/mysql restart

  • run the following command to open user root access any host
    mysql -uroot -proot
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH
    GRANT OPTION;
    FLUSH PRIVILEGES;

  • create sh script : run_docker.sh

    #!bin/bash

    HOSTIP=`ip -4 addr show scope global dev eth0 | grep inet | awk '{print \$2}' | cut -d / -f 1`


      docker run -it -d --name web-app \
                  --add-host=local:${HOSTIP} \
                  -p 8080:8080 \
                  -e DATABASE_HOST=${HOSTIP} \
                  -e DATABASE_PORT=3306 \
                  -e DATABASE_NAME=demo \
                  -e DATABASE_USER=root \
                  -e DATABASE_PASSWORD=root \
                  sopheamak/springboot_docker_mysql

  
  • run with docker-composer

    version: '2.1'

    services:
    tomcatwar: extra_hosts: - "local:10.1.2.232" image: sopheamak/springboot_docker_mysql
    ports: - 8080:8080 environment: - DATABASE_HOST=local - DATABASE_USER=root - DATABASE_PASSWORD=root - DATABASE_NAME=demo - DATABASE_PORT=3306

波浪屿的海角声 2025-02-06 16:16:51

您只需在主机计算机上只做 ifconfig 即可检查主机IP,然后从容器内部连接到IP,对我来说非常有效。

You can simply do ifconfig on host machine to check your host IP, then connect to the ip from within your container, works perfectly for me.

原谅我要高飞 2025-02-06 16:16:51

在Linux上,如果您可以从容器内部使用 host.docker.internal ,但是您无法到达Localhost上打开的端口,请确保您的Docker安装在无根模式下。如果处于无根模式,则受到限制。引用 documentation

IPAddress shown in docker inspect is namespaced inside RootlessKit's network namespace. This means the IP address is not reachable from the host without nsenter-ing into the network namespace.

Host network (docker run --net=host) is also namespaced inside RootlessKit.

希望这对某人有帮助几天

On linux if you can ping the host.docker.internal from inside the container, but you can not reach the ports that are opened on your localhost, please make sure you have docker installed not in rootless mode. If it is in rootless mode it is limited. Quote from documentation:

IPAddress shown in docker inspect is namespaced inside RootlessKit's network namespace. This means the IP address is not reachable from the host without nsenter-ing into the network namespace.

Host network (docker run --net=host) is also namespaced inside RootlessKit.

Hope this helps someone, as I spend several days on it

百合的盛世恋 2025-02-06 16:16:51

您可以使用Alpine图像获得主机IP,

docker run --rm alpine ip route | awk 'NR==1 {print $3}'

这将更加一致,因为您始终使用Alpine来运行该命令。

与Mariano的答案相似,您可以使用同一命令设置环境变量

DOCKER_HOST=$(docker run --rm alpine ip route | awk 'NR==1 {print $3}') docker-compose up

You can get the host ip using alpine image

docker run --rm alpine ip route | awk 'NR==1 {print $3}'

This would be more consistent as you're always using alpine to run the command.

Similar to Mariano's answer you can use same command to set an environment variable

DOCKER_HOST=$(docker run --rm alpine ip route | awk 'NR==1 {print $3}') docker-compose up
静若繁花 2025-02-06 16:16:51

CGroup和名称空间在容器生态系统中起着重要作用。

名称空间提供了一层隔离。每个容器在单独的名称空间中运行,其访问仅限于该命名空间。 Cgroups控制每个容器的资源利用率,而名称空间控制过程可以看到的内容并访问相应的资源。

这是您可以遵循的解决方案方法的基本理解,

使用网络名称空间

当容器从图像中产生时,将定义和创建网络接口。这提供了容器唯一的IP地址和接口。

$ docker run -it alpine ifconfig

通过将命名空间更改为主机,Cotainers网络不会与其接口隔离,该过程将可以访问主机机器网络接口。

$ docker run -it --net=host alpine ifconfig

如果该过程在端口上听,则将在主机接口上侦听并映射到容器中。

使用PID名称空间
通过更改PID名称空间,容器可以与其他过程超出其正常范围之外的其他过程进行交互。

该容器将在其自己的名称空间中运行。

$ docker run -it alpine ps aux

通过将命名空间更改为主机,容器还可以查看系统上运行的所有其他过程。

$ docker run -it --pid=host alpine ps aux

共享名称空间

这是在生产中进行此操作的不良习惯,因为您正在脱离容器安全模型,该模型可能会为漏洞开放,并且可以轻松访问EavesDropper。这仅用于调试工具并低估容器安全性中的漏洞。

第一个容器是Nginx服务器。这将创建一个新的网络和过程名称空间。该容器将使自身绑定到新创建的网络接口的80端口。

$ docker run -d --name http nginx:alpine

另一个容器现在可以重复使用此命名空间,

$ docker run --net=container:http mohan08p/curl curl -s localhost

此容器也可以在共享容器中看到与过程的接口。

$ docker run --pid=container:http alpine ps aux

这将使您在不更改或重新启动应用程序的情况下向容器赋予更多特权。以类似的方式,您可以连接到主机上的MySQL,运行和调试您的应用程序。但是,不建议这样做。希望它有帮助。

The CGroups and Namespaces are playing major role in the Container Ecosystem.

Namespace provide a layer of isolation. Each container runs in a separate namespace and its access is limited to that namespace. The Cgroups controls the resource utilization of each container, whereas Namespace controls what a process can see and access the respective resource.

Here is the basic understanding of the solution approach you could follow,

Use Network Namespace

When a container spawns out of image, a network interface is defined and create. This gives the container unique IP address and interface.

$ docker run -it alpine ifconfig

By changing the namespace to host, cotainers networks does not remain isolated to its interface, the process will have access to host machines network interface.

$ docker run -it --net=host alpine ifconfig

If the process listens on ports, they'll be listened on the host interface and mapped to the container.

Use PID Namespace
By changing the Pid namespace allows a container to interact with other process beyond its normal scope.

This container will run in its own namespace.

$ docker run -it alpine ps aux

By changing the namespace to the host, the container can also see all the other processes running on the system.

$ docker run -it --pid=host alpine ps aux

Sharing Namespace

This is a bad practice to do this in production because you are breaking out of the container security model which might open up for vulnerabilities, and easy access to eavesdropper. This is only for debugging tools and understating the loopholes in container security.

The first container is nginx server. This will create a new network and process namespace. This container will bind itself to port 80 of newly created network interface.

$ docker run -d --name http nginx:alpine

Another container can now reuse this namespace,

$ docker run --net=container:http mohan08p/curl curl -s localhost

Also, this container can see the interface with the processes in a shared container.

$ docker run --pid=container:http alpine ps aux

This will allow you give more privileges to containers without changing or restarting the application. In the similar way you can connect to mysql on host, run and debug your application. But, its not recommend to go by this way. Hope it helps.

清风疏影 2025-02-06 16:16:51

直到 fix 不被合并到 master 分支中,要获得获取主机IP仅从容器的内部运行:(

ip -4 route list match 0/0 | cut -d' ' -f3

在这里)。

Until fix is not merged into master branch, to get host IP just run from inside of the container:

ip -4 route list match 0/0 | cut -d' ' -f3

(as suggested by @Mahoney here).

青巷忧颜 2025-02-06 16:16:50

编辑:

如果您正在使用 docker-for-mac docker-for-windows 18.03+,使用host host.docker.internal.internal 而不是<<<(而不是<<代码> 127.0.0.1 在您的连接字符串中)。

如果您使用 docker-for-linux 20.10.0+,也可以使用主机 host.docker.internal 如果,则带有的docker容器 - add-host host.docker.internal:host-gateway 选项,或在您的docker-compose.yml文件中添加以下片段:

extra_hosts:
    - "host.docker.internal:host-gateway"

否则,请阅读下面的


tldr

使用 -network =“ host” 在您的 docker Run 命令中,然后 127.0.0.1 在您的Docker容器中,将指向您的Docker Host。

注意:此模式仅适用于Linux的Docker,根据文档


在Docker容器网络模式上的注意

Docker提供不同的网络模式 。根据您选择的模式,您将以不同的方式连接到在Docker主机上运行的MySQL数据库。

Docker Run -network =“ bridge”(默认)

Docker默认情况下创建了一个名为 docker0 的桥梁。 Docker主机和Docker容器在该桥上都有一个IP地址。

在docker主机上,键入 sudo ip addr show docker0 您的输出看起来像:

[vagrant@docker:~] $ sudo ip addr show docker0
4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 56:84:7a:fe:97:99 brd ff:ff:ff:ff:ff:ff
    inet 172.17.42.1/16 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::5484:7aff:fefe:9799/64 scope link
       valid_lft forever preferred_lft forever

因此,我的docker主机在上具有IP地址 172.17.42.1 Docker0 网络接口。

现在启动一个新的容器并在其上获取外壳: docker run -rm -rm -it ubuntu:可信赖的bash 以及在容器类型 ip addr show eTH0 中,以发现其如何发现其如何发现其设置了主网络接口:

root@e77f6a1b3740:/# ip addr show eth0
863: eth0: <BROADCAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 66:32:13:f0:f1:e3 brd ff:ff:ff:ff:ff:ff
    inet 172.17.1.192/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::6432:13ff:fef0:f1e3/64 scope link
       valid_lft forever preferred_lft forever

这里我的容器具有IP地址 172.17.1.192 。现在查看路由表:

root@e77f6a1b3740:/# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         172.17.42.1     0.0.0.0         UG    0      0        0 eth0
172.17.0.0      *               255.255.0.0     U     0      0        0 eth0

因此,Docker Host 172.17.42.1 的IP地址设置为默认路由,可以从您的容器中访问。

root@e77f6a1b3740:/# ping 172.17.42.1
PING 172.17.42.1 (172.17.42.1) 56(84) bytes of data.
64 bytes from 172.17.42.1: icmp_seq=1 ttl=64 time=0.070 ms
64 bytes from 172.17.42.1: icmp_seq=2 ttl=64 time=0.201 ms
64 bytes from 172.17.42.1: icmp_seq=3 ttl=64 time=0.116 ms

Docker Run -network =“主机”

可或您可以使用主机 。这样的容器将与Docker主机共享网络堆栈,从容器的角度来看, localhost (或 127.0.0.0.1 )将参考Docker Host。

请注意,在您的Docker容器中打开的任何端口都将在Docker主机上打开。而这无需代码> -p Docker Run 选项

我的Docker主机上的IP配置:

[vagrant@docker:~] $ ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:98:dc:aa brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe98:dcaa/64 scope link
       valid_lft forever preferred_lft forever

主机模式中的Docker容器中:

[vagrant@docker:~] $ docker run --rm -it --network=host ubuntu:trusty ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:98:dc:aa brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe98:dcaa/64 scope link
       valid_lft forever preferred_lft forever

您可以看到Docker Host和Docker容器共享完全相同的网络接口,因此具有相同的IP地址。


连接到MySQL

从容器桥模式

,以访问从docker主机上从 bridge Mode 中的docker主机上运行的mySQL,您需要确保MySQL服务正在侦听 172.17.42.1 IP地址。

为此,请确保您在MySQL Config File(My.cnf)中具有 bind-address = 172.17.42.1 bind-code> bind-address = 0.0.0.0 。

如果您需要使用网关的IP地址设置环境变量,则可以在容器中运行以下代码:

export DOCKER_HOST_IP=$(route -n | awk '/UG[ \t]/{print $2}')

然后在应用程序中,使用 docker_host_ip 环境变量以打开与MySQL的连接。

注意:如果使用 bind-address = 0.0.0.0 您的MySQL Server将在所有网络接口上收听连接。这意味着可以从Internet到达您的MySQL服务器;确保相应地设置防火墙规则。

注2:如果您使用 bind-address = 172.17.42.1 您的mySQL Server将不听取与 127.0.0.0.1 的连接。在Docker主机上运行的过程要连接到MySQL将必须使用 172.17.42.1 IP地址。

主机模式

访问来自主机模式以主机模式的docker主机上运行的mySQL ,您可以在MySQL配置中保留 bind> bind-address = 127.0.0.1 127.0.0.1 从您的容器中:

[vagrant@docker:~] $ docker run --rm -it --network=host mysql mysql -h 127.0.0.1 -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 36
Server version: 5.5.41-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

注意:确实使用 mySQL -H 127.0.0.1 ,而不是 mysql -h localhost ;否则,MySQL客户端将尝试使用UNIX插座连接。

Edit:

If you are using Docker-for-mac or Docker-for-Windows 18.03+, connect to your mysql service using the host host.docker.internal (instead of the 127.0.0.1 in your connection string).

If you are using Docker-for-Linux 20.10.0+, you can also use the host host.docker.internal if you started your Docker container with the --add-host host.docker.internal:host-gateway option, or added the following snippet in your docker-compose.yml file :

extra_hosts:
    - "host.docker.internal:host-gateway"

Otherwise, read below


TLDR

Use --network="host" in your docker run command, then 127.0.0.1 in your docker container will point to your docker host.

Note: This mode only works on Docker for Linux, per the documentation.


Note on docker container networking modes

Docker offers different networking modes when running containers. Depending on the mode you choose you would connect to your MySQL database running on the docker host differently.

docker run --network="bridge" (default)

Docker creates a bridge named docker0 by default. Both the docker host and the docker containers have an IP address on that bridge.

on the Docker host, type sudo ip addr show docker0 you will have an output looking like:

[vagrant@docker:~] $ sudo ip addr show docker0
4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 56:84:7a:fe:97:99 brd ff:ff:ff:ff:ff:ff
    inet 172.17.42.1/16 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::5484:7aff:fefe:9799/64 scope link
       valid_lft forever preferred_lft forever

So here my docker host has the IP address 172.17.42.1 on the docker0 network interface.

Now start a new container and get a shell on it: docker run --rm -it ubuntu:trusty bash and within the container type ip addr show eth0 to discover how its main network interface is set up:

root@e77f6a1b3740:/# ip addr show eth0
863: eth0: <BROADCAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 66:32:13:f0:f1:e3 brd ff:ff:ff:ff:ff:ff
    inet 172.17.1.192/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::6432:13ff:fef0:f1e3/64 scope link
       valid_lft forever preferred_lft forever

Here my container has the IP address 172.17.1.192. Now look at the routing table:

root@e77f6a1b3740:/# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         172.17.42.1     0.0.0.0         UG    0      0        0 eth0
172.17.0.0      *               255.255.0.0     U     0      0        0 eth0

So the IP Address of the docker host 172.17.42.1 is set as the default route and is accessible from your container.

root@e77f6a1b3740:/# ping 172.17.42.1
PING 172.17.42.1 (172.17.42.1) 56(84) bytes of data.
64 bytes from 172.17.42.1: icmp_seq=1 ttl=64 time=0.070 ms
64 bytes from 172.17.42.1: icmp_seq=2 ttl=64 time=0.201 ms
64 bytes from 172.17.42.1: icmp_seq=3 ttl=64 time=0.116 ms

docker run --network="host"

Alternatively you can run a docker container with network settings set to host. Such a container will share the network stack with the docker host and from the container point of view, localhost (or 127.0.0.1) will refer to the docker host.

Be aware that any port opened in your docker container would be opened on the docker host. And this without requiring the -p or -P docker run option.

IP config on my docker host:

[vagrant@docker:~] $ ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:98:dc:aa brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe98:dcaa/64 scope link
       valid_lft forever preferred_lft forever

and from a docker container in host mode:

[vagrant@docker:~] $ docker run --rm -it --network=host ubuntu:trusty ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:98:dc:aa brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe98:dcaa/64 scope link
       valid_lft forever preferred_lft forever

As you can see both the docker host and docker container share the exact same network interface and as such have the same IP address.


Connecting to MySQL from containers

bridge mode

To access MySQL running on the docker host from containers in bridge mode, you need to make sure the MySQL service is listening for connections on the 172.17.42.1 IP address.

To do so, make sure you have either bind-address = 172.17.42.1 or bind-address = 0.0.0.0 in your MySQL config file (my.cnf).

If you need to set an environment variable with the IP address of the gateway, you can run the following code in a container :

export DOCKER_HOST_IP=$(route -n | awk '/UG[ \t]/{print $2}')

then in your application, use the DOCKER_HOST_IP environment variable to open the connection to MySQL.

Note: if you use bind-address = 0.0.0.0 your MySQL server will listen for connections on all network interfaces. That means your MySQL server could be reached from the Internet ; make sure to set up firewall rules accordingly.

Note 2: if you use bind-address = 172.17.42.1 your MySQL server won't listen for connections made to 127.0.0.1. Processes running on the docker host that would want to connect to MySQL would have to use the 172.17.42.1 IP address.

host mode

To access MySQL running on the docker host from containers in host mode, you can keep bind-address = 127.0.0.1 in your MySQL configuration and connect to 127.0.0.1 from your containers:

[vagrant@docker:~] $ docker run --rm -it --network=host mysql mysql -h 127.0.0.1 -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 36
Server version: 5.5.41-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

note: Do use mysql -h 127.0.0.1 and not mysql -h localhost; otherwise the MySQL client would try to connect using a unix socket.

夏天碎花小短裙 2025-02-06 16:16:50

对于所有平台

docker v 20.10及以上(自2020年12月14日起)

使用您的内部IP地址或连接到特殊DNS名称 host.docker.internal.internal ,该将解决至主机使用的内部IP地址。

这是出于开发目的,在Docker桌面以外的生产环境中不起作用。

linux警告

docker linux上启用这一点代码> Docker 命令启用该功能。

要在 docker撰写Linux上的中,将以下行添加到容器定义中:

extra_hosts:
    - "host.docker.internal:host-gateway"

根据某些用户,特殊DNS名称仅在Docker的默认值 bridge 网络中起作用,而不是在在自定义网络中。

的旧MacOS和Windows版本

对于Docker Docker V 18.03及以上

(自2018年3月21日起)使用您的内部IP地址或连接到Special DNS名称 host.docker.docker.internal.internal 它将解析为主机使用的内部IP地址。

linux支持待处理 https://github.com/docker.com/docker/docker/docker/for-linux/issues/264 < /a>

对于旧的MacOS版本的Docker

Docker的Mac V 17.12至V 18.02

与上述相同,但使用 docker.for.mac.host.internal.internal 而不是使用。

Mac V 17.06至V 17.11

与上述相同的Docker,但使用 docker.for.mac.localhost

Mac 17.05及以下的Docker

才能从Docker容器访问主机机器,您必须将IP别名附加到网络接口。您可以绑定所需的任何IP,只需确保您没有将其用于其他任何IP即可。

sudo ifconfig lo0别名123.123.123.123/24

,请确保服务器正在收听上述IP或 0.0.0.0.0 。如果是在Local -Host 127.0.0.1 上聆听,则将不接受连接。

然后只需将Docker容器指向此IP,您就可以访问主机!

要测试,您可以运行 curl -x get在容器内部的123.123.123.123:3000

别名将重置每个重新启动,因此如有必要,请创建一个启动脚本。

解决方案和更多文档此处: https> https:// docs.docker.com/desktop/networking/#use-cases-and-workarounds-for-all-platforms

For all platforms

Docker v 20.10 and above (since December 14th 2020)

Use your internal IP address or connect to the special DNS name host.docker.internal which will resolve to the internal IP address used by the host.

This is for development purpose and does not work in a production environment outside of Docker Desktop.

Linux caveats

To enable this in Docker on Linux, add --add-host=host.docker.internal:host-gateway to your docker command to enable the feature.

To enable this in Docker Compose on Linux, add the following lines to the container definition:

extra_hosts:
    - "host.docker.internal:host-gateway"

According to some users the special DNS name only works within the Docker's default bridge network, not within custom networks.

For older macOS and Windows versions of Docker

Docker v 18.03 and above (since March 21st 2018)

Use your internal IP address or connect to the special DNS name host.docker.internal which will resolve to the internal IP address used by the host.

Linux support pending https://github.com/docker/for-linux/issues/264

For older macOS versions of Docker

Docker for Mac v 17.12 to v 18.02

Same as above but use docker.for.mac.host.internal instead.

Docker for Mac v 17.06 to v 17.11

Same as above but use docker.for.mac.localhost instead.

Docker for Mac 17.05 and below

To access host machine from the docker container you must attach an IP alias to your network interface. You can bind whichever IP you want, just make sure you're not using it to anything else.

sudo ifconfig lo0 alias 123.123.123.123/24

Then make sure that you server is listening to the IP mentioned above or 0.0.0.0. If it's listening on localhost 127.0.0.1 it will not accept the connection.

Then just point your docker container to this IP and you can access the host machine!

To test you can run something like curl -X GET 123.123.123.123:3000 inside the container.

The alias will reset on every reboot so create a start-up script if necessary.

Solution and more documentation here: https://docs.docker.com/desktop/networking/#use-cases-and-workarounds-for-all-platforms

ぃ弥猫深巷。 2025-02-06 16:16:50

使用

host.docker.internal

而不是

localhost

Use

host.docker.internal

instead of

localhost
猥︴琐丶欲为 2025-02-06 16:16:50

我执行了类似于上述帖子的黑客术语,将本地IP映射到容器中的别名名称(DNS)。主要问题是使用一个简单的脚本动态地进行 linux和OSX的工作,主机IP地址。我做了此脚本在两个环境中都可以使用的脚本(即使在Linux发行中,也使用“ $ lang”!=“ en _*” 配置了):

ifconfig | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -v 127.0.0.1 | awk '{ print $2 }' | cut -f2 -d: | head -n1

因此,使用Docker Compose,完整的配置将为:

启动脚本(docker-run.sh)

export DOCKERHOST=$(ifconfig | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -v 127.0.0.1 | awk '{ print $2 }' | cut -f2 -d: | head -n1)
docker-compose -f docker-compose.yml up

docker-compose.yml

myapp:
  build: .
  ports:
    - "80:80"
  extra_hosts:
    - "dockerhost:$DOCKERHOST"

然后将 http:// localhost 更改为 http:// dockerhost 在您的代码中。

有关如何自定义 dockerhost 脚本的更优先指南-my-ip-address-using bass/37212665#37212665“>这篇文章 解释了它的工作原理。

I doing a hack similar to above posts of get the local IP to map to a alias name (DNS) in the container. The major problem is to get dynamically with a simple script that works both in Linux and OSX the host IP address. I did this script that works in both environments (even in Linux distribution with "$LANG" != "en_*" configured):

ifconfig | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -v 127.0.0.1 | awk '{ print $2 }' | cut -f2 -d: | head -n1

So, using Docker Compose, the full configuration will be:

Startup script (docker-run.sh):

export DOCKERHOST=$(ifconfig | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -v 127.0.0.1 | awk '{ print $2 }' | cut -f2 -d: | head -n1)
docker-compose -f docker-compose.yml up

docker-compose.yml:

myapp:
  build: .
  ports:
    - "80:80"
  extra_hosts:
    - "dockerhost:$DOCKERHOST"

Then change http://localhost to http://dockerhost in your code.

For a more advance guide of how to customize the DOCKERHOST script, take a look at this post with a explanation of how it works.

狼性发作 2025-02-06 16:16:50

Linux的解决方案(内核&gt; = 3.6)。

好的,您的 localhost 服务器具有默认的Docker接口 docker0 带有IP地址 172.17.0.0.1 。您的容器以默认网络设置 - Net =“ bridge” 开始。

  1. 启用 route_localnet docker0 接口:

      $ sysctl -w net.ipv4.conf.docker0.route_localnet = 1
     
  2. 将这些规则添加到 iptables

      $ iptables -t nat -i pretering -i docker0 -d 172.17.0.0.1 -p tcp -dport 3306 -j dnat -j dnat -t-t-to 127.0.0.1:3306
    $ iptables -t filter -i输入-i docker0 -d 127.0.0.1 -p tcp -dport 3306 -J接受
     
  3. 创建MySQL用户,并使用'%'访问的方式,这意味着 - 来自任何人,不包括 localhost

     <代码>创建用户'user'@'%'由“密码”识别;
     
  4. 在您的脚本中更改MySQL -Server地址为 172.17。 0.1

来自内核文档

Route_localnet - 布尔值:在路由时,请勿将回环地址视为火星源或目的地。这使使用127/8用于本地路由目的(默认false )。

Solution for Linux (kernel >=3.6).

Ok, your localhost server has a default docker interface docker0 with IP address 172.17.0.1. Your container started with default network settings --net="bridge".

  1. Enable route_localnet for docker0 interface:

    $ sysctl -w net.ipv4.conf.docker0.route_localnet=1
    
  2. Add these rules to iptables:

    $ iptables -t nat -I PREROUTING -i docker0 -d 172.17.0.1 -p tcp --dport 3306 -j DNAT --to 127.0.0.1:3306
    $ iptables -t filter -I INPUT -i docker0 -d 127.0.0.1 -p tcp --dport 3306 -j ACCEPT
    
  3. Create MySQL user with access from '%' that means - from anyone, excluding localhost:

    CREATE USER 'user'@'%' IDENTIFIED BY 'password';
    
  4. Change in your script the mysql-server address to 172.17.0.1.

From the kernel documentation:

route_localnet - BOOLEAN: Do not consider loopback addresses as martian source or destination while routing. This enables the use of 127/8 for local routing purposes (default FALSE).

匿名。 2025-02-06 16:16:50

这对我在nginx/php-fpm堆栈上工作,而无需触摸应用程序刚刚期望能够连接到 localhost

mount mysqld.sock 的 任何代码或网络。主机到容器内部。

在运行mysql的主机上查找MySQL.Sock文件的位置:
netstat -ln | awk'/mysql(.*:\.sock/ {打印$ 9}'

将该文件安装到docker中的预期:
docker run -v/hostpath/to/mysqld.sock:/containerpath/to/mysqld.sock

MySQLD.Sock的可能位置:

/tmp/mysqld.sock
/var/run/mysqld/mysqld.sock 
/var/lib/mysql/mysql.sock
/Applications/MAMP/tmp/mysql/mysql.sock # if running via MAMP

This worked for me on an NGINX/PHP-FPM stack without touching any code or networking where the app's just expecting to be able to connect to localhost

Mount mysqld.sock from the host to inside the container.

Find the location of the mysql.sock file on the host running mysql:
netstat -ln | awk '/mysql(.*)?\.sock/ { print $9 }'

Mount that file to where it's expected in the docker:
docker run -v /hostpath/to/mysqld.sock:/containerpath/to/mysqld.sock

Possible locations of mysqld.sock:

/tmp/mysqld.sock
/var/run/mysqld/mysqld.sock 
/var/lib/mysql/mysql.sock
/Applications/MAMP/tmp/mysql/mysql.sock # if running via MAMP
如痴如狂 2025-02-06 16:16:50

直到 host.docker.internal 为每个平台工作,您可以将我的容器用作NAT网关,而无需任何手动设置:

https://github.com/qoomon/docker-host

Until host.docker.internal is working for every platform, you can use my container acting as a NAT gateway without any manual setup:

https://github.com/qoomon/docker-host

葬心 2025-02-06 16:16:50

Mac OSX的最简单解决方案

只需使用Mac的IP地址即可。在Mac上运行此操作以获取IP地址并从容器中使用IP地址:

ifconfig | grep 'inet 192'| awk '{ print $2}'

只要服务器在Mac上或另一个Docker容器中本地运行将能够在该地址伸出援手。

如果您只想访问另一个在 0.0.0.0 上收听的Docker容器,则可以使用 172.17.0.1

Simplest solution for Mac OSX

Just use the IP address of your Mac. On the Mac run this to get the IP address and use it from within the container:

ifconfig | grep 'inet 192'| awk '{ print $2}'

As long as the server running locally on your Mac or in another docker container is listening to 0.0.0.0, the docker container will be able to reach out at that address.

If you just want to access another docker container that is listening on 0.0.0.0 you can use 172.17.0.1

清音悠歌 2025-02-06 16:16:50

想到了几种解决方案:

  1. 将依赖项转移到容器中,首先
  2. 使您的其他服务在外部访问中,并与外部IP连接
  3. 您的容器,而无需网络隔离
  4. 避免通过网络连接,请使用安装的套接字作为音量,而是

原因。这是不合时宜的,默认情况下,容器以其自己的网络名称空间运行。这意味着Localhost(或指向回环接口的127.0.0.1)是每个容器唯一的。连接到此,将连接到容器本身,而不是在Docker之外或其他Docker容器内部运行的服务。

选项1 :如果您的依赖性可以移至容器中,我将首先执行此操作。当其他人试图在自己的环境上运行您的容器时,它使您的应用程序堆栈可移植。您仍然可以在主机上发布端口,其中未迁移的其他服务仍然可以达到。您甚至可以将端口发布到Docker主机上的Localhost接口,以避免使用以下语法在外部访问: -p 127.0.0.0.1:3306:3306 已发布的端口。

选项2 :有多种方法可以从容器内部检测主机IP地址,但是每个方案都有有限的情况(例如,需要Mac的Docker)。最便携的选项是将主机IP注入包含环境变量或配置文件之类的内容,例如:

docker run --rm -e "HOST_IP=$(ip route get 1 | sed -n 's/^.*src \([0-9.]*\) .*$/\1/p')" ...

这确实要求您的服务在该外部接口上侦听,这可能是安全问题。对于其他方法从容器内部获取主机IP地址,请参阅此帖子

便携式稍小,是使用 host.docker.internal 。这在当前版本的Windows docker和Mac的Docker中起作用。在20.10中,当您通过以下特殊主机条目时,该功能已添加到Linux的Docker中:

docker run --add-host host.docker.internal:host-gateway ...

主机-Gateway 是Docker 20.10中添加的特殊值,该值将自动扩展到主机IP。有关更多详细信息,请参见此pr

选项3 :在没有网络隔离的情况下运行,即使用 - NET HOST 运行,表示您的应用程序正在主机网络名称空间上运行。对于容器而言,这是不太隔离的,这意味着您无法通过具有DNS的共享Docker网络访问其他容器(而是需要使用已发布的端口来访问其他容器化应用程序)。但是,对于需要访问主机上其他仅在 127.0.0.0.1 上侦听其他服务的应用程序,这可能是最简单的选项。

选项4 :各种服务还允许访问基于文件系统的套接字。该插座可以作为绑定的插入体积安装到容器中,从而可以访问主机服务而无需越过网络。对于访问Docker引擎,您经常看到安装/var/run/docker.sock 进入容器中的示例(使该容器root访问主机)。使用MySQL,您可以尝试 -v/var/run/mysqld/mysqld.sock:/var/run/mysqld/mysql.sock ,然后连接到 localhost MySQL使用套接字转换为。

Several solutions come to mind:

  1. Move your dependencies into containers first
  2. Make your other services externally accessible and connect to them with that external IP
  3. Run your containers without network isolation
  4. Avoid connecting over the network, use a socket that is mounted as a volume instead

The reason this doesn't work out of the box is that containers run with their own network namespace by default. That means localhost (or 127.0.0.1 pointing to the loopback interface) is unique per container. Connecting to this will connect to the container itself, and not services running outside of docker or inside of a different docker container.

Option 1: If your dependency can be moved into a container, I would do this first. It makes your application stack portable as others try to run your container on their own environment. And you can still publish the port on your host where other services that have not been migrated can still reach it. You can even publish the port to the localhost interface on your docker host to avoid it being externally accessible with a syntax like: -p 127.0.0.1:3306:3306 for the published port.

Option 2: There are a variety of ways to detect the host IP address from inside of the container, but each have a limited number of scenarios where they work (e.g. requiring Docker for Mac). The most portable option is to inject your host IP into the container with something like an environment variable or configuration file, e.g.:

docker run --rm -e "HOST_IP=$(ip route get 1 | sed -n 's/^.*src \([0-9.]*\) .*$/\1/p')" ...

This does require that your service is listening on that external interface, which could be a security concern. For other methods to get the host IP address from inside of the container, see this post.

Slightly less portable is to use host.docker.internal. This works in current versions of Docker for Windows and Docker for Mac. And in 20.10, the capability has been added to Docker for Linux when you pass a special host entry with:

docker run --add-host host.docker.internal:host-gateway ...

The host-gateway is a special value added in Docker 20.10 that automatically expands to a host IP. For more details see this PR.

Option 3: Running without network isolation, i.e. running with --net host, means your application is running on the host network namespace. This is less isolation for the container, and it means you cannot access other containers over a shared docker network with DNS (instead, you need to use published ports to access other containerized applications). But for applications that need to access other services on the host that are only listening on 127.0.0.1 on the host, this can be the easiest option.

Option 4: Various services also allow access over a filesystem based socket. This socket can be mounted into the container as a bind mounted volume, allowing you to access the host service without going over the network. For access to the docker engine, you often see examples of mounting /var/run/docker.sock into the container (giving that container root access to the host). With mysql, you can try something like -v /var/run/mysqld/mysqld.sock:/var/run/mysqld/mysql.sock and then connect to localhost which mysql converts to using the socket.

回眸一遍 2025-02-06 16:16:50

非常简单快捷,使用 IFCONFIG (Linux)或 ipconfig (Windows)检查主机IP,然后创建 docker-compose.yml

version: '3' # specify docker-compose version
services:
  nginx:
    build: ./ # specify the directory of the Dockerfile
    ports:
      - "8080:80" # specify port mapping
    extra_hosts:
      - "dockerhost:<yourIP>"

这样,您的容器将能够访问主机。访问数据库时,请记住使用您之前指定的名称,在这种情况下, dockerhost 和主机的 port 正在运行的DB。

Very simple and quick, check your host IP with ifconfig (linux) or ipconfig (windows) and then create a docker-compose.yml:

version: '3' # specify docker-compose version
services:
  nginx:
    build: ./ # specify the directory of the Dockerfile
    ports:
      - "8080:80" # specify port mapping
    extra_hosts:
      - "dockerhost:<yourIP>"

This way, your container will be able to access your host. When accessing your DB, remember to use the name you specified before, in this case dockerhost and the port of your host in which the DB is running.

浪漫人生路 2025-02-06 16:16:50

Windows 10

docker社区版17.06.0-ce-win18 2017-06-28(稳定)

您可以使用主机<代码的DNS名称> docker.for.win.localhost ,以解决内部IP。 (警告一些提到的

来源
我需要做类似的操作,这是从我的docker容器到我的本地主机的连接,该容器正在运行 Azure Storage Emulator cosmosdb emulator

Azure存储模拟器默认情况下 127.0.0.1 上听,虽然您也可以更改IP的界限,但我正在寻找可以使用默认设置的解决方案。

这也适用于从我的docker容器连接到 sql Server iis ,均使用默认端口设置在我的主机上运行。

Solution for Windows 10

Docker Community Edition 17.06.0-ce-win18 2017-06-28 (stable)

You can use DNS name of the host docker.for.win.localhost, to resolve to the internal IP. (Warning some sources mentioned windows but it should be win)

Overview

I needed to do something similar, that is connect from my Docker container to my localhost, which was running the Azure Storage Emulator and CosmosDB Emulator.

The Azure Storage Emulator by default listens on 127.0.0.1, while you can change the IP its bound too, I was looking for a solution that would work with default settings.

This also works for connecting from my Docker container to SQL Server and IIS, both running locally on my host with default port settings.

探春 2025-02-06 16:16:50

对于Windows,

我已更改了Spring配置中的数据库URL: Spring.datasource.URL = JDBC:Postgresql://host.docker.internal:5432/apidb

,然后构建图像并运行。它对我有用。

For windows,

I have changed the database url in spring configuration: spring.datasource.url=jdbc:postgresql://host.docker.internal:5432/apidb

Then build the image and run. It worked for me.

墨落成白 2025-02-06 16:16:50

对于Linux,您无法更改界面的界面localhost服务绑定到与

两个问题,使我们

  1. 主机的IP的
  2. 获取

可以使用Qoomon's docker-host图像,其他答案给出。

您需要将此容器与其他容器相同的桥网络添加到同一桥网络中,以便您可以访问它。在容器内打开终端,并确保您可以ping dockerhost

bash-5.0# ping dockerhost
PING dockerhost (172.20.0.2): 56 data bytes
64 bytes from 172.20.0.2: seq=0 ttl=64 time=0.523 ms

现在,更困难的问题,使Docker可以访问该服务。

我们可以使用Telnet检查是否可以访问主机上的端口(您可能需要安装此端口)。

问题在于,我们的容器只能访问绑定到所有接口的服务,例如SSH:

bash-5.0# telnet dockerhost 22
SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3

但是仅绑定到Localhost的服务将无法访问:

bash-5.0# telnet dockerhost 1025
telnet: can't connect to remote host (172.20.0.2): Connection refused

此处的适当解决方案是将服务绑定到Dockers Bridge网络。但是,此答案假定您无法更改此问题。因此,我们将使用 iptables

首先,我们需要找到Docker使用 ifConfig 的桥梁网络的名称。如果您使用的是未命名的桥梁,则仅是 docker0 。但是,如果您使用的是命名网络,则将有一个桥梁,从 br - 开始使用该码头。我的是 BR-5CD80298D6F4

一旦有了这座桥的名称,我们就需要允许从这座桥到Localhost的路线。默认情况下,由于安全原因,这将被禁用:

sysctl -w net.ipv4.conf.<bridge_name>.route_localnet=1

现在设置我们的 iptables 规则。由于我们的容器只能在Docker Bridge网络上访问端口,因此我们将假装我们的服务实际上绑定到该网络上的端口。

为此,我们将把所有请求转发到&lt; docker_bridge&gt;:port to localhost:port

iptables -t nat -A PREROUTING -p tcp -i <docker_bridge_name> --dport <service_port> -j DNAT --to-destination 127.0.0.1:<service_port>

例如,对于我在端口1025上的服务,

iptables -t nat -A PREROUTING -p tcp -i br-5cd80298d6f4 --dport 1025 -j DNAT --to-destination 127.0.0.1:1025

您现在应该能够访问您从容器中的服务:

bash-5.0# telnet dockerhost 1025
220 127.0.0.1 ESMTP Service Ready

For Linux, where you cannot change the interface the localhost service binds to

There are two problems we need to solve

  1. Getting the IP of the host
  2. Making our localhost service available to Docker

The first problem can be solved using qoomon's docker-host image, as given by other answers.

You will need to add this container to the same bridge network as your other container so that you can access it. Open a terminal inside your container and ensure that you can ping dockerhost.

bash-5.0# ping dockerhost
PING dockerhost (172.20.0.2): 56 data bytes
64 bytes from 172.20.0.2: seq=0 ttl=64 time=0.523 ms

Now, the harder problem, making the service accessible to docker.

We can use telnet to check if we can access a port on the host (you may need to install this).

The problem is that our container will only be able to access services that bind to all interfaces, such as SSH:

bash-5.0# telnet dockerhost 22
SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3

But services bound only to localhost will be inaccessible:

bash-5.0# telnet dockerhost 1025
telnet: can't connect to remote host (172.20.0.2): Connection refused

The proper solution here would be to bind the service to dockers bridge network. However, this answer assumes that it is not possible for you to change this. So we will instead use iptables.

First, we need to find the name of the bridge network that docker is using with ifconfig. If you are using an unnamed bridge, this will just be docker0. However, if you are using a named network you will have a bridge starting with br- that docker will be using instead. Mine is br-5cd80298d6f4.

Once we have the name of this bridge, we need to allow routing from this bridge to localhost. This is disabled by default for security reasons:

sysctl -w net.ipv4.conf.<bridge_name>.route_localnet=1

Now to set up our iptables rule. Since our container can only access ports on the docker bridge network, we are going to pretend that our service is actually bound to a port on this network.

To do this, we will forward all requests to <docker_bridge>:port to localhost:port

iptables -t nat -A PREROUTING -p tcp -i <docker_bridge_name> --dport <service_port> -j DNAT --to-destination 127.0.0.1:<service_port>

For example, for my service on port 1025

iptables -t nat -A PREROUTING -p tcp -i br-5cd80298d6f4 --dport 1025 -j DNAT --to-destination 127.0.0.1:1025

You should now be able to access your service from the container:

bash-5.0# telnet dockerhost 1025
220 127.0.0.1 ESMTP Service Ready
薄凉少年不暖心 2025-02-06 16:16:50

当在Windows 10 Home上使用Docker Toolbox时,这些答案都不适合我,但是 10.0.2.2 确实如此,因为它使用了将主机公开到此地址上的VM的VirtualBox。

None of the answers worked for me when using Docker Toolbox on Windows 10 Home, but 10.0.2.2 did, since it uses VirtualBox which exposes the host to the VM on this address.

苦笑流年记忆 2025-02-06 16:16:50

这不是对实际问题的答案。这就是我解决类似问题的方式。该解决方案完全来自:定义docker容器网络,以便容器可以通信。感谢Nic Raboy

将其留在这里,他们可能想在一个容器和另一个容器之间打电话。回答问题:在Docker环境中可以用Localhost使用什么?

获取您的网络看起来像 docker网络LS

创建一个新的网络 Docker Network创建-D MY -NET

启动第一个容器 Docker Run -d -d -P 5000: 5000 - network =“ my-net” -name“ first_container”&lt; myimage1:v0.1&gt;

查看第一个容器 docker Inspect first_container 的网络设置。 “网络”:应该有'my -net'

启动第二个容器 docker run -d -d -p 6000:6000 -network =“ my -neT” ---name“ second_container”&lt; myimage2:v0.1&gt;

查看第二个容器的网络设置 Docker Inspect sectect_container 。 “ Networks”:应将'my -net'ssh

纳入您的第二个容器 docker exec -it second_container sh docker exec -it second_container bash

在第二个容器内部,您可以通过 ping first_container ping第一个容器。另外,您的代码调用,例如 http:// localhost:5000 可以由 http:// first_container替换:5000

This is not an answer to the actual question. This is how I solved a similar problem. The solution comes totally from: Define Docker Container Networking so Containers can Communicate. Thanks to Nic Raboy

Leaving this here for others who might want to do REST calls between one container and another. Answers the question: what to use in place of localhost in a docker environment?

Get how your network looks like docker network ls

Create a new network docker network create -d my-net

Start the first container docker run -d -p 5000:5000 --network="my-net" --name "first_container" <MyImage1:v0.1>

Check out network settings for first container docker inspect first_container. "Networks": should have 'my-net'

Start the second container docker run -d -p 6000:6000 --network="my-net" --name "second_container" <MyImage2:v0.1>

Check out network settings for second container docker inspect second_container. "Networks": should have 'my-net'

ssh into your second container docker exec -it second_container sh or docker exec -it second_container bash.

Inside of the second container, you can ping the first container by ping first_container. Also, your code calls such as http://localhost:5000 can be replaced by http://first_container:5000

烟酒忠诚 2025-02-06 16:16:50

如果您使用-NET =主机跑步,Localhost应该正常工作。如果您使用默认网络,请使用静态IP 172.17.0.1。

请参阅此 - https://stackoverflow.com/a/48547074/14120621

If you're running with --net=host, localhost should work fine. If you're using default networking, use the static IP 172.17.0.1.

See this - https://stackoverflow.com/a/48547074/14120621

蓝礼 2025-02-06 16:16:50

对于Windows上的人,假设您使用的是桥梁网络驱动程序,则需要专门将MySQL绑定到Hyper-V网络接口的IP地址。

这是通过正常隐藏的 c:\ programData \ mysql 文件夹下的配置文件完成的。

绑定到 0.0.0.0 将不起作用。所需的地址也显示在Docker配置中,在我的情况下为 10.0.75.1

For those on Windows, assuming you're using the bridge network driver, you'll want to specifically bind MySQL to the IP address of the hyper-v network interface.

This is done via the configuration file under the normally hidden C:\ProgramData\MySQL folder.

Binding to 0.0.0.0 will not work. The address needed is shown in the docker configuration as well, and in my case was 10.0.75.1.

时光无声 2025-02-06 16:16:50

首先,请参见 this 答案是您必须解决此问题的选项。但是,如果您使用 Docker-Compose 可以将 network_mode:host 添加到您的服务中,然后使用 127.0.0.1 将其连接到本地主机。这只是上面答案中描述的选项之一。下面您可以从 https://github.com/geerlingguy/php-apache-container.git

 ---
 version: "3"
 services:
   php-apache:
+    network_mode: host
     image: geerlingguy/php-apache:latest
     container_name: php-apache
...

+<<+< /代码>指示我添加的行。


[其他信息]这也可以在版本 2.2 中使用。和“主机”或仅仅是'主机'都在 docker-compose 中工作。

 ---
 version: "2.2"

 services:
   php-apache:
+    network_mode: "host"
        or
+    network_mode: host
...

First see this answer for the options that you have to fix this problem. But if you use docker-compose you can add network_mode: host to your service and then use 127.0.0.1 to connect to the local host. This is just one of the options described in the answer above. Below you can find how I modified docker-compose.yml from https://github.com/geerlingguy/php-apache-container.git:

 ---
 version: "3"
 services:
   php-apache:
+    network_mode: host
     image: geerlingguy/php-apache:latest
     container_name: php-apache
...

+ indicates the line I added.


[Additional info] This has also worked in version 2.2. and "host" or just 'host' are both worked in docker-compose.

 ---
 version: "2.2"

 services:
   php-apache:
+    network_mode: "host"
        or
+    network_mode: host
...
倾城泪 2025-02-06 16:16:50

编辑:我最终在Github上制定了该概念。 Check out: https://github.com/sivabudh/system-in- A-box


首先,我的答案是针对2组人:使用Mac的人和使用Linux的人。

主机网络模式在Mac上不起作用。您必须使用IP别名,请参阅: https://stackoverflow.com/a/43541681/2713729

主机网络模式?请参阅: https://docs.docker.com/engine.com/engine/regine/reference/reference/run-run /#/网络插条

其次,对于使用Linux的人(我的直接经验是Ubuntu 14.04 LTS,我很快就会升级到16.04 LTS), ,您可以使在Docker容器中运行的服务连接到 localhost 在Docker主机上运行的服务(例如,笔记本电脑)。

如何?

关键是当您运行Docker容器时,必须使用主机模式运行它。命令看起来像这样:

docker run -network =“ host” -id&lt&docker image id&gt;

当您进行 ifconfig (您需要> code>) apt-get安装net-tools 您的容器在您的容器内进行 ifConfig ),您会看到网络接口与Docker主机上的网络接口相同(例如。笔记本电脑)。

重要的是要注意,我是Mac用户,但是我在相似之处下运行Ubuntu,因此使用Mac并不是一个不利条件。 ;-)

这就是您将NGINX容器连接到在 localhost 上运行的MySQL的方式。

Edit: I ended up prototyping out the concept on GitHub. Check out: https://github.com/sivabudh/system-in-a-box


First, my answer is geared towards 2 groups of people: those who use a Mac, and those who use Linux.

The host network mode doesn't work on a Mac. You have to use an IP alias, see: https://stackoverflow.com/a/43541681/2713729

What is a host network mode? See: https://docs.docker.com/engine/reference/run/#/network-settings

Secondly, for those of you who are using Linux (my direct experience was with Ubuntu 14.04 LTS and I'm upgrading to 16.04 LTS in production soon), yes, you can make the service running inside a Docker container connect to localhost services running on the Docker host (eg. your laptop).

How?

The key is when you run the Docker container, you have to run it with the host mode. The command looks like this:

docker run --network="host" -id <Docker image ID>

When you do an ifconfig (you will need to apt-get install net-tools your container for ifconfig to be callable) inside your container, you will see that the network interfaces are the same as the one on Docker host (eg. your laptop).

It's important to note that I'm a Mac user, but I run Ubuntu under Parallels, so using a Mac is not a disadvantage. ;-)

And this is how you connect NGINX container to the MySQL running on a localhost.

国际总奸 2025-02-06 16:16:50

尝试以下操作:

version: '3.5'
services:
  yourservice-here:
    container_name: container_name
    ports:
      - "4000:4000"
    extra_hosts: # <---- here
      - localhost:192.168.1.202
      - or-vitualhost.local:192.168.1.202

要获取 192.168.1.202 ,使用 ifconfig

这对我有用。希望这个帮助!

Try this:

version: '3.5'
services:
  yourservice-here:
    container_name: container_name
    ports:
      - "4000:4000"
    extra_hosts: # <---- here
      - localhost:192.168.1.202
      - or-vitualhost.local:192.168.1.202

To get 192.168.1.202, uses ifconfig

This worked for me. Hope this help!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文