Ravindra BagaleCourses & study guides Track your progress

Guides

Docker Compose for Beginners

Read this guide in: English · मराठी · हिंदी

Why do we need Docker Compose? Because there is a real problem between developers and operations when the app is more than one process. Developers need a web front and a Redis cache. Operations — the employee on the server side — gets two long docker run lines, forgotten networks, and an app that cannot find the cache hostname. Compose is one docker-compose.yml both sides share: docker compose up lights the stack; docker compose down tears it down.

Friends, when one container is not enough, first see the two-service fight. Then the YAML sticks.

Quick answer

Hold this vertical picture:

  1. Developer — needs app + cache together on the laptop.
  2. Operations — server-side person who must start the same pair without folklore.
  3. The problem — two docker run commands; drift; wrong hostname between containers.
  4. Compose — one YAML, up, ps, logs, down.
  5. Pocket line: one file, one up, one down.

Language: English · मराठी · हिंदी

Why do we need Compose?

  1. Real apps often need more than one container.
  2. Operations on the server side should not guess ports from chat history.
  3. One file is the shared truth for laptop and server.

Need Docker Engine (EC2 install), Compose plugin, optional first Dockerfile.

What does operations have to do with Compose?

  1. Operations still watches disk, ports, and health.
  2. They start the stack the same way: docker compose up -d.
  3. Cache/db often need no public ports — only the app talks by service name.

What is the problem? Suppose we are…

Suppose we are building Instagram (like updates)

Suppose the feed API needs Redis for caching. Developer runs nginx and redis with two commands. Inside a container, localhost is wrong. Operations on EC2 cannot reproduce the laptop network.

Suppose we are working at Netflix and we are giving updates

Suppose a small local stack (app + cache) must match how services talk by name. Compose documents that network so “works on my laptop” is not a private trick.

Suppose you are working in Infosys on a client project

Suppose client operations asks for one file that starts the lab stack. Two mystery docker run lines in a Word doc will fail on release day.

Docker Compose multi-service stack One docker-compose.yml defines web and cache services on a shared network. docker compose up starts the stack so services talk by name. docker-compose.yml services · ports · network web nginx:alpine ports 8080:80 cache redis:alpine internal only network web → cache by service name docker compose up -d One file for the lab stack — not two mystery docker run lines

One docker-compose.yml starts web and cache on a shared network; services talk by name with docker compose up.

Docker Compose services Terminal-style teaching diagram of docker compose up -d and docker compose ps for web and cache.

Terminal-style teaching diagram: docker compose up -d and docker compose ps for web (published port) and cache (internal).

How Compose answers that same fight

docker compose version
mkdir composelab && cd composelab
cat > docker-compose.yml <<'EOF'
services:
  web:
    image: nginx:alpine
    ports:
      - "8080:80"
  cache:
    image: redis:alpine
EOF
docker compose up -d
docker compose ps
curl -I http://localhost:8080
docker compose logs -f web
docker compose exec web sh
docker compose down

Between containers use the service name cache — not localhost. Prefer docker compose (space). On Amazon Linux see the AWS Docker guide’s Compose plugin steps if needed.

Ravindra Bagale's Tip

Beginners publish every port to the internet. For cache/db, often no public ports: — only the app reaches cache by service name. Lakshat theva.

Common errors — story pain then fix

Pain from the stories Likely cause Fix
docker compose not a command Plugin missing Install Compose plugin
Port already allocated Host port busy Change host side of "8080:80"
Services cannot talk Used localhost between containers Use service name
Changes ignored Stack not recreated docker compose up -​d --​build
YAML indent error Spaces/tabs mix Consistent spaces

Samajla ka? Why Compose = multi-container fight between developer and server-side operations; Instagram / Netflix / Infosys “suppose”; one YAML, up, down. Practise tonight — before Kubernetes.

Frequently asked questions

What is Docker Compose?

A tool that starts multi-container apps from one YAML file.

docker compose vs docker-compose?

Modern Docker uses the plugin form docker compose (space). The old hyphen binary may still exist.

Do I need Kubernetes for two containers?

No. Compose is enough for local and small server stacks.

Where do ports go?

ports: maps host:container. Internal service-to-service traffic often needs no public publish.

What is a service?

A named container definition in the Compose file (for example web or db).

How do I rebuild after code changes?

docker compose up -d --build to rebuild images then restart.