Docker, start to ship · Chapter 5 of 26

Run options

By the end of this chapter you can change how a container runs (its settings, its name on the network, its labels, its command) on the docker run line, without building a new image. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Containers
  • 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 is opening its Indiranagar branch. Ravi has written the branch's settings into branch.env, and Meera has one change: chai is Rs 25 during launch week. Ravi also wants the price tool asked a quick question now and then, without containers piling up after it.

Bunty's plan is to edit the code and build a new image for every branch and every price change. That is a new image to build, store and keep track of each time, and the price changes back next week. One image can serve every branch: what changes goes on the docker run line.

What you build: 5 tasks

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

  1. Task 1

    Start chai-05-api with branch.env, chai at 25

    Start the branch API with the settings in branch.env, but with Meera's new chai price.

    1. Run a container named chai-05-api from chai-05-api:1.0, detached (-d).

    2. Give it every setting in branch.env with --env-file branch.env.

    3. Set CHAI_PRICE to 25 with -e. It wins over the 20 in the file.

    4. Publish the container's port 3000 on the lab's port 8005. Put every option before the image name.

    To pass: chai-05-api is running, its environment has TAPRI_BRANCH=indiranagar, OPENS_AT=7am and CHAI_PRICE=25, and localhost:8005 shows chai at 25. You started it with --env-file, in the chapter folder, and the command worked.

  2. Task 2

    Give chai-05-api its hostname and label

    chai-05-api also needs a hostname and a label.

    1. Give it the hostname chai-05-indiranagar with --hostname.

    2. Give it the label team=orders with --label.

    3. Both are fixed when the container is made. If chai-05-api runs without them, delete it with docker rm -f and run it again with every option from task 1 too.

    To pass: chai-05-api has the hostname chai-05-indiranagar and the label team=orders, and task 1 still passes.

  3. Task 3

    Ask the price tool about a samosa

    Ask the price tool for the price of a samosa.

    1. Run a container from chai-05-prices:1.0 with --rm before the image name.

    2. Put samosa after the image name. The tool prints its price, and the container deletes itself.

    3. Run docker ps -a. Delete any container made from chai-05-prices.

    To pass: you ran docker run with --rm, chai-05-prices:1.0 and samosa, in the chapter folder, and it worked. No container made from chai-05-prices is left, from this task or any other.

  4. Task 4

    List the price tool's /tool folder

    Look inside the price tool's image.

    1. Run a container from chai-05-prices:1.0 with --entrypoint ls, so it runs ls in place of its program.

    2. Put the folder to list, /tool, after the image name.

    3. Add --rm too, so no container is left for task 3.

    To pass: you ran docker run with --entrypoint before chai-05-prices:1.0, in the chapter folder, and it worked. A command with a typing mistake or an error does not count, so type it again.

  5. Task 5 · interview question

    Interview: what --rm saves you from

    Interview question: what does --rm save you from?

    1. Think about what happens to one-off containers without --rm.

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

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

    To pass: answer.txt says what happens to finished containers without --rm.

What you will learn

  • Settings from outside: -e and --env-file for environment variables, --hostname and --label.
  • Options before the image: everything after the image name is the command.
  • Command and entrypoint: what the words after the image replace, and --entrypoint to run another program.
  • --rm: a container that removes itself when it exits.

More in Containers

Build "Run options" 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.