In this challenge, you will make a containerized API server resilient to crashes.
The server is already running as a background container named app-1
and serves HTTP traffic on port 8080.
It behaves normally under light usage, but if you push enough requests through it,
it will crash and stay down.
Try overloading the server with a burst of requests and see what happens. You can use this improvised load generator (or any other tool you prefer):
for i in {1..100}; do
curl http://localhost:8080/
sleep 0.1
done
Now reconfigure the crashed app-1
container to restart on failure automatically:
Hint: Setting container restart policy 💡
Several docker
commands allow setting the restart policy for a container:
docker create
- creates a new container with the specified restart policy.docker update
- updates the restart policy for an existing container.docker run
- creates and starts a new container with the specified restart policy.
For this challenge, you can either reconfigure the crashed container
(using docker update
followed by docker (re)start
) or remove it completely and start a new one,
using the same image and the same name, but setting the correct restart policy.
In the latter case (i.e. docker run
),
you will also want to use the --publish 8080:8080
flag to make the container accessible from the host as the original one was.
Hint: Picking the right restart policy 💡
Containers can be configured with the following restart policies:
no
: The container will not restart after exiting (default).unless-stopped
: The container will always restart on exit (even if the exit code is0
) unless it was explicitly stopped by thedocker stop
command.always
: Almost the same asunless-stopped
, but a stopped (by thedocker stop
command) container will also restart if the Docker daemon itself restarts.on-failure
: The container will restart only when the process exits with a non-zero status (the one this challenge expects).
You may also consider providing a retry limit to avoid flapping using the following syntax:
docker ... --restart on-failure[:max-retries]
To see the new restart policy in action, re-run the load generator and keep an eye on the container logs. Has the server's behavior changed compared to the first time you ran the load generator?
Level up your Server Side game — Join 13,000 engineers who receive insightful learning materials straight to their inbox