🧩 Scenario: Two containers need to talk

You have:

  1. A PostgreSQL container (your database)
  2. A Node.js backend container (your app)

Both need to communicate over Docker’s virtual network — not your machine’s localhost.


🐘 Step 1: Run Postgres in Docker

docker run --name postgresDb \\
  -e POSTGRES_PASSWORD=sweabhishek \\
  -d -p 5432:5432 \\
  postgres

✅ This runs Postgres, exposes it on your local port 5432, and names the container postgresDb.

However — your Node.js app container cannot access it via localhost, because each container has its own isolated network namespace.


🚫 Why localhost doesn’t work inside containers

Inside the Node.js container:

You need both containers on the same Docker network.


🌐 Step 2: Create a Docker network

docker network create mynetwork

This creates a bridge network (like a virtual LAN) inside Docker.