Bird
0
0

You wrote this docker-compose.yml but your Spring Boot app fails to connect to the database:

medium📝 Debug Q14 of 15
Spring Boot - Docker and Deployment
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?
APOSTGRES_PASSWORD is incorrect environment variable
BMissing port mapping for db service
CThe app tries to connect to localhost instead of the db service hostname
DThe app image name is wrong
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes