Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access Docker container from another machine on lan

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
like image 233
Mohan Krishna Avatar asked Sep 01 '16 02:09

Mohan Krishna


1 Answers

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
like image 183
Bernard Avatar answered Oct 09 '22 15:10

Bernard