Docker, start to ship · Chapter 10 of 26

Config without a rebuild

By the end of this chapter you can give an image its defaults with ENV, feed values into a build with ARG, and run one image as staging and as production. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Images and Dockerfiles
  • 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

Bunty's release plan is in NOTES.md: edit server.js for staging and build tapri-staging, edit it again for production and build tapri-prod, "and hope nothing else changed in between". Ravi's question is the right one: then what we tested on staging is not what we ship. Two builds are two different images, even from almost the same code.

app/server.js already reads everything that differs from the environment: APP_VERSION, MODE and GREETING. Chapter 5's -e and --env-file can set them at run time, but an image with no defaults starts in mode unset when someone forgets, and the release number belongs to the build, not to whoever types the run line.

What you build: 4 tasks

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

  1. Task 1

    Build chai-10-api:1.0 with a version and a mode

    Build one image whose version is set at build time and whose mode has a default.

    1. Write app/Dockerfile. Start from node:24-alpine, work in /app, copy server.js in, and run node server.js.

    2. Add ARG APP_VERSION. Copy its value into ENV APP_VERSION.

    3. Add ENV MODE=staging.

    4. Build it with the app folder as the context, as chai-10-api:1.0, with --build-arg APP_VERSION=1.0.0.

    To pass: app/Dockerfile has an ARG APP_VERSION line. The environment of chai-10-api:1.0 has APP_VERSION=1.0.0 and MODE=staging. Your docker build with --build-arg APP_VERSION=1.0.0 must work, typed in the chapter folder (or a folder inside it).

  2. Task 2

    Run chai-10-staging on port 8010

    Run staging from the image, with the image's own settings.

    1. Start a container named chai-10-staging from chai-10-api:1.0, in the background.

    2. Publish the container's port 3000 on the lab's port 8010. Add no settings of your own.

    3. Run curl on localhost:8010 to see the version and the mode.

    To pass: chai-10-staging is running from chai-10-api:1.0, and localhost:8010 answers with version 1.0.0 and mode staging.

  3. Task 3

    Run chai-10-prod on port 8090 with prod.env

    Run production from the very same image, with Ravi's settings file.

    1. Do not build a new image. Use chai-10-api:1.0 again.

    2. Start a container named chai-10-prod, in the background, with the settings in prod.env.

    3. Publish the container's port 3000 on the lab's port 8090.

    4. Run curl on localhost:8090 to see the mode.

    To pass: chai-10-prod is running from the same chai-10-api:1.0 as chai-10-staging. Its environment has MODE=production and GREETING=Welcome to Tapri, and localhost:8090 answers with version 1.0.0 and mode production.

  4. Task 4 · interview question

    Interview: what ARG is for and what ENV is for

    Interview question: what is ARG for, and what is ENV for?

    1. Write one or two sentences in your own words.

    2. Say when each one exists: during the build, or in the running container.

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

    To pass: answer.txt names ARG and ENV, and talks about the build and the run.

What you will learn

  • ENV: a variable written into the image, so every container starts with it, and the run line can still change it.
  • ARG and --build-arg: a value that exists only while the image is built.
  • ARG into ENV: how a build value reaches the running app.
  • One image, two places: proving two containers run the same image.

More in Images and Dockerfiles

Build "Config without a rebuild" 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.