Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker compose yml static IP addressing

I have such docker-compose.yml (not a full list here):

version: '2'

services:

  nginx:
    build: ./nginx/
    ports:
      - 8080:80
    links:
      - php
    volumes_from:
      - app
    networks:
      app_subnet:
        ipv4_address: 172.16.1.3

  php:
    build: ./php/
    expose:
      - 9000
    volumes_from:
      - app
    networks:
      app_subnet:
        ipv4_address: 172.16.1.4

    networks:
      app_subnet:
        driver: bridge
        ipam:
        config:
          - subnet: 172.16.1.0/24
          gateway: 172.16.1.1

After docker-compose up I got such an error:

User specified IP address is supported only when connecting to networks with user configured subnets

So I'm creating subnet with docker network create --gateway 172.16.1.1 --subnet 172.16.1.0/24 app_subnet

But this doesn't solve the problem because docker-compose creates the subnet with name dev_app_subnet on the fly. And my subnet is not used - I'm getting the same error. The main purpose of doing this is to assign static IP for nginx service - open my project web url from etc/hosts file.

like image 912
alvery Avatar asked Oct 17 '16 09:10

alvery


People also ask

Can a Docker container have its own IP address?

By default, the container is assigned an IP address for every Docker network it connects to. And each network is created with a default subnet mask, using it as a pool later on to give away the IP addresses.

What IP is 172.17 0.1 Docker?

Listen to Connections in the Docker Network The bridge connection docker0 – with IP address 172.17. 0.1 – is created by Docker at installation time. Because the host and all containers are connected to that network, our application only needs to listen to it.


2 Answers

[SOLVED] Found the solution. When pointing to the network, we should use flag "external" telling compose that network is already created and should be taken outside (otherwise it will be created on the fly with project prefix):

networks:
  app_subnet:
    external: true

So, after that docker-compose will attach containers to existing app_subnet Before that the subnet must be created:

docker network create --gateway 172.16.1.1 --subnet 172.16.1.0/24 app_subnet
like image 73
alvery Avatar answered Sep 23 '22 10:09

alvery


In my case is i first run docker-compose up failed, but the network already created, can see using docker network ls.

In this case, just docker-compose down, fix the yml, rerun docker-compose up is fine

like image 38
tony.hokan Avatar answered Sep 23 '22 10:09

tony.hokan