Create Docker Network #
Create a network with Docker this will be permanent and can be reused by docker-composes.
docker network create local-shared-network
How to use network in docker-composes #
In this example we have 2 docker-compose:
First one is to describe our database:
version: "3"
services:
mysql:
image: mysql:9
container_name: mysql_container
restart: always
environment:
MYSQL_ROOT_PASSWORD: your_root_password
MYSQL_DATABASE: your_database_name
MYSQL_USER: your_user_name
MYSQL_PASSWORD: your_user_password
volumes:
- ./mysql_data:/var/lib/mysql
ports:
- "3306:3306"
networks:
- local-shared-network
networks:
local-shared-network:
external: true
Second one is for describe our Application:
version: "3"
services:
phpmyadmin:
image: phpmyadmin
restart: always
ports:
- "8090:80"
environment:
PMA_HOST: mysql_container
MYSQL_ROOT_PASSWORD: your_root_password
networks:
- local-shared-network
networks:
local-shared-network:
external: true
As it in the example above the PHPMyadmin service can access the Database with using, even they are created in different compose project:
# The reference used to point the MySQL container
PMA_HOST: mysql_container # automatically resolved by Docker to 172.0.0.X IP address, thanks to the local-shared-network