- The resulting image is smaller in size
- The ‘attack surface’ of your application is smaller
Dockerfile from scratch. Here’s part 1:
In this example we assume the use of
poetry, however you can adapt the file to work with other dependency managers too.builder gives us a way to interact with it later. What essentially happens here is exactly what happens when you install a project locally using poetry: a .venv/ directory is created and in it are all your built dependencies and binaries. You can inspect your own .venv/ folder to see what that looks like. This directory is the primary artifact that we want.
Part 2, the runtime, looks something like this:
.venv/ folder over to this image without having to compile it again.
With this setup our image will be around 200MB most of the time (depending on what else you include). This setup is used for nearly all Python apps you deploy on Fly.io.
The image size is largely dependent on what files you add in the dockerfile; by default the entire working directory is copied in. If you do not want to add certain files, you can specify them in a
.dockerignore file.