7.3 The Docker recipes
Build it yourself. Read the tutorial is the other way to read this part.
This walk goes through rahuldave/cheesecracker on GitHub. This part starts from the code at 0acb4556f724, partway through step-07. The code before this part
This part does not start where the part before it ends, because the walk skips some changes. This command puts the files that this part starts from in place of yours. Run it, and then go on:
git fetch --quiet --tags --force origin; git restore --source step-06 --staged -- :/ && git restore --source 0acb4556f724 --staged --worktree -- :/If you start at this part, make your clone as Get the project says, and then run this command. It puts in your clone the files that this part starts from. A file that the project deleted before this part stays, so delete it if git check lists it:
git rm -rq --cached --ignore-unmatch -- :/ && git restore --source 0acb4556f724 --staged --worktree -- :/just setupDocker packs the server, with Python and its packages, into an image. It builds the image
from a file called Dockerfile, and runs it as a container, a process kept apart from the rest of the machine. The
whole tutorial then writes .dockerignore, and a Dockerfile that makes the run image, the image that runs the
server. Three recipes build an image, start its server and stop it.
7.3 just docker-build, just docker-serve and just docker-stop
Three recipes give the Docker commands short names. docker-build builds an image of the Dockerfile.
docker-serve starts the server of the run image in the background, at port 9000, and docker-stop stops it.
Apply the patch to justfile, from the top of the project:
curl -fsSL https://github.com/rahuldave/cheesecracker/commit/d444e0387d256c62024581e98f59d670cbc93296.diff | git apply --include=justfileRead the recipes docker-build, docker-serve and docker-stop at the end of the justfile.
just docker-build runDocker takes each layer that has not changed since the last build from its cache, and prints CACHED for it.
just docker-serveThe recipe prints docker-serve: http://localhost:9000, in the container cheesecracker-run. Ask the server for its
health.
curl -s localhost:9000/healthThe server in Docker answers {"status":"healthy"}, as the server on this machine did. The container runs in the
background, so you do not see what it prints. Docker keeps that output, and docker logs shows it.
docker logs cheesecracker-runuvicorn printed Uvicorn running on http://0.0.0.0:9000, and started four server processes, its workers.
just docker-stopDocker prints the name of the container, cheesecracker-run, and the server stops.
Check your files against this move. The command prints nothing when they match, and otherwise shows what differs:
git check d444e0387d25If you are stuck, this command puts the files of this move in place of yours. A file that you made yourself stays:
git fetch --quiet --tags --force origin; git restore --source 0acb4556f724 --staged -- :/ && git restore --source step-07 --staged --worktree -- :/What changed
justfile: the recipesdocker-build,docker-serveanddocker-stop. 20 lines added. The file- look at
docker run --rm --detach --name cheesecracker-run --publish 9000:9000 cheesecracker-run > /dev/null || exit 1
- look at
The patch to justfile
@@ -62,3 +62,23 @@ check:
just fmt --check
just lint
just test -q
+
+# Build an image of the Dockerfile: just docker-build run
+docker-build kind:
+ docker build --target "$1" --tag "cheesecracker-$1" .
+
+# Start the server in the run image, in the background, at http://localhost:9000, as it runs in production. just docker-stop stops it
+docker-serve:
+ #!/usr/bin/env sh
+ if curl -sf localhost:9000/health > /dev/null; then echo "docker-serve: a server already answers at http://localhost:9000" >&2; exit 1; fi
+ docker run --rm --detach --name cheesecracker-run --publish 9000:9000 cheesecracker-run > /dev/null || exit 1
+ for i in $(seq 1 30); do
+ if curl -sf localhost:9000/health > /dev/null; then echo "docker-serve: http://localhost:9000, in the container cheesecracker-run"; exit 0; fi
+ sleep 1
+ done
+ echo "docker-serve: no answer after 30 seconds; read docker logs cheesecracker-run" >&2
+ exit 1
+
+# Stop the server of just docker-serve, and remove its container
+docker-stop:
+ docker stop cheesecracker-runThis part ends at the code at step-07. The code after this part