Lesson 19: Building and Running a Multi-Service Application
Using the docker-compose.yml file from the previous lesson, we can now manage our entire stack with simple commands.
1. Bringing the Stack Up
In the directory containing docker-compose.yml, run the primary command:
bash docker compose up
What happens?
- Network Creation: Docker Compose creates a default network (e.g.,
my-app-stack_default). - Image Build: It builds the
webservice image using the local Dockerfile. - Image Pull: It pulls the
postgresimage. - Container Creation: It creates and starts both the
dbandwebcontainers. - Logging: It streams the logs from all services directly to your terminal.
Running in Detached Mode
Just like docker run, we use the -d flag to run the services in the background, freeing up your terminal.
bash docker compose up -d
2. Checking the Status
Verify that all services are running and healthy:
bash docker compose ps
This command lists all services defined in your stack, their current status, and port bindings.
3. Viewing Logs
To see real-time log output from all services combined:
bash docker compose logs -f
-fstands for 'follow'.
You can also view logs for a specific service (e.g., the database):
bash docker compose logs db