1

I want to assign static ip to nginx, but it doesn't work at all. I have no idea where I'm doing it wrong. Could you please help?

docker.compose.yml

version: '3'
services:
web:
    container_name: nginx
    image: nginx:latest
    ports:
    - "9000:80"
    volumes:
    - .:/var/www/html/resta
    - ./docker/nginx.conf:/etc/nginx/conf.d/default.conf
    networks:
      default:
          ipv4_address: 172.25.0.9
redis:
    container_name: redis
    image: redis
    ports:
    - "6379:6379"
mysql:
    container_name: mysql
    image: mysql
    ports:
    - "33061:3306"
    - "3306:3306"
    volumes:
    - ./docker/mysql:/var/lib/mysql
    restart: always
    environment:
        MYSQL_ROOT_PASSWORD: password
php:
    container_name: php
    build: docker
    image: php:fpm
    volumes:
    - .:/var/www/html/resta
networks:
default:
  driver: bridge
  ipam:
    config:
        - subnet: 172.25.0.0/24

It gives me such an error.

ERROR: for web  user specified IP address is supported only when connecting to networks with user configured subnets
ERROR: Encountered errors while bringing up the project.

why does it give such an error?

Spartan Troy
  • 111
  • 1
  • 2
  • 1
    You should just leave that block so, docker does automatic IP assignment – saviour123 Jun 07 '19 at 13:57
  • I did the docker-compose down and then it worked when I did the docker-compose up -d. However, in the range I specified with subnet, nginx assigned 172.25.0.9 for ipv4_address, but the gateway seems to be 172.25.0.1. How do I change the gateway part? – Spartan Troy Jun 07 '19 at 14:03

2 Answers2

1

probably you're having an indentation issue. Your services should be indented as child of services:, like below:

version: '3'
services:
  web: [...]
  redis: [...]
  mysql: [...]
  php: [...]
networks:
  default:
    driver: bridge
    ipam:
      config:
        - subnet: 172.25.0.0/24

Your final docker-compose.yml should look like this:

version: '3'
services:
  web:
      container_name: nginx
      image: nginx:latest
      ports:
        - "9000:80"
      volumes:
        - .:/var/www/html/resta
        - ./docker/nginx.conf:/etc/nginx/conf.d/default.conf
      networks:
        default:
            ipv4_address: 172.25.0.9
  redis:
      container_name: redis
      image: redis
      ports:
      - "6379:6379"
  mysql:
      container_name: mysql
      image: mysql
      ports:
        - "33061:3306"
        - "3306:3306"
      volumes:
        - ./docker/mysql:/var/lib/mysql
      restart: always
      environment:
          MYSQL_ROOT_PASSWORD: password
  php:
      container_name: php
      build: docker
      image: php:fpm
      volumes:
      - .:/var/www/html/resta
networks:
  default:
    driver: bridge
    ipam:
      config:
        - subnet: 172.25.0.0/24

Check this link as referrence: Docker Compose Network configuration referrence

Ricardo Silva
  • 205
  • 2
  • 7
  • I did the docker-compose down and then it worked when I did the docker-compose up -d. However, in the range I specified with subnet, nginx assigned 172.25.0.9 for ipv4_address, but the gateway seems to be 172.25.0.1. How do I change the gateway part? – Spartan Troy Jun 07 '19 at 14:05
  • But why you want to change it? By default, you default gateway ip address is the first address of your subnet, your CIDR is /24, which means that your default gateway should be 172.25.0.1. If you want to change it, change your CIDR. [Learn more](https://www.digitalocean.com/community/tutorials/understanding-ip-addresses-subnets-and-cidr-notation-for-networking) – Ricardo Silva Jun 07 '19 at 14:26
  • yes thanks now i could understand.thanks for your help. – Spartan Troy Jun 07 '19 at 14:54
  • I am glad to help mate! – Ricardo Silva Jun 07 '19 at 14:56
  • May I ask you to accept my answer? Thx a lot. – Ricardo Silva Jun 07 '19 at 20:53
0

The error you got: for web user specified IP address is supported only when connecting to networks with user configured subnets. The rule of thumb is for the first docker compose file that creates the first container to define the network subnet to be used by future containers.

Here is an example: Container 1: Last section of Docker Compose file

    networks:
      customnetwork:
        ipv4_address: 172.20.0.10
networks:
  customnetwork:
    ipam:
      config:
        - subnet: 172.20.0.0/24

This will create a network named nginxproxymanager_customnetwork. All future containers should join this network. The last part of the docker compose file for joining should be as follows:

    networks:
      nginxproxymanager_customnetwork:
        ipv4_address: 172.20.0.11
    restart: unless-stopped
volumes:
  portainer_data:
networks:
  nginxproxymanager_customnetwork:
    external: true

Source: Docker Compose for Static/fixed IP