Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 457
breq Avatar asked Sep 28 '17 06:09

breq


People also ask

Can a Docker container have a static IP?

Static IP addresses don't change when containers or services are stopped and started, making them useful for permanent networking. Assigning Docker containers static IP addresses is an easy way to make them more accessible.

What is Depends_on in Docker compose?

depends_on is a Docker Compose keyword to set the order in which services must start and stop. For example, suppose we want our web application, which we'll build as a web-app image, to start after our Postgres container.


2 Answers

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
like image 168
user2932688 Avatar answered Sep 24 '22 19:09

user2932688


# 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
like image 28
Milton Aguilar Avatar answered Sep 25 '22 19:09

Milton Aguilar