简体   繁体   中英

Connect to docker0 on host from docker container

Is there a way to access a service (eg Postfix) which is running on the host machine from inside my docker container?

Here is my docker compose file

version: '2'

networks:
   customer:

backend:
   image: backend:2.0
   mem_limit: 5g
   restart: always
   networks:
      - customer
   ... 

on Host System

> ip route show
172.17.0.0/16 dev docker0  proto kernel  scope link  src 172.17.0.1 
172.18.0.0/16 dev br-48c656087ada  proto kernel  scope link  src 172.18.0.1 

> docker network ls
86ec0ba25160        bridge                            bridge              local     
48c656087ada        customer_customer         bridge              local  

For testing I bound a python socket script to 0.0.0.0 Port 25 but a telnet from inside a container won't work. I also tried to bind the socket to the docker bridge (172.17.0.1) as well as to the network bridge (172.18.0.1). Same result.

on Container

> ip route show | awk '/default/ {print $3}'
172.18.0.1

> telnet 172.18.0.1 25
telnet: Unable to connect to remote host: Connection refused

When running docker on Linux, to be able to connect to the host machine from within the container, you need to start the container with network mode host:

...
backend:
   image: backend:2.0
   mem_limit: 5g
   restart: always
   networks:
      - customer
   network_mode: "host"

Alternatively, you can find the host IP on docker0 interface using ifconfig . In your case it is 172.17.0.1 and you should use that to connect from the container.

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