breq
breq

Reputation: 25516

Docker-Compose v3 - static ip

I am trying to set a static IP on my docker-compose v3 file but.. i can't. Each time I set it, I can't connect to the webpage anymore. I am getting ERR_ADDRESS_UNREACHABLE

Here is my config:

# 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"

and

# 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

Upvotes: 16

Views: 40443

Answers (3)

Kuba Šimonovský
Kuba Šimonovský

Reputation: 2041

Just a very important note that should all future users know.

If you are trying to edit already existing network, you will most likely get error Cannot start service xxx: Invalid address xxx.xxx.xxx.xxx: It does not belong to any of this network's subnets

I have been struggling for about 2 hours with this problem. The solution is to set a different name for the network or propably use docker-compose down.

Networks settings is not much different between versions 2 and 3, but here is a good link to official doc (v3): https://docs.docker.com/compose/compose-file/compose-file-v3/#ipv4_address-ipv6_address

Upvotes: 1

Milton Aguilar
Milton Aguilar

Reputation: 11

# 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

Upvotes: 1

user2932688
user2932688

Reputation: 1704

It should be like this:

services:
  web:
    networks:
      test:
        ipv4_address: 10.1.0.100

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

And in my case I placed networks section before services.

UPDATE:

eventually I've ended up using external network for like this:

docker network create --subnet 10.5.0.0/24 local_network_dev

and then in any docker-compose you can just use it like this:

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

and inside of image:

  web:
    networks:
      default:
        ipv4_address: 10.5.0.11

Upvotes: 20

Related Questions