0

I work with 2 projects. Each has :

  • 1 docker-compose.yml
  • 3 containers (Apache, PHP, MariaDB)

First application is a website needs to consume seconds application API.

All internet topics and documentation dealing with SINGLE docker-compose architecture and MY issue dealing with 2 specific and separate docker-compose architecture

What is the best networking drivers (bridge, host, overlay, ipvlan, macvlan, none) need to use ?

Is it mandatory to use 'extra_host' config into docker-compose file ?

Do I needs declare networking for each containers ?

Why ping run correctly into same docker-compose containers but not between another docker-compose architecture ? All containers running into same instance of Docker Engine windows...

enter image description here

miltone
  • 4,416
  • 11
  • 42
  • 76
  • The only thing you need to do is make sure the two Compose files' `default` networks are the same network; the linked question has several examples. You should do none of the things you describe in the question – do not manually specify a network driver, do not set `extra_hosts:`, do not set `networks:` per container, and do not run ping(1). – David Maze Mar 23 '23 at 10:51

1 Answers1

1

If not specified otherwise docker-compose will create a docker network called default for each project/docker-compose.yml and connect each service/container of the project to that network.

You can see and inspect these networks with docker network ls. The networks are isolated from one another but the host is connected to all of them as their gateway (at least in the default configuration).

To be able to reach one service of another project you need to connect both services to a common network, for example like this:

docker network create shared_api_network
# project 1
services:
  apache:
    # ...
    networks:
      - default # connect to default network of this project so apache can communicate with the DB
      - shared # connect to the shared network so apache can communicate with API of the other project
  php:
    # ...
  db:
    # ...

networks:
  shared:
    name: shared_api_network
    external: true
# project 2
services:
  apache:
    # ...
    networks:
      - default # connect to default network of this project so apache can communicate with the DB
      - shared # connect to the shared network so the API can be called from other projects
  php:
    # ...
  db:
    # ...

networks:
  shared:
    name: shared_api_network
    external: true

For more see the docker-compose networking docs.

acran
  • 7,070
  • 1
  • 18
  • 35
  • I try to use your example without launch external command prompt – miltone Mar 23 '23 at 14:09
  • Do you mean you want to avoid the `docker network create` command? The you can define the `shared_api_network` network in one of the `docker-compose.yml`s with a [custom network name](https://docs.docker.com/compose/compose-file/06-networks/#name), the other `docker-compose.yml` stays the same as above. – acran Mar 23 '23 at 16:55