Docker启动MongoDB遇ECONNREFUSED错误求助
Hey there, let's break down what's going on here and get your MongoDB setup back on track. It's super common to hit snags like this when working with Docker, especially when renaming services or rolling back changes—let's go through the most likely fixes step by step.
1. Fix the MongoDB Host Alias (Biggest Culprit!)
Looking at your .env and docker-compose.dev.yml files, I spot a critical mismatch:
- Your Docker Compose MongoDB service is named
mongo-db - But your
.envsetsMONGO_HOST=mongodb://db:27017
In Docker Compose, service names act as DNS aliases for container-to-container communication. So your app container is trying to reach a service named db, which doesn't exist anymore (you renamed it to mongo-db). Even if you rolled back changes, double-check that your .env matches the service name in your compose file.
Update your .env file:
MONGO_HOST=mongodb://mongo-db:27017
2. Fully Clean Up Docker Resources (Including Volumes!)
When you ran docker-compose rm and docker rm $(docker ps -a -q), you deleted containers—but Docker volumes (which store MongoDB's persistent data) stick around by default. If the volume has corrupted data or leftover state from your renamed service, it could prevent MongoDB from starting properly.
Run this command to fully tear down containers, networks, and volumes for your dev environment:
docker-compose -f docker-compose.dev.yml down -v
The -v flag is key here—it removes the named volume myresume-service-mongodata, wiping any problematic persistent data.
3. Wait for MongoDB to Be Fully Ready Before Starting Your App
The depends_on option in Docker Compose only waits for the MongoDB container to start, not for the MongoDB service inside the container to be fully ready to accept connections. Your app might be trying to connect before MongoDB finishes initializing, leading to ECONNREFUSED.
Fix this by adding a health check to your MongoDB service and updating your app's depends_on to wait for MongoDB to be healthy:
Update your docker-compose.dev.yml:
version: "3" services: myresume-service: build: . container_name: myresume-service command: npm run dev ports: - 9001:9001 depends_on: mongo-db: condition: service_healthy # Wait for MongoDB to pass health check env_file: - .env volumes: - ./src:/usr/myresume-service/src mongo-db: container_name: mongo-db image: mongo ports: - 27017:27017 volumes: - myresume-service-mongodata:/data/db environment: MONGO_INITDB_DATABASE: "myresume" healthcheck: test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"] interval: 5s timeout: 5s retries: 5 volumes: myresume-service-mongodata:
4. Check MongoDB Logs for Hidden Errors
If you still run into issues, check the MongoDB container logs to see why it's failing to start. Run this command:
docker logs mongo-db
Look for errors like permission issues on the data volume, corrupted data files, or initialization failures. This will give you a clear picture of what's going wrong under the hood.
Final Steps to Test
After making these changes:
- Rebuild your app service:
docker-compose -f docker-compose.dev.yml build - Start everything up:
docker-compose -f docker-compose.dev.yml up
You should see MongoDB initialize properly, and your app will wait until it's ready before trying to connect.
内容的提问来源于stack exchange,提问作者Nanobrain91

