简体   繁体   中英

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

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

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

Using the quick n dirty curl | sh 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?

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.

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