Docker
Mental model Dockerfile = how to build an image. Docker Compose = how to run multiple containers together.
Dockerfile vs Docker Compose
Dockerfile Docker Compose
Builds an image Defines/runs services
Describes application packaging Describes the local environment
Usually one application image Can run many containers
docker build docker compose up
flowchart LR Dockerfile --> Image["Docker Image"] Image --> Container["Application Container"] Compose["docker-compose.yml"] --> App["App"] Compose --> DB["PostgreSQL"] Compose --> Kafka["Kafka"] Compose --> Redis["Redis"]
Small Dockerfile
FROM eclipse-temurin:21-jre
WORKDIR /app
COPY target/order-service.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]The important distinction:
Interview question Does a Dockerfile run PostgreSQL and
Kafka?
No. It describes how to build the application image. Compose can then run that application alongside PostgreSQL, Kafka, Redis, etc.
Compose Example
services:
postgres:
image: postgres:17
environment:
POSTGRES_DB: ecommerce
POSTGRES_USER: ecommerce
POSTGRES_PASSWORD: ecommerce
ports:
- "5432:5432"
redis:
image: redis:8
ports:
- "6379:6379"
kafka:
image: apache/kafka:latest
ports:
- "9092:9092"Remember Dockerfile packages. Compose orchestrates the local stack.
Why We Use Compose
Without Compose:
docker run postgres ...
docker run redis ...
docker run kafka ...
docker run order-service ...With Compose:
docker compose upThis gives every developer a repeatable local environment.
Production Connection
Compose is primarily useful for local development and simple environments.
For production we may use:
Docker image
↓
Kubernetes
↓
EKSInterview Questions
Why containerize the application?
Containers give us a consistent runtime, isolate dependencies, simplify deployment, and make the application easier to move between environments.
Is Docker a VM?
No. Containers share the host kernel, whereas a traditional VM includes a guest operating system.
Why not just install PostgreSQL and Kafka locally?
Containers make the development environment reproducible and avoid polluting the developer’s machine with infrastructure-specific installations.
] → use Docker Compose to run Kafka
locally and connect Java/.NET services to it.