Untitled Lesson
Skill Matrix
UNLOCK NODES BY LEARNING NEW TAGS.
If you define a database service named `postgres-db` in your docker-compose.yml file, how does your Node.js application (running in a separate container) connect to that database over the Compose network?
š» Code Challenge | +75 XP
Write the Node.js/Backend code snippet to implement a Node Docker Compose (multi-container apps) pipeline. Include the setup and basic execution steps.
You are reviewing a Node Docker Compose (multi-container apps) pipeline and the output is incorrect. Reorder the following pipeline stages in the correct logical order to fix the bug: Input Data, Process, Output.
Task: Reorder the blocks in logical sequence to solve the problem.
A.D.A. Interface
Adaptive Didactic Assistant

Pascual Vila
Frontend Instructor // Code Syllabus
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_healthyThe 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.