Docker Compose returns error "networks have overlapping IPv4"

Hello,

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 ?

Thank you in advence!

Probably the most important thing to know here is that Docker manages its own networking in a way that doesn’t usually require manual intervention. More specifically, I would remove every ipv4_address line and the manual subnet specification in the network declaration in your docker-compose.yml file, and let Docker manage this for you.

Given that you do have the network setup, between containers you should be able to use other containers’ names directly as DNS names (like database should be the internally-visible name of your MySQL container). You don’t need to manually specify links, and both links and depends_on are all but no-ops in practice.

The real problem you’ll hit with this setup is that you’re claiming specific host ports, and you can’t run two processes of any sort both listening on e.g. port 8080. That’s tricky to work around. If you have multiple projects and aren’t trying to run multiple copies of this one, you can pick non-conflicting ports, but that leads to the same sort of scaling issues.

Beyond that, if you don’t force specific host ports and aren’t trying to run two database daemons on the same backing directory, you should be able to run multiple copies of this just fine.

@dmaze I’d like to start by thank you for your reply.

As I have described, if I remove the networking information then I will not be able to access the MySQL from none of my other services while I have the volumes directive in the database service.

If you just copy the contents of my docker-compose.yml and try it locally you will see what I mean ( of course you should remove the web service that is based on a Dockerfile and the network information. ).

Once you run the docker-compose up try to access the localhost:8181 which is the phpMyAdmin service. At this point, you will find that either phpMyAdmin can’t connect to MySQL service.

Also, I tried to access the MySQL from my web service using both the mysql, database, database:mysql, db, localhost. I tried every possible combination and none worked.

Apart from that, before setup the network information, I also give it a try to connect using the MySQL Workbench on the MySQL service from my host machine using the localhost and the 3306 port, but again I couldn’t get connected.

And all that happens when I try to use the volumes directive in the database service without using network setup.

That doesn’t really make sense to me; those things are pretty different.

You say you get “an error”; what error do you get?

If I run the following, I can access PHPMyAdmin. In addition to changing the network declarations (to use a locally-declared network, but not specifically request any addresses on it) I set the database host PMA_HOST to database, the name of the Docker Compose service. (And I deleted the MySQL exposed port but that’s not necessary, and in an environment where you have a Web-based database editor you might want that external direct access.)

version: "3.4"
services:
   database:
      image: mysql:latest
      environment:
         MYSQL_ROOT_PASSWORD: password
         MYSQL_DATABASE: appdb
         MYSQL_USER: appuser
         MYSQL_PASSWORD: password
      volumes:
         - $PWD/data/db:/var/lib/mysql
      networks:
          - app_net
   phpmyadmin:
      image: phpmyadmin/phpmyadmin:latest
      ports:
         - "8181:80"
      environment:
         MYSQL_ROOT_PASSWORD: password
         MYSQL_USER: appuser
         MYSQL_PASSWORD: password
         PMA_HOST: database
         PMA_PORT: 3306
         PMA_USER: appuser
         PMA_PASSWORD: password
      networks:
         - app_net
networks:
   app_net:
      driver: bridge

All of this is sounding to me a lot like the database isn’t starting up. When you run docker-compose up, are the messages that it prints out reasonable-looking, or scary-looking? If you delete the data/db directory and start over, is it better?

1 Like

So what you have described worked like a charm !! :slight_smile: Thank you very much for your assistance !! :slight_smile: