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.

See also

Pyjoyment, https://mojolicious.org.

Ported from Mojolicious::Command::Author::generate::dockerfile of Mojolicious 9.49.

Classes

class Pyjoyment.Command.Author.generate.dockerfile.Pyjoyment_Command_Author_generate_dockerfile(**kwargs: Any)[source]

Pyjoyment.Command.Author.generate.dockerfile inherits all attributes and methods from Pyjoyment.Command and implements the following new ones.

compress_patterns: tuple[str, ...] = ('*.css', '*.csv', '*.html', '*.js', '*.json', '*.map', '*.mjs', '*.otf', '*.svg', '*.ttf', '*.txt', '*.wasm', '*.xml')
patterns = dockerfile.compress_patterns
dockerfile.compress_patterns = ('*.css', '*.js')

Names of the files that --compress compresses without --compress-pattern, the formats of text, which compress well.

description = 'Generate "Dockerfile"'
description = dockerfile.description
dockerfile.description = 'Foo'

Short description of this command, used for the command list.

run(*args: str) → None[source]
dockerfile.run(*sys.argv[1:])

Run this command.

usage = 'Usage: APPLICATION generate dockerfile [OPTIONS]\n\n  ./myapp.py generate dockerfile\n  python -m MyApp generate dockerfile\n  python -m MyApp generate dockerfile --user app\n  python -m MyApp generate dockerfile --port 8080\n  python -m MyApp generate dockerfile --graceful-timeout 8\n  python -m MyApp generate dockerfile --healthcheck /\n  python -m MyApp generate dockerfile --target lambda\n  python -m MyApp generate dockerfile --compress public\n  python -m MyApp generate dockerfile --apt-package brotli --apt-package zstd --compress public\n  python -m MyApp generate dockerfile --apt-package brotli --compress public --compress-encoding br --compress-encoding gzip\n\nOptions:\n  -a, --apt-package <name>           Install a package of Debian in the\n                                     image, can be repeated\n  -c, --compress <path>              Write the variants of gzip, and of\n                                     brotli and zstd if the image has them,\n                                     of the static files of a directory in\n                                     the image, can be repeated\n  -C, --compress-pattern <glob>      Name of the files that --compress\n                                     compresses, can be repeated, defaults to\n                                     the formats of text\n  -e, --compress-encoding <name>     Encoding of the variants that --compress\n                                     writes, "br", "zstd" or "gzip", can be\n                                     repeated, defaults to gzip and the ones\n                                     that the image has\n  -G, --graceful-timeout <seconds>   Graceful timeout of the workers of the\n                                     server, defaults to the one of the\n                                     server\n  -H, --healthcheck <path>           Path that the HEALTHCHECK of the image\n                                     requests, defaults to the liveness of\n                                     the plugin Health, if the application\n                                     has it\n  -h, --help                         Show this summary of available options\n  -p, --port <port>                  Port that the server listens on and the\n                                     image exposes, defaults to 3000\n  -t, --target <name>                Target of the image, "lambda" for an\n                                     event function of AWS Lambda, defaults\n                                     to a server\n  -u, --user <name>                  Create a system user and run the\n                                     application as it, defaults to the user\n                                     of the image\n'
usage = dockerfile.usage
dockerfile.usage = 'Foo'

Usage information for this command, used for the help screen.