5.1 Setting up networks
Let's dive deep once again into configuring networks and volumes for our multi-container app. This ensures proper interaction between services and long-term data storage.
Docker networks let containers interact with each other. For our app, we are gonna use a bridge network to connect the frontend, backend, and database.
Creating a network
We've already defined the task-network in the docker-compose.yml file:
networks:
task-network:
driver: bridge
Where:
- task-network: the name of the network that'll be used for container interaction.
- driver:
bridge: the network type. Thebridgenetwork is the default for connecting containers on the same host.
Connecting services to the network
We connected all services to this network in the docker-compose.yml file. Let's make sure every service is connected properly:
Example for the frontend:
frontend:
build: ./frontend
ports:
- "3000:3000"
networks:
- task-network
Example for the backend:
backend:
build: ./backend
ports:
- "5000:5000"
depends_on:
- database
networks:
- task-network
environment:
- DATABASE_URL=postgresql://taskuser:taskpassword@database:5432/taskdb
Example for the database:
database:
image: postgres:13
environment:
- POSTGRES_DB=taskdb
- POSTGRES_USER=taskuser
- POSTGRES_PASSWORD=taskpassword
networks:
- task-network
volumes:
- db-data:/var/lib/postgresql/data
5.2 Volume Setup
Docker volumes let you save container data on your host system, making it persistent. In our app, we'll use a volume to store PostgreSQL database data.
Creating a volume
We've already defined the volume db-data in the compose.yaml file:
volumes:
db-data:
Where:
-
db-data: the name of the volume that'll be used to store PostgreSQL database data.
Connecting the volume to the database service
We linked the volume db-data to the database service in the docker-compose.yml file. Let’s double-check this is done right:
An example for the database:
database:
image: postgres:13
environment:
- POSTGRES_DB=taskdb
- POSTGRES_USER=taskuser
- POSTGRES_PASSWORD=taskpassword
networks:
- task-network
volumes:
- db-data:/var/lib/postgresql/data
Where:
-
volumes: defines which volumes will be attached to the container. -
db-data:/var/lib/postgresql/data: ties thedb-datavolume to/var/lib/postgresql/datadirectory inside the container. This directory is used by PostgreSQL to store data.
5.3 Full compose.yaml file
For clarity and completeness, here’s the full compose.yaml file, including network and volume setups:
version: '3'
services:
frontend:
build: ./frontend
ports:
- "3000:3000"
networks:
- task-network
backend:
build: ./backend
ports:
- "5000:5000"
depends_on:
- database
networks:
- task-network
environment:
- DATABASE_URL=postgresql://taskuser:taskpassword@database:5432/taskdb
database:
image: postgres:13
environment:
- POSTGRES_DB=taskdb
- POSTGRES_USER=taskuser
- POSTGRES_PASSWORD=taskpassword
networks:
- task-network
volumes:
- db-data:/var/lib/postgresql/data
networks:
task-network:
driver: bridge
volumes:
db-data:
Checking network and volume configurations
After configuring networks and volumes, make sure everything is set up correctly and works as expected:
- 1. Start the containers:
Terminal
docker compose up - 2. Check the network:
- Use the
docker network lscommand to ensure thetask-networknetwork has been created. - Use the
docker network inspect task-networkcommand to check that all containers are connected to this network.
- Use the
- 3. Check the volumes:
- Use the
docker volume lscommand to ensure thedb-datavolume has been created. - Use the
docker volume inspect db-datacommand to verify the volume details.
- Use the
GO TO FULL VERSION