Skip to main content Docker Compose can I assign any ipv4 address? : r/docker
r/docker icon
Go to docker
•

Docker Compose can I assign any ipv4 address?

Hello, so I have following Docker Compose yml file and would like to know if I can assign any IPv4 address to the Containers or does it strictly have to be within the 172.x.x.x range? Essentially the goal is to have the Containers communicate with each other and the Host.

version: "3.7"
services:
  db:
    image: postgres
    networks:
      testnet:
        ipv4_address: 192.168.50.20
    ports:
      - "8001:5432"
  web:
    image: nginx:latest
    networks:
        testnet:
          ipv4_address: 192.168.50.10
    ports:
      - "8000:8000"
networks:
  testnet:
    ipam:
      driver: default
      config:
      - subnet: 192.168.50.0/24
How to responsibly select AI for your organization
Thumbnail image: How to responsibly select AI for your organization
Sort by:
Best
Open comment sort options

Also, instead of hardcoded IP addresses to use for communication, you should look into network aliases for Docker containers. Docker has an internal DNS service which allows for you to reference a Docker container's IP address by either it's container name or network alias. This allows for you to not care about what IP address it is given.

So the idea would be that I don't have to know on the spot which IP addresses are taken. I'll read the documentation on that, thank you.

More replies
More replies

You should be able to use any subnet on the bridge as long as the subnet isn't used on another interface on the host.

Thanks for the quick answer. I'll double check if that is the case.

More replies

Not sure if you have fixed the problem but this docker compose file will communicate between the wordpress and the maria db containers with no additional setup

wordpress:
  image: wordpress
  links:
    - wordpress_db:mysql
  ports:
    - 80:80
  volumes:
   - ./docker-runtime/wwwroot:/var/www/html
restart: always

wordpress_db:
  image: mariadb
  environment:
MYSQL_ROOT_PASSWORD: rootpass
 restart: always
  volumes:
- ./docker-runtime/sql:/var/lib/mysql