docker container in same network cannot reach other container in same network

房东的猫 提交于 2019-12-12 05:39:04

问题


I'm using ubuntu 15.10 on digital ocean

The following works docker network create a

docker run -d --name=nginx --net=a nginx

docker run -it --net=a --name web node bash

apt-get install -yq curl && curl nginx

The opposite, trying to reach the web container from the nginx container, does not work for me.

I go into the web container: docker exec -it web bash

Then I add my index.html file

Then I use http-server to serve an index.html file with the command http-server ./ -p 4200 -a 0.0.0.0 index.html.

http-server returns:

Starting up http-server, serving ./
Available on:
  http:127.0.0.1:4200
  http:172.17.0.5:4200
Hit CTRL-C to stop the server

If I then go into nginx and try curl web:4200 then I get curl: (7) Failed to connect to web port 4200: Connection refused


回答1:


Spun up a fresh Ubuntu 15.10 droplet on DigitalOcean and trying to reproduce this;

Using the quick n dirty curl | sh install method - not best practice, but heck, it's easy:

apt-get install -y curl && curl -fsSL https://get.docker.com | sh

Create network mynetwork and containers weba and webb on that network;

docker network create mynetwork
docker run --net mynetwork --name weba -d node sh -c 'npm install http-server -g && mkdir -p /public && echo "welcome to weba" > /public/index.html && http-server -a 0.0.0.0 -p 4200'
docker run --net mynetwork --name webb -d node sh -c 'npm install http-server -g && mkdir -p /public && echo "welcome to webb" > /public/index.html && http-server -a 0.0.0.0 -p 4200'

Reach webb from inside weba

docker exec -it weba sh -c 'curl http://webb:4200'
# welcome to webb

Reach weba from inside webb

docker exec -it webb sh -c 'curl http://weba:4200'
# welcome to weba

That looks to work for me; is there anything different on your environment?




回答2:


This is a good time to question whether you network isolation is an important part of why you are using containers.

Consider avoiding the issue by running your containers on the same network as the Host OS.

On modern Linux systems running systemd, you have access to the systemd-nspawn container solution, without installing any additional software. It provides process isolation, resource management, chroot'ed environments, and the ability to share the Host OS network with the --network-veth option.



来源:https://stackoverflow.com/questions/34845649/docker-container-in-same-network-cannot-reach-other-container-in-same-network

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!