Docker 101 labs

Run the shop for real

Five labs, about 25 minutes. You'll end with the shop's Node.js API and its Postgres database running in containers on your own machine.

1. Get the lab files

The API with its Dockerfile, the Compose file, and the SQL 101 shop data.

Download docker-101-lab.zip

Unzip it anywhere. You'll get a folder called shop.

2. Install Docker

Docker Desktop on Windows or macOS, or Docker Engine on Linux.

Get Docker

On Windows, let it set up WSL 2 when it asks. Docker Desktop is free for personal use, education and small businesses.

No Docker yet? Practice the same commands in a simulated terminal
  1. 1

    Say hello

    Check that Docker works by running its test image.

    docker run hello-world

    You should see, after a short download:

    Hello from Docker!
    This message shows that your installation appears to be working correctly.
    It didn't work
    Cannot connect to the Docker daemon
    Docker isn't running. Start Docker Desktop and wait until it says it's running, then try again.
    permission denied … docker.sock (Linux)
    Run sudo usermod -aG docker $USER, then log out and back in, so you don't need sudo for every command.
    docker: command not found
    Docker isn't installed, or the terminal was open before you installed it. Open a new terminal.
  2. 2

    Run a web server

    Start nginx in the background, publish its port 80 as your port 8080, then look at it and clean up.

    docker run -d -p 8080:80 --name web nginx

    Open http://localhost:8080: you should see "Welcome to nginx!". Then:

    docker ps
    CONTAINER ID   IMAGE   COMMAND                  CREATED         STATUS         PORTS                                     NAMES
    ffe12294fb46   nginx   "/docker-entrypoint.…"   2 seconds ago   Up 2 seconds   0.0.0.0:8080->80/tcp, [::]:8080->80/tcp   web
    docker logs web

    The last line is your browser's visit: "GET / HTTP/1.1" 200.

    docker stop web
    docker rm web
    It didn't work
    port is already allocated, or ports are not available
    Something else uses port 8080. Use another: -p 8081:80, then open localhost:8081.
    The container name "/web" is already in use
    An old one is still there. Remove it with docker rm -f web and run again.
  3. 3

    Build the API's image

    In a terminal, go into the unzipped shop folder. Read api/Dockerfile first, then build it.

    cd shop
    docker build -t shop-api ./api

    It ends with naming to docker.io/library/shop-api:latest. Run the same command again: steps 2 to 4 now say CACHED.

    Now run the API on its own, and ask it for the products:

    docker run -d -p 3000:3000 --name api shop-api

    Open http://localhost:3000/api/products: you get Internal Server Error. That's expected. Find out why:

    docker logs api
    API on http://localhost:3000
    Error: connect ECONNREFUSED 127.0.0.1:5432

    Inside the container, 127.0.0.1 is the container itself, and there's no database in it. Remove it so port 3000 is free:

    docker rm -f api
  4. 4

    Start the whole shop

    Make your own .env from the example, and pick a password in it.

    cp .env.example .env
    Copy-Item .env.example .env
    docker compose up -d --build
    docker compose ps

    Some columns are left out here:

    NAME         IMAGE         SERVICE   STATUS                   PORTS
    shop-api-1   shop-api      api       Up 2 seconds             0.0.0.0:3000->3000/tcp
    shop-db-1    postgres:16   db        Up 8 seconds (healthy)   5432/tcp

    Open http://localhost:3000/api/products: now you get the 20 products. Then place an order:

    curl -X POST localhost:3000/api/orders -H "Content-Type: application/json" -d '{"productId":3,"quantity":1}'
    Invoke-RestMethod -Method Post http://localhost:3000/api/orders -ContentType application/json -Body '{"productId":3,"quantity":1}'
    {"orderId":194}
    orderId
    -------
        194
    docker compose logs api
    api-1  | API on http://localhost:3000
    api-1  | order 194 saved
    It didn't work
    port is already allocated (3000)
    The container from lab 3 is still running: docker rm -f api, then docker compose up -d again.
    The order number isn't 194
    That's fine if you've placed orders before: the data is kept between runs. Lab 5 shows how to start fresh.
    password authentication failed for user "shop"
    You changed the password after the database was created; Postgres keeps the first one. Start fresh with docker compose down -v, then up again.
    dependency failed to start: container shop-db-1 exited (1)
    Almost always a missing .env, so the database has no password; Compose also warns that DB_PASSWORD isn't set. Create .env and run up again. docker compose logs db shows the details.
  5. 5

    Keep the data, then throw it away

    Remove the containers and start new ones. The order is still there, because it lives in the pgdata volume.

    docker compose down
    docker compose up -d
    docker compose exec db psql -U shop -c "SELECT COUNT(*) FROM orders;"
     count
    -------
       194
    (1 row)

    Now delete the volume too, and start again: the count goes back to 193, because the SQL 101 data loads into the empty database.

    docker compose down -v
    docker compose up -d

    When you're finished, docker compose down -v cleans everything up.

  6. Bonus: change the code

    In api/server.js, change the order … saved message, then rebuild and restart just the API:

    docker compose up -d --build api

    Only the last step, COPY . ., runs again; npm's install comes from the cache. Place an order and check the logs for your new message.

The lab files