4.1 Initialization
In this step, we'll set up the compose.yaml
file to start all the services of our multi-container application. The compose.yaml
file will include configurations for the frontend, backend, and database, as well as the definition of networks and volumes.
Defining the Docker Compose version
Let's begin by specifying the Docker Compose version. In this example, version 3 is used:
version: '3'
4.2 Defining Services
Service 1: Frontend (ReactJS)
Let’s define a service for the frontend, which will be built from the frontend directory and will be available on port 3000:
services:
frontend:
build: ./frontend
ports:
- "3000:3000"
networks:
- task-network
Service 2: Backend (Flask)
Let’s define a service for the backend, which will be built from the backend directory. It depends on the database service and will be available on port 5000:
backend:
build: ./backend
ports:
- "5000:5000"
depends_on:
- database
networks:
- task-network
environment:
- DATABASE_URL=postgresql://taskuser:taskpassword@database:5432/taskdb
Where:
- depends_on: points out that the backend service depends on the database service. This means the database will start up before the backend.
- environment: configures environment variables to connect to the database.
Service 3: Database (PostgreSQL)
Let’s define a service for the database by using the official postgres:13 image. We’ll set up environment variables to create a database and user credentials:
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:
-
environment
: configures environment variables to create the database and user when the container starts. -
volumes
: attaches the db-data volume for persistent data storage.
4.3 Setting up networks and volumes
Let's define the networks and volumes that will be used by our services:
networks:
task-network:
driver: bridge
volumes:
db-data:
Where:
- networks: defines the network
task-network
with a driver typebridge
, which will be used for communication between containers. - volumes: defines the volume
db-data
for storing the database data.
4.4 Complete docker-compose.yml
File
Now let's combine all the parts into one docker-compose.yml
file:
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:
Explanation of the Settings
-
build
: specifies the path to the directory with theDockerfile
to build the image. -
ports
: opens ports on the host for accessing services. -
depends_on
: indicates that a service depends on another to ensure startup order. -
environment
: configures environment variables for services. -
networks
: defines the network for communication between containers. -
volumes
: defines volumes for persistent data storage.
4.5 Running the Application
Now that the compose.yaml
file is set up, we can start all the services with one command:
docker compose up
This command will create and launch containers for the frontend, backend, and database, using the settings defined in compose.yaml
.
Checking the launch
After starting the containers, make sure all the services are working correctly:
- Frontend: open a browser and go to http://localhost:3000 to check the frontend.
- Backend: open a browser and go to http://localhost:5000 to check the backend.
- Database: make sure the database container is running and accessible through network interaction with the backend.
GO TO FULL VERSION