Hello and welcome to the next entry in this series. Today I will cover creating development environment on local machine using Docker Compose. This way we can setup both API application and database server quickly and effortlessly.
For purpose of demonstration we can reuse the dockerfiles created in previous entries in the series (for postgres server and API app). We were already able to setup and run these containers separately, but using Docker Compose we can create both of them at the same time with just one command.
The important part is networking: we need to make sure that containers can „talk” to each other. For that let’s create separate network called „docker_network” utilising bridge type driver. There is also need to expose the ports; one thing changes compared to the previous setup. We don’t need to specify port forwarding for database server and API container can talk to database directly on port 5432 (default for Postgres). We still need to forward the requests to API app on different port than the one exposed by Flask container (5000).
I’ve also added some safety mechanism in form of healthcheck for database and „depends_on” clause for api which will cause the API to start only when database container is up and ready.
Putting this all together compose file looks the following:
services:
db:
container_name: postgres
build: ./db/
ports:
- "5432"
expose:
- "5432"
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgrespassword
- POSTGRES_DB=postgres
networks:
- docker_network
healthcheck:
test: ["CMD", "pg_isready", "-U", $POSTGRES_USER, "-d", $POSTGRES_DB]
interval: 1s
timeout: 5s
retries: 10
api:
container_name: rpgapp
build: .
ports:
- "5005:5000"
networks:
- docker_network
depends_on:
db:
condition: service_healthy
networks:
docker_network:
driver: bridge
Let’s give it a spin and start the environment using command:
docker compose up
Afterwards we can see in Docker desktop app that both containers are running as part of single compose stack:

To confirm that our app works let’s try to create a new character via API call:

We have successfully created environment consisting of database and application server with just one command!
That’s it for today, in my next entry I will cover creating Prometheus container for monitoring our application.