通过本地网络共享Docker容器并从其他主机访问它

时间:2018-10-08 15:28:41

标签: docker networking docker-compose macvlan

我尝试通过本地网络共享一个容器,以便从同一网络上的另一台计算机访问该容器。我遵循了tihs tutorial(“使用macvlan设备”一节),并成功共享了一个简单的Web容器并从另一个主机进行访问。

但是我要共享的容器稍微复杂一点,因为他通过主机上的内部网络与主机上的其他容器进行了通信。

我尝试绑定在docker-compose中创建的现有容器,但无法访问它。您能帮我吗,或者请告诉我哪里错了?

这是我的docker-compose:

version: "2"
services:
  baseimage:
    container_name: baseimage
    image: base
    build:
      context: ./
      dockerfile: Dockerfile.base
  web:
    container_name: web
    image: web
    env_file:
      - .env
      context: ./
      dockerfile: Dockerfile.web
    extra_hosts:
      - dev.api.exemple.com:127.0.0.1
      - dev.admin.exemple.com:127.0.0.1
      - dev.www.exemple.com:127.0.0.1
    ports:
     - 80:80
     - 443:443
    volumes:
     - ./code:/ass
     - /var/run/docker.sock:/var/run/docker.sock
    tty: true
    dns:
      - 8.8.8.8
      - 8.8.4.4
    links:
      - mysql
      - redis
      - elasticsearch
      - baseimage
    networks:
      devbox:
        ipv4_address: 172.20.0.2
  cron:
    container_name: cron
    image: cron
    build:
      context: ./
      dockerfile: Dockerfile.cron
    volumes:
     - ./code:/ass
    tty: true
    dns:
      - 8.8.8.8
      - 8.8.4.4
    links:
      - web:dev.api.exemple.com
      - mysql
      - redis
      - elasticsearch
      - baseimage
    networks:
      devbox:
        ipv4_address: 172.20.0.3
  mysql:
    container_name: mysql
    image: mysql:5.6
    ports:
      - 3306:3306
    networks:
      devbox:
        ipv4_address: 172.20.0.4
  redis:
    container_name: redis
    image: redis:3.2.4
    ports:
      - 6379:6379
    networks:
      devbox:
        ipv4_address: 172.20.0.5
  elasticsearch:
    container_name: elastic
    image: elasticsearch:2.3.4
    environment:
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    volumes:
      - ./es_data:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
    networks:
      devbox:
        ipv4_address: 172.20.0.6
  chromedriver:
    container_name: chromedriver
    image: robcherry/docker-chromedriver:latest
    privileged: true
    ports:
      - 4444:4444
    environment:
      - CHROMEDRIVER_WHITELISTED_IPS='172.20.0.2'
      - CHROMEDRIVER_URL_BASE='wd/hub'
      - CHROMEDRIVER_EXTRA_ARGS='--ignore-certificate-errors'
    networks:
      devbox:
        ipv4_address: 172.20.0.7
    links:
      - web:dev.www.exemple.com
networks:
  devbox:
    driver: bridge
    driver_opts:
      com.docker.network.enable_ipv6: "false"
    ipam:
      driver: default
      config:
        - subnet: 172.20.0.0/16
          gateway: 172.20.0.1

1 个答案:

答案 0 :(得分:0)

创建一个external网络,将external网络和devbox网络分配给web。然后,可以通过external网络公共ip地址公开访问Web,并使用devbox网络与内部服务进行通信。

将尽快发布工作示例