Docker, start to ship · Chapter 4 of 26

Why did it die?

By the end of this chapter you can find out why a container stopped, from its logs and its exit code, and run it again with what it was missing. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Containers
  • 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

Ravi's message arrives early: "Orders API is down. Price tool too." Bunty started both last night and went home. The orders API, chai-04-api, should be serving the indiranagar kitchen on port 8004, and the price tool, chai-04-prices, should print the price list and finish. This is all that is running:

$ docker ps
CONTAINER ID  IMAGE       COMMAND     CREATED     STATUS      PORTS       NAMES

Starting them again and hoping is the guess everyone makes first. A container that died because of how it was started dies the same way on every start. Both of Bunty's containers left a note saying why: you need to know where to read it.

What you build: 4 tasks

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

  1. Task 1

    Read the end of chai-04-api's logs

    chai-04-api stopped. Its logs say why.

    1. Run docker logs on chai-04-api with --tail and a number, such as 3. It shows only the last lines.

    2. Read the last line. It says what the API was missing.

    To pass: you ran docker logs with --tail and a number on chai-04-api, in the chapter folder, and it worked. In this lab, -n does not work in place of --tail. A command with a typing mistake or an error does not count, so type it again.

  2. Task 2

    Save chai-04-api's exit code in exit-code.txt

    Find the exit code chai-04-api stopped with, and save it.

    1. Run docker ps -a, or docker inspect on chai-04-api. Find its exit code.

    2. Save that number in exit-code.txt, in the chapter folder.

    3. Do this before task 3, because task 3 deletes the old container.

    To pass: exit-code.txt has the exit code, and no other digits at all. Words around the number are fine, but not the 04 of the name.

  3. Task 3

    Run chai-04-api for the indiranagar kitchen

    Start the API again, with the setting it was missing.

    1. Delete the old chai-04-api with docker rm. Its name is taken, and its settings cannot change.

    2. Run a new container named chai-04-api from the image chai-04-api:1.0, detached (-d).

    3. Use -e to set the setting the logs named. Its value is indiranagar.

    4. Publish the container's port 3000 on the lab's port 8004.

    To pass: chai-04-api runs from chai-04-api:1.0, the setting is indiranagar in its environment, and localhost:8004 answers for the indiranagar kitchen.

  4. Task 4

    Make chai-04-prices finish with exit code 0

    chai-04-prices also failed. Bunty ran it with the wrong command.

    1. Read the logs of chai-04-prices. Python says which file it could not open.

    2. Delete the old chai-04-prices with docker rm.

    3. Run a new container named chai-04-prices from the image chai-04-prices:1.0, with nothing after the image name. Then it runs the image's own command.

    To pass: chai-04-prices is made from chai-04-prices:1.0, it finished with exit code 0, and its logs show the price list (Masala chai and the rest).

What you will learn

  • docker logs: everything the container's process printed, even after it exited, and --tail for only the end.
  • The exit code: the number the process left behind, in docker inspect and docker ps -a.
  • Settings are fixed: a container's environment and command are set when it is made, so a fix means a new container.
  • The command after the image: what replaces the image's own command.

More in Containers

Build "Why did it die?" 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.