Docker Compose returns error “networks have overlapping IPv4”

dockerdocker-compose

starting by saying I am totally new to Docker and I am not yet familiarized with the Docker ecosystem.

What I try to perform is to create a docker-composer.yml to be used in my local dev environment across my projects.

I already use the wp-local-docker for my WordPress projects, but I also have old projects that come with different requirements and for that reason, I did my own docker-compose.yml. The code of my own docker-compose.yml is the following:

version: "3.4"
services:
   database:
      image: mysql:latest
      ports:
         - "3306:3306"
      environment:
         MYSQL_ROOT_PASSWORD: password
         MYSQL_DATABASE: appdb
         MYSQL_USER: appuser
         MYSQL_PASSWORD: password
      volumes:
         - $PWD/data/db:/var/lib/mysql
      networks:
          app_net:
              ipv4_address: 192.168.50.10
   phpmyadmin:
      image: phpmyadmin/phpmyadmin:latest
      links:
         - database:mysql
      ports:
         - "8181:80"
      environment:
         MYSQL_ROOT_PASSWORD: password
         MYSQL_USER: appuser
         MYSQL_PASSWORD: password
         PMA_HOST: mysql
         PMA_PORT: 3306
         PMA_USER: appuser
         PMA_PASSWORD: password
      networks:
         app_net:
            ipv4_address: 192.168.50.11
   web:
      build: $PWD/ApachePHP
      depends_on:
         - database
      links:
         - database:mysql
         - mailcatcher
      ports:
         - "8080:80"
         - "443:443"
      volumes:
         - $PWD/www:/var/www/html
         - $PWD/ApachePHP/000-default.conf:/etc/apache2/sites-available/000-default.conf
      environment:
         - MYSQL_ROOT_PASSWORD=appuser
         - MYSQL_ROOT_USER=password
      networks:
         app_net:
            ipv4_address: 192.168.50.12
   mailcatcher:
      image: schickling/mailcatcher
      ports:
         - "1025:1025"
         - "1080:1080"
      environment:
         MAILCATCHER_PORT: 1025
      networks:
         app_net:
            ipv4_address: 192.168.50.13
networks:
   app_net:
      driver: bridge
      ipam:
         driver: default
         config:
            - subnet: 192.168.50.0/24

The reason I have installed the network is that when I try to access the database server, I get an error related to the network.

After a long time of research, I found that when I use the volumes in database service I get back the error. If you try it, you will find that when you try to access the phpmyadmin you will get the same error.

Again, after a long time of research, I found that I can use the volumes for the database if I set up a network.

Now when I did the first run this worked like a charm. In the test folder, I had the database files in my host folder under the path /data/db

Then I stopped the docker compose using the command docker-compose stop and then I moved to another folder, in which I copied the docker-compose.yml and then I run the docker-compose up.

Unfortunately this time I got the following error:

Creating network "corfurealestatedch_app_net" with driver "bridge"
ERROR: cannot create network 791b388ece09120f1138d48427969c23ded22c6fc73699b7f8c2c8e195b59586 (br-791b388ece09): conflicts with network a675c47764eba17e3338860f56512067df580cef31415906ec57fa3b64f3cdab (br-a675c47764eb): networks have overlapping IPv4

So the question is if I can set up the docker-compose.yml in a such a way that it can be used across my local projects without conflicting with the other containers IPs.

I know, of course I can have a different IP range / sub net for each project, but I am thinking that this could become a mess in a sort period of time.

Any idea on how I can fix that ?

Best Answer

From the error you’ve shown here, it looks to me that docker is trying to create a network with bridge driver named br-791b388ece09, which then complains has an overlapping IP subnet with the bridge br-a675c47764eb.

So first check your available networks using:

$ docker network ls

If you see the bridge with the ID a675c47764eb, make sure this is deleted first before creating new one on the same subnet, for this use:

$ docker network rm a675c47764eb

Then rerun your docker setup for swarm, it should properly create the network.

Alternatively, you can use a different IP subnet. Replace 192.168.50.x with something like 192.168.51.x. This should solve your problem as well.