Docker, start to ship · Chapter 20 of 26

Compose builds and config

By the end of this chapter you can have Compose build your own image, pass settings to a service, keep its data in a named volume, and start services in order. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Compose
  • 5 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 orders API (api/server.js and its api/Dockerfile) keeps its order count in redis. Bunty builds the image on his laptop and tags it by hand, so nobody knows which code a server runs. Ravi keeps the API's settings in api.env, and the Indiranagar branch needs its own name on top. Dev wants the order count to survive a redeploy.

Chapter 19's compose file only ran images somebody else built, with no settings and no data of its own. Doing the rest by hand means a docker build, a -e per setting typed again on every server, and a cache whose data goes with its container.

What you build: 5 tasks

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

  1. Task 1

    Build the api as chai-20-api, its tag from .env

    Let Compose build the orders API, with the image tag kept in a .env file.

    1. Make compose.yaml in the chapter folder, with the project name chai-20 and a service called api.

    2. Make Compose build api from the api folder. Name its image chai-20-api, with the tag as a variable, like chai-20-api:${TAG}.

    3. Make a file .env next to compose.yaml. In it, set that variable to 1.0.

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

    5. Run docker compose up in the background. The API answers only when it reaches redis, so add the cache from task 3 too.

    To pass: the image line in compose.yaml uses a variable, .env sets it to 1.0, the image chai-20-api:1.0 exists, the api runs from it, and localhost:8020 answers JSON with "shop":"Tapri".

  2. Task 2

    Give the api its branch and Ravi's settings

    Give the api its settings: one in compose.yaml, the rest from Ravi's file.

    1. In compose.yaml, give the api service TAPRI_BRANCH set to Indiranagar, in its environment section.

    2. Read the rest of its settings from api.env, with env_file. Do not change api.env.

    3. Run docker compose up -d again, so the api gets the new settings.

    To pass: compose.yaml sets TAPRI_BRANCH in environment and names api.env in env_file, and localhost:8020 shows "branch":"Indiranagar" and "orderLimit":50.

  3. Task 3

    Keep the orders in a named volume through a down

    Keep the order count in a named volume, so it stays after docker compose down.

    1. Add a service called cache: the image redis:7-alpine, network_mode: host, and a command that makes redis listen on port 8070 (the port api.env gives the API).

    2. Mount a named volume at /data in the cache. List the volume in a volumes section at the top level of the file.

    3. Run docker compose up -d. Then place an order with curl: a POST to localhost:8020/orders.

    4. Run docker compose down, without the option that deletes volumes. Then run docker compose up -d again.

    To pass: the cache runs with /data on a named volume of chai-20. You placed the order before your last docker compose down. Both ran in the chapter folder, and the order count on localhost:8020 is still above 0.

  4. Task 4

    Make the api depend on the cache

    Make Compose start the cache before the api.

    1. Under the api service in compose.yaml, add the key that lists the services it needs.

    2. Name the cache service in it.

    3. Run docker compose config to see the result.

    To pass: in compose.yaml, the api service depends on cache.

  5. Task 5 · interview question

    Interview: what .env and env_file are for

    Interview question: what is the .env file for, and what is env_file for?

    1. Think about who reads each one: Compose itself, or the app in the container.

    2. Write one or two sentences in your own words, one about each.

    3. Save them in answer.txt, in the chapter folder.

    To pass: answer.txt says, for each file, who reads it and where its values end up.

What you will learn

  • build: and a .env file: Compose builds the image, and fills in a variable like ${VERSION} in the file.
  • environment: and env_file:: settings for a service, one by one or from a file.
  • Named volumes and depends_on:: data that outlives the stack, and the order services start in.

More in Compose

Build "Compose builds and config" 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.