Docker-Compose v3 - 静态ip

时间:2017-09-28 06:01:21

标签: docker docker-compose

我正在尝试在我的docker-compose v3文件上设置静态IP但是..我不能。 每次我设置它,我都无法连接到网页。 我得到了ERR_ADDRESS_UNREACHABLE

这是我的配置:

# docker-compose.yml
version: '3'
services:
    web:
        build: ./etc/nginx
        ports:
            - "90:80"
        volumes:
            - "./etc/ssl:/etc/ssl"
        depends_on:
            - php
            - database
    php:
        build: ./etc/php
        ports:
            - 9000:9000
        links:
            - database:mysqldb
        volumes:
            - "./etc/php/php.ini:/usr/local/etc/php/conf.d/php.ini"
            - ${APP_PATH}:/var/www/symfony
    database:
        image: mysql
        environment:
            MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
            MYSQL_DATABASE: ${MYSQL_DATABASE}
            MYSQL_USER: ${MYSQL_USER}
            MYSQL_PASSWORD: ${MYSQL_PASSWORD}
        ports:
            - 3300:3306
        volumes:
            - "./data/db/mysql:/var/lib/mysql"

# docker-compose.override.yml
version: '3'
services:
  web:
    networks:
      test:
        ipv4_address: '10.1.0.100'

networks:
  test:
    ipam:
      driver: default
      config:
        - subnet: 10.1.0.0/24

3 个答案:

答案 0 :(得分:1)

应该是这样的:

services:
  web:
    networks:
      test:
        ipv4_address: 10.1.0.100

networks:
  test:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 10.1.0.0/24

就我而言,我在networks之前放置了services部分。

更新:

最终我最终使用外部网络:

docker network create --subnet 10.5.0.0/24 local_network_dev

然后在任何docker-compose中你可以像这样使用它:

version: '3.2'
networks:
  default:
    external:
      name: local_network_dev

和图像内部:

  web:
    networks:
      default:
        ipv4_address: 10.5.0.11

答案 1 :(得分:1)

# Use root/example as user/password credentials
version: '3.3'

networks:
    netBackEnd:
        ipam:
            driver: default
            config:
                 - subnet: 192.168.0.0/24
services:
  mongo-db:
    image: mongo
    container_name: cnt_mongo
    restart: always
    environment:
      MONGO_INITDB_DATABASE: dbArland
      MONGO_INITDB_ROOT_USERNAME: maguilarac
      MONGO_INITDB_ROOT_PASSWORD: pwdmaguilarac
    ports:
      - 27017:27017
    volumes:
      - ./script1_creacion_usuario.js:/docker-entrypoint-initdb.d/script1_creacion_usuario.js:ro
      - ./script2_creacion_coleccion.js:/docker-entrypoint-initdb.d/script2_creacion_coleccion.js:ro
      - ./script4_carga_productos.js:/docker-entrypoint-initdb.d/script4_carga_productos.js:ro
      - ./productos_inicial.json:/docker-entrypoint-initdb.d/productos_inicial.json:ro
      - ./mongo-volume:/data/db 
    networks:
      netBackEnd:
        ipv4_address: 192.168.0.4
        
  mongo-express:
    image: mongo-express
    container_name: cnt_mongo-express
    restart: always
    ports:
      - 9081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: maguilarac
      ME_CONFIG_MONGODB_ADMINPASSWORD: pwdmaguilarac
    networks:
      netBackEnd:
        ipv4_address: 192.168.0.6

答案 2 :(得分:0)

只是一个非常重要的注释,所有未来的用户都应该知道。

如果您尝试编辑已经存在的网络,则很可能会出现错误 Cannot start service xxx: Invalid address xxx.xxx.xxx.xxx: It does not belong to any of this network's subnets

我已经为这个问题苦苦挣扎了大约2个小时。解决方案是为网络设置其他名称或适当使用docker-compose down

第2版和第3版之间的网络设置没有太大区别,但这是指向官方文档(v3)的良好链接:https://docs.docker.com/compose/compose-file/#ipv4_address-ipv6_address