alvery
alvery

Reputation: 1983

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.

Upvotes: 28

Views: 66049

Answers (5)

alvery
alvery

Reputation: 1983

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

Upvotes: 39

Muhammad Tariq
Muhammad Tariq

Reputation: 4714

Adding to the Qiushi's answer, you can use the docker-compose 3.9 version to specify the external network as below.

version: "3.9"
networks:
  network1:
    external: true
    name: etl_subnet

Ref: https://docs.docker.com/compose/compose-file/compose-file-v3/#network-configuration-reference

Upvotes: 3

ale_tri
ale_tri

Reputation: 423

It is probable that from previous run of the script the network interface was already created but without the subnet parameter.

For fixing it run

docker network ls -a

and remove the network that is blocking the creation of the service

docker network rm <network interface id> 

Upvotes: 9

tony.hokan
tony.hokan

Reputation: 571

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

Upvotes: 13

Qiushi
Qiushi

Reputation: 223

To be specific if you're using docker stack deploy to deploy a swarm cluster. And you need to specify the scope of the subnet as 'swarm':

docker network create --gateway 172.16.1.1 --subnet 172.16.1.0/24 --scope swarm app_subnet

in the docker-compose.yml, you need to specify the external network as:

networks: default: external: name: etl_subnet

Use it as default.

Upvotes: 1

Related Questions