5

I'm using Docker Desktop for Mac and running a container on my host machine. I'm able to access the container via localhost on host machine. But it is not available from other systems on lan.

The container is nginx web server, which is accessible on localhost:80 but not accessible from other systems on same LAN network.

I want other systems to be able to access the container on host machine.

Edit 1: Adding docker-compose configuration and "docker info" command output

version: '2'

services:
  nginx:
    image: artifactory.service.dev:5000/nginx:latest
    network_mode: host
    ports:
      - "80:80"
      - "10001-10020:10001-10020"
      - "8080:8080"
    volumes:
      - ~/docker/.docker/nginx/html:/usr/share/nginx/html

  redis:
    image: artifactory.service.dev:5000/redis:latest
    restart: always
    ports:
      - "6379:6379"

  activemq:
    image: artifactory.service.dev:5000/rmohr/activemq:5.11.1
    restart: always
    ports:
      - "61613:61613"
      - "61616:61616"
      - "8161:8161"

  oracle:
    image: artifactory.service.dev:5000/oracle-12c:latest
    restart: always
    ports:
      - "1521:1521"
    volumes:
      - ~/docker/.docker/oracle:/tmp/oracle:ro
    privileged: true

docker info

Containers: 4
 Running: 3
 Paused: 0
 Stopped: 1
Images: 38
Server Version: 1.12.1
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Backing Filesystem: extfs
 Dirs: 310
 Dirperm1 Supported: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: host bridge null overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Security Options: seccomp
Kernel Version: 4.4.19-moby
Operating System: Alpine Linux v3.4
OSType: linux
Architecture: x86_64
CPUs: 6
Total Memory: 11.71 GiB
Name: moby
ID: LBLG:7UQC:W67Q:J744:QAHE:4JLX:QRVB:2QQD:PTB2:MV75:HD6Y:FROD
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): true
 File Descriptors: 50
 Goroutines: 72
 System Time: 2016-09-01T06:51:40.063477725Z
 EventsListeners: 1
No Proxy: *.local, 169.254/16, *.dev
Registry: https://index.docker.io/v1/
Experimental: true
Insecure Registries:
 artifactory.service.dev:5000
 127.0.0.0/8

command used to start the container:

docker-compose up -d nginx
veben
  • 19,637
  • 14
  • 60
  • 80
Mohan Krishna
  • 352
  • 3
  • 15
  • Find response to this question here: https://stackoverflow.com/questions/33814696/how-to-connect-to-a-docker-container-from-outside-the-host-same-network-windo – Vivek Goel Oct 01 '19 at 11:35

1 Answers1

4

You didn't list what command you're using. This works for me. I can access it using the ip address of my mac from my iphone (same wi-fi network).

docker run -d --name myserver -p 80:80 nginx:1.10-alpine

Edit: sample compose file.

Create a bridge network (backbone) that is internal to your services. They all communicate through this network. The only external access point is through your nginx proxy (ports: section).

Do not use ip address anywhere (eg in nginx.conf). Only use service names. Eg use oracle:1521 to connect to oracle.

Find somewhere else to store your html files. ~/docker/.docker should only be used by docker.

version: "2"

services:
  nginx:
  image: artifactory.service.dev:5000/nginx:latest
  ports:
    - "80:80"
    - "10001-10020:10001-10020"
    - "8080:8080"
  volumes:
    - ~/docker/.docker/nginx/html:/usr/share/nginx/html
  networks:
    - backbone

  redis:
  image: artifactory.service.dev:5000/redis:latest
  restart: always
  expose:
    - "6379"
  networks:
    - backbone

  activemq:
  image: artifactory.service.dev:5000/rmohr/activemq:5.11.1
  restart: always
  expose:
    - "61613"
    - "61616"
    - "8161"
  networks:
    - backbone

  oracle:
  image: artifactory.service.dev:5000/oracle-12c:latest
  restart: always
  expose:
    - "1521"
  volumes:
    - ~/docker/.docker/oracle:/tmp/oracle:ro
  privileged: true
  networks:
    - backbone

networks:

  backbone:
    driver: bridge
Bernard
  • 16,149
  • 12
  • 63
  • 66
  • Thanks for the answer. I have updated the question with docker-compose configuration i am using to start the container – Mohan Krishna Sep 01 '16 at 03:36
  • That's because it's running on the default bridge network. Add `network_mode: host` to your nginx service definition (I'm assuming you're using `version: "2"` compose file, if not convert to version 2 first). https://docs.docker.com/compose/overview/ – Bernard Sep 01 '16 at 05:35
  • I updated my docker-compose configuration with `network_mode: host` still having the same issue. Localhost url is working but not from any systems on same lan network. I'm using version 2 of docker compose. Appreciate your help. – Mohan Krishna Sep 01 '16 at 06:41
  • Append an **Edit ** line to your post above and add your full dockerfile listing. Also add what is printed by `docker info` and the command line you use to start your services (eg: `docker-compose up -d`) – Bernard Sep 01 '16 at 06:43
  • Updated with docker-compose configuration and docker info. – Mohan Krishna Sep 01 '16 at 08:15
  • You've got multiple services which need to talk to each other so they all have to be part of a common network. If you specify `host` for one, they all need to have `network_mode: host`. But forget network=host. It's only good to debug in your case. Remove the network_mode=host from your nginx service. The nginx service should be accessible from any computer on the same network as your mac. Let me add some code to the answer above. – Bernard Sep 01 '16 at 08:35
  • The problem is not with containers talking to each other, that is working fine because i am starting them all at once with `docker-compose up` which is creating a network and adding all the containers to it. The problem is with nginx not accessible from other computers. I tried with changes you suggested still out of luck – Mohan Krishna Sep 02 '16 at 00:56
  • Do you see the port exposed from your mac? `sudo lsof -iTCP -sTCP:LISTEN -P` – Bernard Sep 02 '16 at 01:03
  • I don't see any of the ports of nginx in the output of the above command. I guess the ports are not exposed. – Mohan Krishna Sep 02 '16 at 08:21
  • The culprit was my McAfee firewall, which is blocking incoming connections. Thanks for the help. – Mohan Krishna Dec 29 '16 at 09:42
  • Tried to add a route from host1 to the docker container network inside of host2. Is this possible? I tried to add the static route on host1 with the 172.17.x.x(docker network on host2) and put the gateway as host2. I dont get any packets to host2. Is there something I need to look into? 172.17.0.0 172.31.69.211 255.255.0.0 UG 0 0 0 eth0 is the route on host1 pointing to host2 ip address. – Angelo Apr 29 '20 at 20:39
  • @Angelo You need to use an "overlay" network if you want it to span more than one host. https://docs.docker.com/network/overlay/ – Bernard May 03 '20 at 07:39
  • I got it working with a static route...THanks Bernard – Angelo May 05 '20 at 19:41