从Docker容器访问主机的虚拟机 [英] accessing host's virtual machine from docker container

查看:722
本文介绍了从Docker容器访问主机的虚拟机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在本地计算机(Mac)上使用名为sqlvm的旧版虚拟机进行工作(意味着我可以通过从本地主机访问此虚拟机。 http:// sqlvm :)。
现在,我在应该连接到虚拟机的同一本地主机(我的Mac)中设置了几个Docker容器(使用docker-compose)。 pymysql 引发异常:

I'm working on local machine (Mac) with legacy virtual machine within it called sqlvm (mean I can access this vm from local host via http://sqlvm:). Now I set up few docker containers (using docker-compose) in the same local host (my Mac) which should connect to the vm. The pymysql raise an exception:

OperationalError: (OperationalError) (2003, "Can't connect to MySQL server on 'sqlvm' ([Errno -2] Name or service not known)") None None

如何将外部 sqlvm暴露给内部docker网络?

How can I expose the outside 'sqlvm' to the internal docker network?

编辑:
我尝试为yml文件
中的相关容器添加 net: host 并得到以下错误(在 docker-compose up ):

I've tried to add net: "host" for the relevant container in the yml file and get the following error (on docker-compose up):

ERROR: for defaultworker  Cannot create container for service defaultworker: Conflicting options: host type networking can't be used with links. This would result in undefined behavior

我需要容器与外部和内部进行通信网络。

I need the container to communicate with both 'outside' and 'inside' networks..

docker-compose.yml:

docker-compose.yml:

recommendation-rabbit:
  image: rabbitmq:3-management
  hostname: my-rabbit
  ports:
    - "8080:15672"
    - "5672:5672"
  command: bash -c "rabbitmq-server start"
defaultworker:
  image: rcom-worker
  hostname: rcom-worker-host
  environment:
    - CELERY_BROKER_URL=amqp://user:pass@my-rabbit:5672
  command: bash -c "celery worker -A app -l info -Q default -c 2 -n defaultworker -Ofair"
  links:
    - recommendation-rabbit:rabbit
  net: "host"


推荐答案

要快速解决此问题,您可以运行带有标志-net = host 的所有容器,这意味着Docker容器将使用主机网络接口。可以在此处找到有关Docker网络的更多信息。

For a quick work around you can run all the containers with flag --net=host which means the docker containers will use the host network interface. more info on docker networking can be found here.

您可以对虚拟机执行相同的操作。所以现在vm和docker容器将具有相同的网络接口,因此 pymysql 将可以工作。

You can do the same thing with the vm. so now the vm and the docker containers will have the same network interface and hence pymysql will work.

编辑-当您使用-net = host 时,

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
    link/ether a0:48:1c:10:43:97 brd ff:ff:ff:ff:ff:ff
3: wlan1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether fc:4d:d4:50:23:04 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.107/24 brd 192.168.1.255 scope global wlan1
       valid_lft forever preferred_lft forever
    inet6 fe80::fe4d:d4ff:fe50:2304/64 scope link 
       valid_lft forever preferred_lft forever
4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN 
    link/ether 02:42:c8:46:60:1c brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:c8ff:fe46:601c/64 scope link 
       valid_lft forever preferred_lft forever

这对我来说是这样。因此可以观察到存在 wlan1 ,它是主机的网络接口。
现在,虚拟机应该将mysql服务器的端口公开给主机,因此,如果可以使用主机访问它,那么可以使用容器对其进行访问。

this is how it looks like for me. so it can be observed that wlan1 is present which is the network interface of the host machine. Now the vm should expose the port of the mysql server to the host machine so if you can access it using the host then it can be accessed using the containers.

这篇关于从Docker容器访问主机的虚拟机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆