Bird
Raised Fist0
Spring Bootframework~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using docker-compose with Spring Boot services?
easy
A. To run multiple related services together with one configuration file
B. To compile Java code faster
C. To replace the Spring Boot framework
D. To write unit tests automatically

Solution

  1. Step 1: Understand what docker-compose does

    Docker Compose is a tool to run multiple containers (services) together using one YAML file.
  2. Step 2: Relate to Spring Boot services

    It helps run your Spring Boot app alongside databases or other services easily.
  3. Final Answer:

    To run multiple related services together with one configuration file -> Option A
  4. Quick Check:

    docker-compose = run multiple services [OK]
Hint: Remember: docker-compose manages multiple containers easily [OK]
Common Mistakes:
  • Thinking docker-compose compiles code
  • Confusing docker-compose with testing tools
  • Believing it replaces Spring Boot
2. Which of the following is the correct way to specify a service's port mapping in a docker-compose.yml file?
easy
A. ports: - "8080:8080"
B. ports: 8080:8080
C. port: 8080->8080
D. expose: 8080:8080

Solution

  1. Step 1: Recall docker-compose port syntax

    Ports are listed as a YAML array with dash and quotes, like ports: - "hostPort:containerPort".
  2. Step 2: Check options

    ports: - "8080:8080" matches the correct YAML syntax for port mapping.
  3. Final Answer:

    ports: - "8080:8080" -> Option A
  4. Quick Check:

    Port mapping uses dash and quotes in YAML [OK]
Hint: Use dash and quotes for ports in docker-compose YAML [OK]
Common Mistakes:
  • Omitting dash for list items
  • Using wrong key 'port' instead of 'ports'
  • Confusing 'expose' with 'ports'
3. Given this docker-compose.yml snippet:
services:
  app:
    image: springboot-app
    ports:
      - "8080:8080"
    depends_on:
      - db
  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: example

What happens when you run docker-compose up?
medium
A. docker-compose throws a syntax error
B. Only app service starts, db is ignored
C. db starts but app fails due to missing environment
D. Both app and db services start, app waits for db to start

Solution

  1. Step 1: Understand depends_on behavior

    The app service depends on db, so docker-compose starts db first, then app.
  2. Step 2: Check environment and ports

    db has required environment variable, app exposes port 8080 correctly.
  3. Final Answer:

    Both app and db services start, app waits for db to start -> Option D
  4. Quick Check:

    depends_on starts dependencies first [OK]
Hint: depends_on starts services in order, no syntax errors here [OK]
Common Mistakes:
  • Assuming depends_on waits for full readiness
  • Thinking db is ignored without explicit start
  • Confusing environment variables with ports
4. You wrote this docker-compose.yml but your Spring Boot app fails to connect to the database:
services:
  app:
    image: springboot-app
    ports:
      - "8080:8080"
    environment:
      SPRING_DATASOURCE_URL: jdbc:postgresql://localhost:5432/mydb
  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: example

What is the likely problem?
medium
A. POSTGRES_PASSWORD is incorrect environment variable
B. Missing port mapping for db service
C. The app tries to connect to localhost instead of the db service hostname
D. The app image name is wrong

Solution

  1. Step 1: Check datasource URL in app environment

    The app uses jdbc URL with 'localhost', which inside container means itself, not db service.
  2. Step 2: Understand docker-compose networking

    Services communicate by service name, so URL should use 'db' hostname, not 'localhost'.
  3. Final Answer:

    The app tries to connect to localhost instead of the db service hostname -> Option C
  4. Quick Check:

    Use service name, not localhost, for inter-service DB URL [OK]
Hint: Use service names, not localhost, inside docker-compose networks [OK]
Common Mistakes:
  • Assuming localhost works inside containers
  • Forgetting to map ports for external access
  • Misnaming environment variables
5. You want to run a Spring Boot app with a MySQL database using docker-compose. You want the database to keep data even if the container stops. Which snippet correctly adds persistent storage for the database?
hard
A. volumes: - /var/lib/mysql:/db-data volumes: db-data:
B. volumes: - db-data:/var/lib/mysql volumes: db-data:
C. volumes: - db-data:/mysql/data volumes: mysql-data:
D. volumes: - /mysql/data:/db-data volumes: db-data:

Solution

  1. Step 1: Understand volume syntax for persistence

    Docker volumes map a named volume to the container path where data is stored, e.g., db-data:/var/lib/mysql.
  2. Step 2: Check volume declaration

    The named volume must be declared under top-level volumes: with the same name.
  3. Final Answer:

    volumes: - db-data:/var/lib/mysql volumes: db-data: -> Option B
  4. Quick Check:

    Named volume mapped to container path with declaration [OK]
Hint: Map named volume to container data path and declare it below [OK]
Common Mistakes:
  • Reversing host and container paths
  • Not declaring the named volume
  • Using wrong container data directory
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard