简体   繁体   中英

accessing host's virtual machine from docker container

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

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

EDIT: 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:

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"

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 .

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.

There are other ways to do this as well.

EDIT - when you are using --net=host then you have access to the host network interface and wont need linking between the containers as they will already have a command interface.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM