0
0
Spring Bootframework~10 mins

docker-compose for services in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - docker-compose for services
Write docker-compose.yml
Define services
Set images, ports, env
Run docker-compose up
Docker creates containers
Services start and communicate
Access services via ports
This flow shows how you write a docker-compose file, define services, run it, and how containers start and communicate.
Execution Sample
Spring Boot
version: '3.8'
services:
  app:
    image: my-springboot-app
    ports:
      - "8080:8080"
  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: example
This docker-compose file defines two services: a Spring Boot app and a Postgres database, exposing app on port 8080.
Execution Table
StepActionDetailsResult
1Read docker-compose.ymlParse version and servicesServices 'app' and 'db' recognized
2Pull imagesPull 'my-springboot-app' and 'postgres' imagesImages downloaded or found locally
3Create containersCreate container for 'app' and 'db'Containers created but not started
4Start containersStart 'db' container firstPostgres database starts running
5Start 'app' containerSpring Boot app container startsApp starts and tries to connect to db
6Map portsHost port 8080 maps to app container port 8080App accessible at localhost:8080
7Containers runningServices communicate internallyApp can use database
8Exitdocker-compose stops or user interruptsContainers stop and exit
💡 Execution stops when containers are stopped or docker-compose is terminated
Variable Tracker
VariableStartAfter Step 3After Step 5Final
app containernonecreatedrunningrunning
db containernonecreatedrunningrunning
app port mappingnonenone8080:80808080:8080
db environmentnoneset POSTGRES_PASSWORD=exampleset POSTGRES_PASSWORD=exampleset POSTGRES_PASSWORD=example
Key Moments - 3 Insights
Why does the database container start before the app container?
In the execution_table at step 4 and 5, the db container starts first so the app can connect to it when it starts.
How does port mapping work in docker-compose?
Step 6 shows host port 8080 maps to container port 8080, making the app accessible on localhost:8080.
What happens if the images are not found locally?
Step 2 shows docker-compose pulls the images from the registry before creating containers.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step are the containers created but not started?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Check the 'Result' column in execution_table row for Step 3
According to variable_tracker, what is the state of the 'app container' after Step 5?
Anone
Brunning
Ccreated
Dstopped
💡 Hint
Look at the 'app container' row under 'After Step 5' column in variable_tracker
If the POSTGRES_PASSWORD environment variable was missing, what step would be affected?
AStep 4 - Start db container
BStep 2 - Pull images
CStep 6 - Map ports
DStep 7 - Services communicate
💡 Hint
Check environment variable setup in variable_tracker and when db container starts in execution_table
Concept Snapshot
docker-compose.yml defines multiple services
Each service has image, ports, environment
Run 'docker-compose up' to start all
Containers start in order, can communicate
Ports map container to host for access
Environment variables configure services
Full Transcript
This visual trace shows how docker-compose manages multiple services for a Spring Boot app and a database. First, the docker-compose.yml file is read and parsed to find service definitions. Then, docker pulls the required images if not present locally. Containers for each service are created but not started immediately. The database container starts first to be ready for connections. Next, the app container starts and connects to the database. Ports are mapped from container to host so users can access the app on localhost. The services run and communicate internally. Finally, when docker-compose is stopped, containers exit. Variables like container states and environment variables change step-by-step as shown. This helps beginners see how docker-compose orchestrates multiple services simply and reliably.