Reading time: 2 minutes
To connect two separate Docker Compose setups using the same network, you need to create a custom network in Docker and then add all the services from both Docker Compose setups to that custom network.
Here is the updated Docker Compose file for Nginx Proxy Manager with the name of the custom network my-network
added:
version: '3.7' services: app: image: 'jc21/nginx-proxy-manager:latest' ports: - '80:80' - '81:81' - '443:443' environment: DB_MYSQL_HOST: "db" DB_MYSQL_PORT: 3306 DB_MYSQL_USER: "npm" DB_MYSQL_PASSWORD: "npm" DB_MYSQL_NAME: "npm" volumes: - ./data:/data - ./letsencrypt:/etc/letsencrypt - ./dhparam:/etc/nginx/dhparam depends_on: - db networks: my-network: aliases: - proxy restart: always db: image: 'jc21/mariadb-aria:latest' environment: MYSQL_ROOT_PASSWORD: 'npm' MYSQL_DATABASE: 'npm' MYSQL_USER: 'npm' MYSQL_PASSWORD: 'npm' volumes: - ./data/mysql:/var/lib/mysql networks: my-network: aliases: - db restart: always networks: my-network: driver: bridge
Here is the updated Docker Compose file for PHP + MariaDB + phpMyAdmin with the name of the custom network my-network
added:
version: '3.7' services: web: image: php:7.4-apache ports: - "8000:80" volumes: - ./src:/var/www/html environment: MYSQL_HOST: db MYSQL_DATABASE: myapp MYSQL_USER: user MYSQL_PASSWORD: password depends_on: - db networks: my-network: aliases: - web restart: always db: image: mariadb:latest environment: MYSQL_ROOT_PASSWORD: password MYSQL_DATABASE: myapp MYSQL_USER: user MYSQL_PASSWORD: password volumes: - ./data/mysql:/var/lib/mysql networks: my-network: aliases: - db restart: always phpmyadmin: image: phpmyadmin:latest ports: - "8080:80" environment: PMA_HOST: db MYSQL_ROOT_PASSWORD: password depends_on: - db networks: my-network: aliases: - phpmyadmin restart: always networks: my-network: external: true
The Docker Compose file for PHP + MariaDB + phpMyAdmin uses the custom network my-network
created by the Docker Compose for Nginx Proxy Manager. The last networks
section ensures that Docker Compose looks for the custom network my-network
created in the Docker Compose for Nginx Proxy Manager.
To run them, use the following command:
docker-compose -p nginx-proxy -f /path/to/docker-compose-nginx-proxy.yml up -d && docker-compose -p php-app -f /path/to/docker-compose-php-app.yml up -d
With this configuration, both Docker Compose setups communicate with each other through the custom network my-network
! Services can access other services using the aliases configured in the networks
sections of each Docker Compose.
For example, the web service in the Docker Compose for PHP + MariaDB + phpMyAdmin can access the database service in the Docker Compose for Nginx Proxy Manager using the alias db
. Similarly, the Nginx Proxy Manager service can access the web service in the Docker Compose for PHP + MariaDB + phpMyAdmin using the alias web
.