Docker 101

    Put the shop in containers

    You've met the shop's API in Software Development 101 and its database in SQL 101. Today you'll package both, so they run the same on any machine with one command.

    "It works on my machine"

    Your laptopThe server
    OSWindowsLinux
    Node.js2218
    Postgres1614
    Setupyou did it once, by handsomeone else did it differently
    • Same code, different result, and hours lost finding out why.
    • A container packs the app together with the exact runtime and libraries it needs.
    • Your laptop, a teammate's laptop and the server all run the same thing.

    Image, container, registry

    You know this
    shop.c
    shop.exe
    A running process
    Docker
    Dockerfile
    Image
    Container
    • An image is a read-only package: the app, its runtime and its files. You build it once.
    • A container is an image running. One image can run as many containers as you like.
    • A registry stores images. Docker Hub has ready-made ones: nginx, postgres, node.

    Containers aren't virtual machines

    Virtual machines

    App AApp B
    A whole OSA whole OS
    Hypervisor
    Hardware

    Minutes to boot. Gigabytes each.

    Containers

    APIPostgresnginx
    One Linux kernel, shared
    Hardware

    Seconds to start. Megabytes each.

    On Windows and macOS, Docker Desktop runs one small Linux VM, and your containers live inside it.

    Your first container

    • No hello-world image yet, so Docker downloads it from Docker Hub.
    • It starts a container, which prints its message and exits.
    • docker ps -a still lists it, stopped. -a means all, not just running.

    Run a web server in one line

    docker run -d -p 8080:80 --name web nginx
    -d
    Run in the background, and give the terminal back.
    -p 8080:80
    Your machine's port 8080 leads to port 80 inside the container.
    --name web
    A name to use instead of the long ID.
    nginx
    The image to start.

    Stop, start, remove

    docker run
    running
    stopped
    removed
    • stop and start keep the container and its files.
    • rm deletes it, and refuses while it's running. -f forces it.
    • A "port is already allocated" or "ports are not available" error means something else holds that port. Pick another, like -p 8081:80.

    A Dockerfile for the shop's API

    api/Dockerfile VS Code
    
    # The recipe for the shop API's image.
    FROM node:22-slim
    WORKDIR /app
    COPY package*.json ./
    RUN npm ci --omit=dev
    COPY . .
    EXPOSE 3000
    USER node
    CMD ["node", "server.js"]
    
    FROM
    Start from an official image that already has Node.js 22.
    WORKDIR
    Work in /app inside the image.
    COPY, RUN
    Copy the package list, then install exactly what package-lock.json says.
    COPY . .
    Then copy the code. Why in this order? Next slide.
    EXPOSE, USER
    Note the port, and don't run as root.
    CMD
    What runs when a container starts.

    Build it, layer by layer

    • Each line becomes a layer. Docker caches layers and reuses them.
    • Build twice: steps 2 to 4 say CACHED. Edit server.js and only COPY . . runs again.
    • .dockerignore keeps node_modules and .env out of the image.

    Inside a container, localhost is the container

    • The API starts, but every request fails with a 500.
    • The logs say why: ECONNREFUSED 127.0.0.1:5432.
    • 127.0.0.1 inside the container is the container itself, and there's no Postgres in it.

    We need a second container, and a way for the two to find each other. That's Compose.

    docker-compose.yml: the whole shop in one file

    docker-compose.yml VS Code
    
    services:
      api:
        build: ./api
        ports:
          - "3000:3000"
        environment:
          DATABASE_URL: postgresql://shop:${DB_PASSWORD}@db:5432/shop
        depends_on:
          db:
            condition: service_healthy
      db:
        image: postgres:16
        environment:
          POSTGRES_USER: shop
          POSTGRES_PASSWORD: ${DB_PASSWORD}
        volumes:
          - pgdata:/var/lib/postgresql/data
          - ./db/init:/docker-entrypoint-initdb.d
        healthcheck:
          test: ["CMD-SHELL", "pg_isready -U shop"]
          interval: 2s
          retries: 15
    volumes:
      pgdata:
    
    services
    One entry per container: api and db.
    build, ports
    The same as docker build and -p.
    @db:5432
    Containers find each other by service name. No localhost.
    ${DB_PASSWORD}
    Read from a .env file that stays out of Git.
    volumes
    Keep the data, and load the SQL 101 shop on first start.
    healthcheck
    Start the API only once Postgres answers.

    One command, the whole shop

    • up -d --build builds the API, creates the network and volume, and starts both.
    • The order comes back as 194: the same Buy as Software Development 101, now in containers.
    • logs shows every service's output in one place.

    Where the data lives: volumes

    • Containers are disposable. down deletes them, and that's fine.
    • The volume lives outside them, so order 194 is still there after up.
    • down -v deletes volumes too: the data is gone. Great for a fresh start, terrible in production.

    Secrets stay out of the image and out of Git

    
    # .env.example: committed, no real values
    DB_PASSWORD=change-me
    
    # .env: your copy, never committed
    DB_PASSWORD=a-long-random-password
    
    • Compose reads .env next to docker-compose.yml.
    • Put .env in .gitignore, and commit .env.example so others know what to set.
    • Never write a password into a Dockerfile: anyone with the image can read every layer.

    Your one-slide cheat sheet

    docker run -d -p 8080:80 --name web nginxstart a container in the background
    docker ps docker ps -arunning containers, or all of them
    docker logs webwhat it printed
    docker stop web docker rm webstop it, then delete it
    docker build -t shop-api ./apibuild an image from a Dockerfile
    docker imagesimages on this machine
    docker compose up -d --buildbuild and start everything
    docker compose ps logs execcheck on it, read logs, run a command inside
    docker compose down down -vremove containers, or containers and data

    Your turn: run the shop for real

    1. 1Install DockerDocker Desktop, then docker run hello-world
    2. 2A web serverrun, ps, logs, stop, rm with nginx
    3. 3Build the API imagedocker build, then see the localhost error
    4. 4The whole shopdocker compose up, then place order 194
    5. 5Keep the datadown, up, then down -v
    Open the labs
    • Download the lab folder: the API, its Dockerfile, the Compose file and the SQL 101 data.
    • Each step shows what you should see, and what to do when you don't.
    • No Docker yet? The practice terminal on the labs page works anywhere.

    Keep going

    • Share an imagedocker push sends it to a registry such as Docker Hub or GitHub's, so a server can pull it.
    • Run it on a serverSSH in, docker compose up -d, nginx in front: covered in Software Development 101.
    • Read the docsDocker's own Get started guide is short and good.
    • Next ideasMulti-stage builds, smaller images, bind mounts for live reload, and later Kubernetes.

    Containerize something of your own this week: one Dockerfile, one compose file.

    
    docker compose up -d questions
    

    Thank you