Pyjoyment.Command.Author.generate.dockerfile - Dockerfile generator command
Usage: APPLICATION generate dockerfile [OPTIONS]
./myapp.py generate dockerfile
python -m MyApp generate dockerfile
python -m MyApp generate dockerfile --user app
python -m MyApp generate dockerfile --port 8080
python -m MyApp generate dockerfile --graceful-timeout 8
python -m MyApp generate dockerfile --healthcheck /
python -m MyApp generate dockerfile --target lambda
python -m MyApp generate dockerfile --compress public
python -m MyApp generate dockerfile --apt-package brotli --apt-package zstd --compress public
python -m MyApp generate dockerfile --apt-package brotli --compress public --compress-encoding br --compress-encoding gzip
Options:
-a, --apt-package <name> Install a package of Debian in the
image, can be repeated
-c, --compress <path> Write the variants of gzip, and of
brotli and zstd if the image has them,
of the static files of a directory in
the image, can be repeated
-C, --compress-pattern <glob> Name of the files that --compress
compresses, can be repeated, defaults to
the formats of text
-e, --compress-encoding <name> Encoding of the variants that --compress
writes, "br", "zstd" or "gzip", can be
repeated, defaults to gzip and the ones
that the image has
-G, --graceful-timeout <seconds> Graceful timeout of the workers of the
server, defaults to the one of the
server
-H, --healthcheck <path> Path that the HEALTHCHECK of the image
requests, defaults to the liveness of
the plugin Health, if the application
has it
-h, --help Show this summary of available options
-p, --port <port> Port that the server listens on and the
image exposes, defaults to 3000
-t, --target <name> Target of the image, "lambda" for an
event function of AWS Lambda, defaults
to a server
-u, --user <name> Create a system user and run the
application as it, defaults to the user
of the image
Pyjoyment.Command.Author.generate.dockerfile generates Dockerfile
and .dockerignore for applications.
The image is built in two stages. The first one installs the dependencies
with uv into .venv: the ones of pyproject.toml, which
Pyjoyment.Command.Author.generate.pyproject generates, locked with
uv.lock if there is one, or the ones of the inline metadata of PEP 723 of
a Pyjoyment.Lite script that has it and no pyproject.toml, locked
with its .lock file if there is one. Without either, the generator fails
with the commands that add them. The second one has Python, .venv and the
application only, and its command runs the pre-forking server without
uv, so that the signals of docker stop go straight to the server, which
stops gracefully on TERM. The images of Python and of uv are those of the
versions that run the generator.
Docker and Cloud Run give a container 10 seconds to stop, less than the 25
seconds that Pyjo.Server.Prefork.Pyjo_Server_Prefork.graceful_timeout
gives a worker, so --graceful-timeout 8 sets PYJO_GRACEFUL_TIMEOUT in
the image, which the command prefork takes for its option, and which the
environment of the container can change again. With Docker,
docker stop -t 30, or stop_grace_period: 30s of Docker Compose, lets
the workers finish their requests too.
The server listens on the port of the PORT environment variable, which
the image sets to the port of --port, 3000 by default, and exposes,
and which docker build --build-arg PORT=8080 changes too. A platform as
Knative, Cloud Run, Render, Koyeb or Heroku sets PORT for the container,
and the server listens on its port, as Knative ignores EXPOSE and sets
PORT to the containerPort of the service, 8080 by default.
An application with Pyjoyment.Plugin.Health gets a HEALTHCHECK
that requests its liveness, /livez by default, or the path of
--healthcheck, and an empty path leaves it out. Python makes the request
with http.client, as the image has no curl, and Docker shows the
health of the container in docker ps, and Docker Compose waits for it with
depends_on and condition: service_healthy. Kubernetes and Cloud Run
ignore it and have probes of their own.
With --apt-package, the image has packages of Debian, which apt-get
installs before the application is copied, so that the layer stays in the
cache of the build while the application changes: the libraries that a
dependency of the application loads, as libpq5, or tools, as brotli
and zstd.
With --compress, the image has the precompressed variants of the static
files of a directory, as public, which Pyjoyment.Static serves to
the clients that take them: .gz of gzip -k -9 -n of the image, and
.br of brotli -k -q 11 and .zst of zstd -k -19 if the image has
them, as --apt-package brotli --apt-package zstd installs them, each with
the mtime of its file. It compresses the formats of text, CSS, JavaScript,
JSON, source maps, SVG, HTML, plain text, XML, WebAssembly and fonts in
TrueType and OpenType, or the names of --compress-pattern, as the formats
of images, video, audio, the WOFF fonts, PDF and archives are compressed
already, and leaves out the files smaller than
min_compress_size of the
renderer.
With --compress-encoding, it writes the variants of those encodings only,
as br and gzip for CloudFront, which forwards no other encoding of
Accept-Encoding to the origin, and the build fails without their tools.
With --target lambda, the image is the one of an event function of AWS
Lambda: its command runs the script of the application, or its package, which
is the runtime of the function itself, as Pyjoyment.Command.lambda
detects, with the runtime client of Pyjo.Server.Lambda, and it has no
port, no HEALTHCHECK and no graceful timeout of a server, whose options
are not for it. A function whose Function URL streams its responses has
PYJO_LAMBDA_INVOKE_MODE=RESPONSE_STREAM in its environment, as
Pyjo.Server.Lambda.Pyjo_Server_Lambda.invoke_mode says.
This is a core command, that means it is always enabled and its code a good
example for learning to build new commands, you’re welcome to fork it.
See Pyjoyment.Commands for a list of commands that are available by
default.