CampusFlow
DevOpsDocker

Docker

Containerization platform for building, shipping, and running applications. Explore images, containers, Dockerfiles, Compose, and networking.

Container Lifecycle Simulator

$ docker lifecycle demo

Step 1: Dockerfile

Define the image layers

Dockerfile

FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npm run build

FROM node:18-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
EXPOSE 3000
CMD ["node", "dist/index.js"]

Docker Compose

version: "3.8"
services:
  web:
    build: .
    ports:
      - "3000:3000"
    depends_on:
      - db
    environment:
      - DATABASE_URL=postgres://user:pass@db:5432/app

  db:
    image: postgres:15
    volumes:
      - pgdata:/var/lib/postgresql/data
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: pass

volumes:
  pgdata:

Essential Docker Commands

docker psList running containers
docker ps -aList all containers
docker imagesList images
docker build -t app .Build image
docker run -d -p 8080:80 nginxRun container detached
docker exec -it cont bashShell into container
docker logs -f contFollow container logs
docker stop contStop container
docker rm contRemove container
docker rmi imgRemove image
docker compose up -dStart Compose services
docker compose downStop Compose services

Container Networking

bridge

Default network. Containers communicate via IP on a private subnet.

host

Container uses host's network stack directly. No isolation.

none

No network interfaces except loopback.

overlay

Multi-host networking for Swarm/Kubernetes.

Pro tip: Use docker network create mynet to create a user-defined bridge network. Containers on the same network can resolve each other by container name.

Interview Questions

Q1: What is the difference between an image and a container?
An image is an immutable, read-only template with instructions for creating a container. A container is a runnable instance of an image — it has a writable layer on top of the image layers and runs as an isolated process on the host.
Q2: How does Docker use layers and copy-on-write?
Each Dockerfile instruction creates a read-only layer. When a container runs, Docker adds a thin writable layer on top. On write, the file is copied up from the underlying layer (copy-on-write). This makes containers lightweight — they share base layers and only store differences.
Q3: Explain Docker multi-stage builds and their benefits.
Multi-stage builds use multiple FROM statements in one Dockerfile. Build artifacts are copied from intermediate stages to the final stage. Benefits: smaller final images (no build tools), cleaner Dockerfiles, and no need for separate build scripts. The builder pattern reduces image size by 10-100x.
Q4: What is the difference between docker-compose and docker stack?
Docker Compose is a CLI tool for defining and running multi-container applications on a single host using a compose.yml file. Docker Stack is part of Docker Swarm — it deploys the same compose file across a Swarm cluster with built-in load balancing, rolling updates, and replication.