Quick answer: Docker packages your application and everything it needs (runtime, libraries, config) into an image. A running image is a container. You describe the image in a Dockerfile, build it with docker build, run it with docker run, and use Docker Compose to run multiple services (app + database + cache) together. The result: "it works on my machine" becomes "it works everywhere".
Docker is now a basic skill for backend developers, DevOps engineers and even data scientists. Almost every job description for cloud and backend roles mentions containers. This beginner guide explains the core ideas and gives you working examples.
Key concepts
Term | Simple meaning |
|---|---|
Image | A read-only template of your app and its environment (like a class) |
Container | A running instance of an image (like an object) |
Dockerfile | The recipe to build an image |
Registry | Storage for images: Docker Hub, GitHub Container Registry, AWS ECR, Azure ACR |
Volume | Persistent storage that survives container restarts |
Network | Lets containers talk to each other by name |
Containers vs virtual machines
- A VM includes a full operating system β heavy, slow to boot.
- A container shares the host's kernel and isolates only the process β light, starts in seconds.
- You can run many more containers than VMs on the same server.
Essential commands
docker --version
docker pull nginx:alpine # download an image
docker run -d -p 8080:80 --name web nginx:alpine
docker ps # running containers
docker logs -f web # follow logs
docker exec -it web sh # open a shell inside
docker stop web && docker rm web
docker images # list images
docker system prune # clean unused data (careful)Your first Dockerfile (Node.js example)
# 1. Base image
FROM node:20-alpine
# 2. Working directory
WORKDIR /app
# 3. Install dependencies first (better caching)
COPY package*.json ./
RUN npm ci --omit=dev
# 4. Copy source code
COPY . .
# 5. Run as non-root user
USER node
EXPOSE 3000
CMD ["node", "server.js"]docker build -t my-api:1.0 .
docker run -d -p 3000:3000 --env-file .env my-api:1.0Multi-stage builds (smaller, safer images)
# Build stage
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY . .
RUN dotnet publish -c Release -o /out
# Runtime stage
FROM mcr.microsoft.com/dotnet/aspnet:8.0
WORKDIR /app
COPY --from=build /out .
ENTRYPOINT ["dotnet", "MyApi.dll"]The final image contains only the runtime and compiled app β not the SDK, source code or build tools. Use the .NET version your project targets.
Docker Compose: app + database + cache
# compose.yaml
services:
app:
build: .
ports: ["8000:8000"]
env_file: .env
depends_on: [db, redis]
db:
image: postgres:16
environment:
POSTGRES_DB: app
POSTGRES_USER: app
POSTGRES_PASSWORD: secret
volumes: ["pgdata:/var/lib/postgresql/data"]
redis:
image: redis:7-alpine
volumes:
pgdata:docker compose up -d
docker compose logs -f app
docker compose downInside the network, the app connects to the database using the host name db, not localhost.
Best practices
- Use small official base images (alpine, slim, distroless) and pin versions β avoid
latestin production. - Order layers well: copy dependency files first, then source code.
- Add a .dockerignore (node_modules, .git, .env, build output).
- Never bake secrets into images; pass them at runtime or use a secrets manager.
- Run as non-root.
- One main process per container.
- Add a HEALTHCHECK so orchestrators know when your app is ready.
- Scan images for vulnerabilities (Docker Scout, Trivy) in CI.
- Log to stdout/stderr, not files inside the container.
Common beginner errors
- "Port already in use" β change the host port:
-p 8081:80. - Data disappears after restart β you forgot a volume.
- App cannot reach the database β use the service name, and wait until the DB is ready.
- Huge images β use multi-stage builds and .dockerignore.
What to learn next
CI/CD pipelines that build and push images, then Kubernetes or managed container services (AWS ECS, Azure Container Apps, Google Cloud Run) for running containers in production.
Summary
- Dockerfile β image β container.
- Use Compose for local multi-service setups.
- Small pinned images, multi-stage builds, non-root, no secrets.
- Volumes for data, service names for networking.