Nginx – docker-compose static ip does not work

docker-composenginxstatic-ip

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?

Best Answer

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

Related Topic