Skip to main content

Docker

"Containers = run app in a consistent, isolated environment"


What is Docker?

Docker runs applications in containers: lightweight, isolated environments built from an image. Same image runs the same way on dev, CI, and production.


Memory hook

"Image = blueprint; container = running instance; Dockerfile = recipe for image"


Core concepts

  • Image — read-only template (OS, app, dependencies)
  • Container — running instance of an image
  • Dockerfile — instructions to build an image
  • Docker Compose — define and run multi-container apps (YAML)

Essential commands

Images

docker build -t myapp:latest .   # build image from Dockerfile
docker images # list images
docker pull nginx:alpine # pull image from registry
docker rmi <image> # remove image

Containers

docker run -d -p 8080:80 nginx   # run container (detached, port map)
docker ps # list running containers
docker ps -a # list all (including stopped)
docker stop <container> # stop container
docker rm <container> # remove container
docker logs <container> # view logs
docker exec -it <container> sh # shell into running container

Cleanup

docker system prune              # remove unused data
docker container prune # remove stopped containers

Dockerfile basics

FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["node", "index.js"]
  • FROM — base image
  • WORKDIR — working directory inside container
  • COPY — copy files from host
  • RUN — run command during build
  • EXPOSE — document port (does not publish)
  • CMD — default command when container starts

Docker Compose (multi-container)

# docker-compose.yml
version: "3.8"
services:
app:
build: .
ports:
- "3000:3000"
environment:
- NODE_ENV=production
db:
image: postgres:15-alpine
environment:
POSTGRES_PASSWORD: secret
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
docker compose up -d      # start in background
docker compose down # stop and remove
docker compose logs -f # follow logs

Volumes and networks

  • Volume — persistent storage; survives container removal
  • Bind mount — map host path into container (e.g. source code)
  • Network — containers on same network can talk by service name (Compose)

Interview one-liner

"Docker runs apps in containers from images. Dockerfile defines the image; docker run starts a container. Use Docker Compose for multi-container apps; volumes for persistent data."


Cheat sheet

Image = template; container = running instance
docker build / run / ps / logs / exec
Dockerfile: FROM, COPY, RUN, CMD, EXPOSE
Compose: services, volumes, networks
Volume = persistent data; bind mount = host path