šŸš€ LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
šŸŽ“ COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
HTML MASTER CLASS /// LEARN TAGS /// BUILD STRUCTURE /// SEMANTIC WEB /// HTML MASTER CLASS /// LEARN TAGS ///

Untitled Lesson

⚔ Total XP: 0|šŸ’» backend XP: 0

Skill Matrix

UNLOCK NODES BY LEARNING NEW TAGS.

Select an unlocked node to view details root

Pascual Vila

Pascual Vila

Frontend Instructor // Code Syllabus

Common Pitfalls & Errors

The Error //

Assuming depends_on waits for a service to be ready, not just started

services: db: image: postgres:15 healthcheck: test: ["CMD-SHELL", "pg_isready -U postgres"] interval: 5s timeout: 5s retries: 5 api: build: . depends_on: db: condition: service_healthy

The Solution //

depends_on only guarantees Docker starts the db container before the api container — it does NOT wait for Postgres to finish initializing and accept connections. This causes intermittent ECONNREFUSED errors on 'docker-compose up'. Add a healthcheck to the db service and use the condition: service_healthy form of depends_on.

The Error //

Connecting to 127.0.0.1 or localhost from inside a container

// Wrong: only works if Postgres runs on the same host, outside Docker const pool = new Pool({ host: '127.0.0.1', port: 5432 }); // Correct: resolves via Compose's internal DNS const pool = new Pool({ host: 'db', port: 5432 });

The Solution //

Each container has its own isolated network namespace, so 'localhost' inside the api container refers to the api container itself, not the db container next to it. Use the service name defined in docker-compose.yml as the hostname — Compose's built-in DNS resolves it to the right container automatically.

Continue Learning