0

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?

  • 1
    You should just leave that block so, docker does automatic IP assignment – saviour123 Jun 7 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 7 at 14:03
0

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

  • 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 7 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 – Ricardo Silva Jun 7 at 14:26
  • yes thanks now i could understand.thanks for your help. – Spartan Troy Jun 7 at 14:54
  • I am glad to help mate! – Ricardo Silva Jun 7 at 14:56
  • May I ask you to accept my answer? Thx a lot. – Ricardo Silva Jun 7 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.