基于SQL转储的多PostgreSQL数据库Docker-Compose配置求助
Hey there! Let's break down what might be going wrong with your Docker Compose setup since you mentioned you've tried existing answers without luck. First, let's start by formatting the incomplete config you shared for clarity:
version: '3' services: backend: image: backend:1.0 build: ./backend ports: - "9090:9090" depends_on: - db - ppt environment: - DATABASE_HOST=db db: image: main_db:26.03.18 restart: always build: ./db ports: - "5432:5432" ppt: image: ppt_generator:1.0 build: ./ppt ports: - "6060:6060" login: image: login:1.0 # Your login service config appears to be cut off here!
Now let's walk through the most common pitfalls that might be blocking your setup:
1. Incomplete Login Service Configuration
Your login service is missing key details (like build context, dependencies, or environment variables) in the shared snippet. If this service needs to connect to your db or other services, fill it out properly. For example:
login: image: login:1.0 build: ./login # Add this if you're building from a local Dockerfile depends_on: - db # Include this if login interacts with the database environment: - DATABASE_HOST=db
2. depends_on Doesn't Wait for Database Readiness
This is a super common gotcha! The depends_on flag only waits for the container to start—not for the database inside to finish initializing and accept connections. To fix this, add a healthcheck to your db service and update depends_on to wait for the healthcheck to pass.
Update your db service:
db: image: main_db:26.03.18 restart: always build: ./db ports: - "5432:5432" healthcheck: test: ["CMD-SHELL", "pg_isready -U your_db_user -d your_db_name"] # Replace with your actual DB credentials interval: 5s timeout: 5s retries: 5
Then adjust the depends_on for services that connect to the database:
backend: # ... existing config depends_on: db: condition: service_healthy ppt: condition: service_started # Use service_healthy here too if ppt has its own readiness check login: # ... existing config depends_on: db: condition: service_healthy
3. Missing Database Connection Variables
Your backend only sets DATABASE_HOST=db, but you almost certainly need additional variables like DATABASE_USER, DATABASE_PASSWORD, and DATABASE_NAME to establish a valid connection. Make sure these match the credentials configured in your custom main_db image.
Add these to your backend (and login if needed):
environment: - DATABASE_HOST=db - DATABASE_USER=postgres # Replace with your actual DB user - DATABASE_PASSWORD=your_secure_password - DATABASE_NAME=your_target_db
4. Verify Your Custom Database Image
Since you're using a custom main_db:26.03.18 image, double-check that it's configured correctly:
- Does it initialize the database and user/password properly?
- Is it set to listen on port 5432 for external connections (not just localhost)?
- You can test connectivity directly by running:
docker exec -it <your_db_container_name> psql -U your_user -d your_db
5. Network Isolation (Unlikely but Worth Checking)
By default, Docker Compose creates a shared network where all services can reach each other via their service name (e.g., db resolves to the container's IP). If you've manually added custom networks, ensure all services are attached to the same network. If you haven't touched network config, this shouldn't be an issue, but you can explicitly define one for clarity:
version: '3' services: # ... all your services here networks: default: name: my_app_network
内容的提问来源于stack exchange,提问作者Ludo

