Docker, start to ship · Chapter 18 of 26
Stay alive
By the end of this chapter you can make Docker restart a container that fails, cap the CPU and processes it may use, and read an exit code. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Ports and config
- 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
At 2 a.m. the accountant's server did not answer, and Tapri's nightly worker, chai-18-worker, gave up with it. Nobody noticed until morning:
$ docker logs chai-18-worker
worker: sending yesterday's orders to the accountant
worker: the accountant's server did not answer
docker ps -a lists it as Exited (1). Ravi wants the worker to try again by itself, and the menu server, chai-18-menu, kept up whatever happens to it, without eating the whole machine. So far a stopped container stayed stopped until you typed docker start, and any container could take every CPU: one runaway slows all the others.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Give chai-18-worker a retry policy, at most 5 times
Bunty's worker failed at 2 a.m. and nothing started it again. Give it a restart policy.
1. Read the worker's logs and its exit code.
2. Delete chai-18-worker. A restart policy is set when a container is made.
3. Run a new chai-18-worker from chai-18-worker:1.0, in the background.
4. Give it the restart policy on-failure, with at most 5 retries.
To pass: chai-18-worker has the policy on-failure with 5 retries, and Docker has restarted it at least once. The worker fails after about 3 seconds, so wait a few seconds before you press Check.
- Task 2
Run chai-18-menu on port 8018 with a policy and limits
Run the menu server so it stays up, and limit what it can use.
1. Run a container named chai-18-menu from nginx, in the background.
2. Publish its port 80 on the lab's port 8018.
3. Give it the restart policy unless-stopped (always also counts).
4. Give it half a CPU (0.5), and a limit of 100 processes.
To pass: chai-18-menu has the policy unless-stopped or always, exactly 0.5 CPUs, a limit of exactly 100 processes, port 80 published on 8018, and localhost:8018 shows the Welcome to nginx page.
- Task 3
Crash chai-18-menu and watch it come back
Crash the menu server on purpose, and see Docker start it again by itself.
1. Run a command inside chai-18-menu that ends its main process, process 1 (nginx).
2. Do not use docker stop, docker kill or docker restart. Docker treats those as you, not as a crash.
3. Wait a second or two. Then ask docker inspect for its RestartCount and its state.
To pass: chai-18-menu has a RestartCount above 0, it is running, and localhost:8018 shows the Welcome to nginx page again.
- Task 4 · interview question
Interview: what exit code 137 means
Interview question: a container exits with code 137. How did its main process end?
1. An exit code above 128 means a signal ended the process. Take 128 away from 137.
2. Run kill -l with that number. It shows the signal's name.
3. Write one or two sentences in your own words, and save them in answer.txt, in the chapter folder.
To pass: answer.txt says how the process ended: the signal's name, or what sent it.
What you will learn
- --restart: what Docker does when a container's main process exits, and RestartCount.
- --cpus and --pids-limit: a share of the CPU and a cap on processes.
- Exit codes above 128: a process killed by a signal, and how to read which one.
More in Ports and config
Build "Stay alive" 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.