Docker, start to ship · Chapter 19 of 26

The one-command stack

By the end of this chapter you can write a compose file for a small stack, bring it up and take it down with one command, and run a command inside one of its services. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Compose
  • 4 tasks
  • Docker
  • In the all labs plan

The first 2 chapters of Docker, start to ship are free. This one comes with the all labs plan.

Why you need this

Tapri's menu site needs two containers: nginx serving site/ on port 8019, and a redis cache beside it for today's special. Bunty's setup notes are a page long: run the cache with these flags, the web server with those, in this order, and do not forget the folder. Every new server means typing the page again, and one wrong flag means a site that is different from yesterday's. Bunty has also started his own stack in bunty/, and it holds port 8019.

With what you know, the stack is a row of docker run commands, each with its own name, ports and mounts, and nothing ties them together: removing "the stack" means remembering every name.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Take Bunty's chai-19-bunty stack down with compose

    Bunty's compose project, chai-19-bunty, is using port 8019. Take it down.

    1. Go into Bunty's folder, bunty. His compose file is bunty/compose.yaml.

    2. Run docker compose down there.

    3. Come back to the chapter folder, and list every container to check.

    To pass: no container of chai-19-bunty is left, running or stopped, and you ran docker compose down in the chapter folder or in bunty, and it worked.

  2. Task 2

    Write the chai-19 stack and bring it up

    Write your own compose file for the menu site and a cache, and start it.

    1. Make compose.yaml in the chapter folder, with the project name chai-19.

    2. Add a service web: the nginx image, port 80 published on the lab's port 8019, and the site folder mounted at /usr/share/nginx/html (read-only is best).

    3. Add a service cache: the redis image.

    4. In this lab every service also needs the line network_mode: slirp4netns.

    5. Run docker compose up in the background, in the chapter folder.

    To pass: compose.yaml is in the chapter folder, the services web and cache of chai-19 both run, you started them from the chapter folder, cache runs from redis, and localhost:8019 shows the Tapri menu with ginger chai 25.

  3. Task 3

    Store today's special in the cache with compose exec

    Save today's special in the cache.

    1. Use docker compose exec on the cache service. Use the service's name, cache, not the container's name.

    2. Inside it, run redis-cli to set the key special to ginger chai. The value has a space, so put it in quotes.

    3. Read the key back the same way.

    To pass: the key special in the running cache is ginger chai, and you ran docker compose exec on cache in the chapter folder, and it worked.

  4. Task 4 · interview question

    Interview: what down removes, what it keeps

    Interview question: what does docker compose down remove, and what does it keep?

    1. Think about the project's containers.

    2. Think about a named volume that a service uses.

    3. Write one or two sentences in your own words, and save them in answer.txt, in the chapter folder.

    To pass: answer.txt talks about the containers and about the volume: what goes, and what stays.

What you will learn

  • compose.yaml: the stack written down once, one entry per service, with the same settings as docker run.
  • docker compose up -d and down: the whole stack at once, from the folder with the file.
  • ps, logs and exec: looking at one service by its service name.

More in Compose

Build The one-command stack yourself

Tapri’s menu site works on one laptop and nowhere else. You ship the machine. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Docker, start to ship are free. This one comes with the all labs plan.