通过docker-compose为docker容器提供静态IP

时间:2016-09-14 14:48:30

标签: docker docker-compose

我正在尝试为容器提供静态IP地址。我知道我必须创建一个自定义网络。我创建它,并且主机上的桥接接口(Ubuntu 16.x)。容器从该子网获取IP,但不是我提供的静态。

这是我的docker-compose.yml:

version: '2'

services:
  mysql:
    container_name: mysql
    image: mysql:latest
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=root
    ports:
     - "3306:3306"
    networks:
     - vpcbr

  apigw-tomcat:
    container_name: apigw-tomcat
    build: tomcat/.
    ports:
     - "8080:8080"
     - "8009:8009"
    networks:
     - vpcbr
    depends_on:
     - mysql

networks:
  vpcbr:
    driver: bridge
    ipam:
     config:
       - subnet: 10.5.0.0/16
         gateway: 10.5.0.1
         aux_addresses:
          mysql: 10.5.0.5
          apigw-tomcat: 10.5.0.6

容器得到10.5.0.2和10.5.0.3,而不是5和6。

3 个答案:

答案 0 :(得分:79)

请注意,我不建议在Docker中为容器推荐固定的IP,除非您正在做一些允许从外部路由到容器网络内部的内容(例如macvlan)。 DNS已经存在于容器网络内部进行服务发现,并支持容器扩展。在容器网络之外,您应该使用主机上的公开端口。有了这个免责声明,这里有你想要的撰写​​文件:

version: '2'

services:
  mysql:
    container_name: mysql
    image: mysql:latest
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=root
    ports:
     - "3306:3306"
    networks:
      vpcbr:
        ipv4_address: 10.5.0.5

  apigw-tomcat:
    container_name: apigw-tomcat
    build: tomcat/.
    ports:
     - "8080:8080"
     - "8009:8009"
    networks:
      vpcbr:
        ipv4_address: 10.5.0.6
    depends_on:
     - mysql

networks:
  vpcbr:
    driver: bridge
    ipam:
     config:
       - subnet: 10.5.0.0/16
         gateway: 10.5.0.1

答案 1 :(得分:9)

使用自定义名称(而不是KAPACITOR_BASE_URL和KAPACITOR_ALERTS_ENDPOINT的容器名称/端口约定)的环境变量时,我遇到了一些困难。如果在这种情况下提供服务名称,则无法将IP解析为

KAPACITOR_BASE_URL:  http://kapacitor:9092

http://[**kapacitor**]:9092以上无法解析为http://172.20.0.2:9092

我使用子网划分配置解决了静态IP问题。

version: "3.3"

networks:
  frontend:
    ipam:
      config:
        - subnet: 172.20.0.0/24
services:
    db:
        image: postgres:9.4.4
        networks:
            frontend:
                ipv4_address: 172.20.0.5
        ports:
            - "5432:5432"
        volumes:
            - postgres_data:/var/lib/postgresql/data

    redis:
        image: redis:latest
        networks:
            frontend:
                ipv4_address: 172.20.0.6
        ports:
            - "6379"

    influxdb:
        image: influxdb:latest
        ports:
            - "8086:8086"
            - "8083:8083"
        volumes:
            - ../influxdb/influxdb.conf:/etc/influxdb/influxdb.conf
            - ../influxdb/inxdb:/var/lib/influxdb
        networks:
            frontend:
                ipv4_address: 172.20.0.4
        environment:
          INFLUXDB_HTTP_AUTH_ENABLED: "false"
          INFLUXDB_ADMIN_ENABLED: "true"
          INFLUXDB_USERNAME: "db_username"
          INFLUXDB_PASSWORD: "12345678"
          INFLUXDB_DB: db_customers

    kapacitor:
        image: kapacitor:latest
        ports: 
            - "9092:9092"
        networks:
            frontend:
                ipv4_address: 172.20.0.2
        depends_on:
            - influxdb
        volumes:
            - ../kapacitor/kapacitor.conf:/etc/kapacitor/kapacitor.conf
            - ../kapacitor/kapdb:/var/lib/kapacitor
        environment:
          KAPACITOR_INFLUXDB_0_URLS_0: http://influxdb:8086

    web:
        build: .
        environment:
          RAILS_ENV: $RAILS_ENV
        command: bundle exec rails s -b 0.0.0.0
        ports:
            - "3000:3000"
        networks:
            frontend:
                ipv4_address: 172.20.0.3
        links:
            - db
            - kapacitor
        depends_on:
            - db
        volumes:
            - .:/var/app/current
        environment:
          DATABASE_URL: postgres://postgres@db
          DATABASE_USERNAME: postgres
          DATABASE_PASSWORD: postgres
          INFLUX_URL: http://influxdb:8086
          INFLUX_USER: db_username
          INFLUX_PWD: 12345678
          KAPACITOR_BASE_URL:  http://172.20.0.2:9092
          KAPACITOR_ALERTS_ENDPOINT: http://172.20.0.3:3000

volumes:
  postgres_data:

答案 2 :(得分:1)

如果您从未看到静态 IP 地址集,可能是因为您正在使用“docker compose up”。尝试使用“docker-compose up”。

当我使用“docker-compose up”(带连字符)时,我现在可以看到分配的静态 IP。

self.batting_average

我浪费了很多时间来解决这个问题。 :(