本文翻译自:From inside of a Docker container, how do I connect to the localhost of the machine?

So I have a Nginx running inside a docker container, I have a mysql running on localhost, I want to connect to the MySql from within my Nginx. 所以我有一个Nginx在docker容器中运行,我有一个mysql在本地主机上运行,​​我想从我的Nginx内部连接到MySql。 The MySql is running on localhost and not exposing a port to the outside world, so its bound on localhost, not bound on the ip address of the machine. MySql在localhost上运行,并且没有将端口暴露给外界,因此它绑定在localhost上,而不绑定在计算机的IP地址上。

Is there any way to connect to this MySql or any other program on localhost from within this docker container? 有什么方法可以从此Docker容器中连接到此MySql或localhost上的任何其他程序吗?

This question is different from "How to get the IP address of the docker host from inside a docker container" due to the fact that the IP address of the docker host could be the public IP or the private IP in the network which may or may not be reachable from within the docker container (I mean public IP if hosted at AWS or something). 此问题与“如何从Docker容器内部获取Docker主机的IP地址”不同,这是因为Docker主机的IP地址可以是网络中的公共IP或私有IP,这可能是也可能是无法从docker容器中访问(我的意思是公共IP,如果托管在AWS或其他地方)。 Even if you have the IP address of the docker host it does not mean you can connect to docker host from within the container given that IP address as your Docker network may be overlay, host, bridge, macvlan, none etc which restricts the reachability of that IP address. 即使您具有Docker主机的IP地址,也并不意味着您可以从容器内部连接到Docker主机,因为IP地址可能会覆盖您的Docker网络,主机,网桥,macvlan等,从而限制Docker的可达性该IP地址。


#1楼

参考:https://stackoom.com/question/1e2ec/如何从Docker容器内部连接到计算机的本地主机


#2楼

Edit: If you are using Docker-for-mac or Docker-for-Windows 18.03+, just connect to your mysql service using the host host.docker.internal . 编辑:如果您正在使用Docker-for-mac或Docker-for-Windows 18.03+,只需使用主机host.docker.internal连接到您的mysql服务。

As of Docker 18.09.3, this does not work on Docker-for-Linux. 从Docker 18.09.3开始,这不适用于Linux上的Docker。 A fix has been submitted on March the 8th, 2019 and will hopefully be merged to the code base. 已在2019年3月8日提交修复程序 ,有望将其合并到代码库中。 Until then, a workaround is to use a container as described in qoomon's answer . 在此之前,解决方法是使用qoomon的answer中所述的容器。


TLDR TLDR

Use --network="host" in your docker run command, then 127.0.0.1 in your docker container will point to your docker host. --network="host" docker run命令中使用--network="host" ,然后--network="host"容器中的127.0.0.1将指向您的docker主机。

Note: This mode only works on Docker for Linux, per the documentation . 注意: 根据文档 ,此模式仅适用于Linux的Docker。


Note on docker container networking modes 关于Docker容器联网模式的注意事项

Docker offers different networking modes when running containers. 运行容器时,Docker提供了不同的联网模式 。 Depending on the mode you choose you would connect to your MySQL database running on the docker host differently. 根据您选择的模式,您将以不同的方式连接到在docker主机上运行的MySQL数据库。

docker run --network="bridge" (default) docker run --network =“ bridge”(默认)

Docker creates a bridge named docker0 by default. Docker默认创建一个名为docker0的网桥。 Both the docker host and the docker containers have an IP address on that bridge. docker主机和docker容器在该网桥上均具有IP地址。

on the Docker host, type sudo ip addr show docker0 you will have an output looking like: 在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 defaultlink/ether 56:84:7a:fe:97:99 brd ff:ff:ff:ff:ff:ffinet 172.17.42.1/16 scope global docker0valid_lft forever preferred_lft foreverinet6 fe80::5484:7aff:fefe:9799/64 scope linkvalid_lft forever preferred_lft forever

So here my docker host has the IP address 172.17.42.1 on the docker0 network interface. 所以我的172.17.42.1主机在docker0网络接口上的IP地址为docker0

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: 现在启动一个新容器并在其上安装一个外壳: docker run --rm -it ubuntu:trusty 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 1000link/ether 66:32:13:f0:f1:e3 brd ff:ff:ff:ff:ff:ffinet 172.17.1.192/16 scope global eth0valid_lft forever preferred_lft foreverinet6 fe80::6432:13ff:fef0:f1e3/64 scope linkvalid_lft forever preferred_lft forever

Here my container has the IP address 172.17.1.192 . 我的容器的IP地址为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. 因此, 172.17.42.1主机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="host" 泊坞窗运行--network =“ host”

Alternatively you can run a docker container with network settings set to host . 或者,您可以运行将网络设置设置为host的docker容器。 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. 这样的容器将与docker主机共享网络堆栈,从容器的角度来看, localhost (或127.0.0.1 )将引用docker主机。

Be aware that any port opened in your docker container would be opened on the docker host. 请注意,在Docker容器中打开的任何端口都将在Docker主机上打开。 And this without requiring the -p or -P docker run option . 而且这不需要-p-P docker run选项 。

IP config on my docker host: 我的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 1000link/ether 08:00:27:98:dc:aa brd ff:ff:ff:ff:ff:ffinet 10.0.2.15/24 brd 10.0.2.255 scope global eth0valid_lft forever preferred_lft foreverinet6 fe80::a00:27ff:fe98:dcaa/64 scope linkvalid_lft forever preferred_lft forever

and from a docker container in host mode: 并在主机模式下从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 1000link/ether 08:00:27:98:dc:aa brd ff:ff:ff:ff:ff:ffinet 10.0.2.15/24 brd 10.0.2.255 scope global eth0valid_lft forever preferred_lft foreverinet6 fe80::a00:27ff:fe98:dcaa/64 scope linkvalid_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. 如您所见,docker主机和docker容器共享完全相同的网络接口,因此具有相同的IP地址。


Connecting to MySQL from containers 从容器连接到MySQL

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. 要以桥接模式从容器访问在docker主机上运行的MySQL,您需要确保MySQL服务正在侦听172.17.42.1 IP地址上的连接。

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). 为此,请确保您的MySQL配置文件(my.cnf)中具有bind-address = 172.17.42.1bind-address = 0.0.0.0

If you need to set an environment variable with the IP address of the gateway, you can run the following code in a container : 如果您需要使用网关的IP地址设置环境变量,则可以在容器中运行以下代码:

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. 然后在您的应用程序中,使用DOCKER_HOST_IP环境变量打开与MySQL的连接。

Note: if you use bind-address = 0.0.0.0 your MySQL server will listen for connections on all network interfaces. 注意:如果使用bind-address = 0.0.0.0 MySQL服务器将侦听所有网络接口上的连接。 That means your MySQL server could be reached from the Internet ; 这意味着您可以从Internet访问MySQL服务器。 make sure to setup 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 . 注意2:如果您使用bind-address = 172.17.42.1您的MySQL服务器将不会监听与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. 要连接到MySQL的172.17.42.1主机上运行的进程必须使用172.17.42.1 IP地址。

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 all you need to do is to connect to 127.0.0.1 from your containers: 要以主机模式从容器访问在docker主机上运行的MySQL,可以在MySQL配置中保持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>

note: Do use mysql -h 127.0.0.1 and not mysql -h localhost ; 注意:请使用mysql -h 127.0.0.1而不是mysql -h localhost ; otherwise the MySQL client would try to connect using a unix socket. 否则,MySQL客户端将尝试使用unix套接字进行连接。


#3楼

I disagree with the answer from Thomasleveil. 我不同意Thomasleveil的回答。

Making mysql bind to 172.17.42.1 will prevent other programs using the database on the host to reach it. 使mysql绑定到172.17.42.1将阻止其他程序使用主机上的数据库访问它。 This will only work if all your database users are dockerized. 仅当您所有的数据库用户都被docker化后,这才起作用。

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. 使mysql绑定到0.0.0.0将使数据库向外界开放,这不仅是一件很糟糕的事情,而且与原始问题作者想要做的相反。 He explicitly says "The MySql is running on localhost and not exposing a port to the outside world, so its bound on localhost" 他明确表示“ MySql在本地主机上运行,​​并且没有将端口暴露给外界,因此它绑定在本地主机上”

To answer the comment from ivant 回答ivant的评论

"Why not bind mysql to docker0 as well?" “为什么还不将mysql绑定到docker0?”

This is not possible. 这是不可能的。 The mysql/mariadb documentation explicitly says it is not possible to bind to several interfaces. mysql / mariadb文档明确表示不可能绑定到多个接口。 You can only bind to 0, 1, or all interfaces. 您只能绑定到0、1或所有接口。

As a conclusion, I have NOT found any way to reach the (localhost only) database on the host from a docker container. 结论是,我还没有找到从Docker容器访问主机上(仅本地主机)数据库的任何方法。 That definitely seems like a very very common pattern, but I don't know how to do it. 这绝对是一个非常非常常见的模式,但是我不知道该怎么做。


#4楼

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 这在NGINX / PHP-FPM堆栈上为我工作,而无需接触应用程序仅希望能够连接到localhost任何代码或网络

Mount mysqld.sock from the host to inside the container. mysqld.sock从主机安装到容器内部。

Find the location of the mysql.sock file on the host running mysql: 在运行mysql的主机上找到mysql.sock文件的位置:
netstat -ln | awk '/mysql(.*)?\\.sock/ { print $9 }'

Mount that file to where it's expected in the docker: 将该文件挂载到docker中预期的位置:
docker run -v /hostpath/to/mysqld.sock:/containerpath/to/mysqld.sock

Possible locations of 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

#5楼

I doing a hack similar to above posts of get the local IP to map to a alias name (DNS) in the container. 我在做类似于以上文章的技巧,将本地IP映射到容器中的别名(DNS)。 The major problem is to get dynamically with a simple script that works both in Linux and OSX the host IP address . 主要的问题是要使用一个可以在Linux和OSX中运行的主机IP地址的简单脚本来动态获取。 I did this script that works in both environments (even in Linux distribution with "$LANG" != "en_*" configured): 我做了在两个环境中都可以使用的脚本(即使在配置了"$LANG" != "en_*" Linux发行版中):

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: 因此,使用Docker Compose,完整的配置将是:

Startup script (docker-run.sh) : 启动脚本(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 : docker-compose.yml

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

Then change http://localhost to http://dockerhost in your code. 然后在您的代码http://dockerhost http://localhost更改为http://dockerhost

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. 有关如何自定义DOCKERHOST脚本的更高级指南,请查看此帖子,并对其工作方式进行说明。


#6楼

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. 对于Windows上的用户,假设您正在使用网桥网络驱动程序,则需要将MySQL专门绑定到hyper-v网络接口的ip地址。

This is done via the configuration file under the normally hidden C:\\ProgramData\\MySQL folder. 这是通过通常隐藏的C:\\ ProgramData \\ MySQL文件夹下的配置文件完成的。

Binding to 0.0.0.0 will not work. 绑定到0.0.0.0将不起作用。 The address needed is shown in the docker configuration as well, and in my case was 10.0.75.1. 所需的地址也显示在docker配置中,在我的情况下为10.0.75.1。

如何从Docker容器内部连接到计算机的本地主机?相关推荐

  1. docker容器启动后无法访问宿主机host

    项目场景: 项目相关背景: java项目,SpringCloud框架,Consul注册中心. Consul注册中心部署在服务器物理机了,其中在增加了 host配置 127.0.0.1 consul,也 ...

  2. Docker容器无法启动,里面的配置文件如何修改

    背景: 当修改一个容器, 如mysql里的配置文件/etc/mysql/my.cnf , 修改完成后重启mysql 容器, 使用docker ps -a 查看容器,显示容器EXIT(1), 没有正常启 ...

  3. 外部访问docker容器(docker run -p/-P 指令) docker run -d -p 5000:5000 {hostPort:containerPort(映射所有接口地}

    https://www.cnblogs.com/williamjie/p/9915019.html (2)-p(小写)则可以指定要映射的IP和端口,但是在一个指定端口上只可以绑定一个容器.支持的格式有 ...

  4. sql docker容器_了解SQL Server Docker容器中的备份和还原操作

    sql docker容器 In this 17th article of the series (see the full article index at bottom), we will disc ...

  5. docker容器的跨主机访问

    在同一宿主机下的Docker的容器之间是默认互相联通的.通过docker inspect id或name可以查看到ip地址.在不同的容器中来执行ping是可以ping通的,然而跨容器则行不通,于是总结 ...

  6. Prometheus使用cAdvisor监控Docker容器指标

    完整译文请访问:使用cAdvisor监控Docker容器指标. 点击这里获取云原生干货https://www.coderdocument.com/resource_credential.html?co ...

  7. Docker 容器监控方案随手记

    天气:雨转阴 Docker环境准备 官方安装文档 # 卸载旧 软件 yum remove docker \docker-client \docker-client-latest \docker-com ...

  8. 外部访问docker容器(docker run -p/-P 指令)

    容器中可以运行一些网络应用,要让外部也可以访问这些应用,可以通过 -P(大写) 或 -p (小写) 参数来指定端口映射. (1)当使用 -P 标记时,Docker 会随机映射一个 49000~4990 ...

  9. Docker容器总结

    目录 docker出现背景 docker相关注意事项 虚拟机与容器的对比 虚拟机 虚拟机的缺点 linux容器 虚拟机与容器不同点 docker相关网址 docker三要素 docker架构图 doc ...

最新文章

  1. 微软亚研院CV大佬代季峰跳槽商汤为哪般?
  2. python系统提供构造函数传入参数_[ Python入门教程 ] Python函数定义和使用
  3. 中自苏研院2021招聘季开始啦!
  4. MaxCompute(原ODPS) 脑图 - 常用知识总结
  5. docker build命令详解_『中级篇』docker之java容器运行外置springboot-jar(番外篇)(79)...
  6. UVA 10341 二分搜索
  7. Spring中Bean的后置处理器
  8. linux手工迁移php,Linux+PHP+MySql网站迁移配置
  9. HTTP 错误 403.14 - Forbidden Web 服务器被配置为不列出此目录的内容
  10. Oauth 2.0概述
  11. 汇编语言(二十)之分类统计字符个数
  12. parted工具详解
  13. mysql数据操作-数据库的定义-DLL
  14. c++ qml 数组_【QML与C++混合编程】用QVariantList传递数组类型成员
  15. Linux核心进程管理命令
  16. win10电脑右键视频或者图片文件卡死
  17. rogabet notepad 浏览器调用
  18. 记事狗微博php,记事狗微博系统_366rtc 源码采用php实现 - 下载 - 搜珍网
  19. java中的守护线的应用_JVM中的守护线程示例详解
  20. 图像质量评估-BRISQUE

热门文章

  1. Installation error: INSTALL_FAILED_CANCELLED_BY_USER
  2. postInvalidate postInvalidateOnAnimation 区别
  3. android圆角ImageView的几种实现方式
  4. Session的几个主要方法
  5. ipad php mysql_如何用PHP/MySQL为 iOS App 写一个简单的web服务器(译) PART1
  6. android sqlitelog,android-Room-SQLiteLog:(1)SQL变量过多
  7. ubuntu——安装和NS3
  8. tail -f 和 -F 的用法
  9. 2014年百度之星资格赛第一题Energy Conversion
  10. WPF wpf scrollviewer 触屏滚动 窗体弹跳