Default Auto-Generated Dockerfile
Buildr creates a simple Dockerfile based on yourPackageOptions configuration:
{version}: Value fromPackageOptions.py_version(default: 3){exec_file}: Your script’s filename
How It Works
When you decorate a function with@Containerize:
Container detection
Buildr checks if it’s already running inside a container using
/proc/1/cgroup and the METAPARTICLE_IN_CONTAINER environment variable.Dockerfile generation
If not in a container, Buildr calls
write_dockerfile() to generate the Dockerfile with your configured Python version and script name.Limitations of Auto-Generation
The default Dockerfile works well for simple applications but has limitations:- Uses Alpine Linux (small but may have compatibility issues with some Python packages)
- Installs all dependencies from
requirements.txtwithout layer caching optimization - No support for system packages (apt, apk)
- No multi-stage builds
- No custom build arguments or environment variables
- No custom base images
- Requires
requirements.txtat project root
Custom Dockerfile Support
Source reference: option.py:42-47, containerize.py:33-36Workarounds
If you need features beyond the auto-generated Dockerfile, consider these alternatives:Option 1: Pre-build Your Image
Build your Docker image separately, then reference it directly:Option 2: Modify Generated Dockerfile
You could manually edit the generatedDockerfile after Buildr creates it, but this is not recommended as it will be overwritten on the next run.
Option 3: Fork and Modify Buildr
To properly support custom Dockerfiles, you would need to:- Add
dockerfileto the PackageOptions namedtuple fields - Modify
write_dockerfileto properly handle the custom dockerfile path - Test the changes
option.py:
Optimizing the Auto-Generated Dockerfile
While you can’t replace the Dockerfile, you can optimize your project structure to work better with the auto-generated one:Use requirements.txt Efficiently
The auto-generated Dockerfile copies all files first, then installs requirements. To minimize build time:Use .dockerignore
Create a.dockerignore file to exclude unnecessary files from the build context:
Choose the Right Python Version
Configurepy_version based on your needs:
Alpine-based images are smaller but may have compatibility issues with some Python packages that require compilation. If you encounter issues, you may need to pre-build with a custom Dockerfile outside of Buildr.
When Buildr’s Auto-Generation Works Well
Buildr’s auto-generated Dockerfiles are suitable for:- Simple Python applications with pure-Python dependencies
- Microservices without system-level dependencies
- Quick prototyping and development
- Applications that work well on Alpine Linux
- Projects with straightforward requirements.txt dependencies
When You Need More
If your application needs any of these, you’ll need to build images outside of Buildr:- System packages (postgresql-client, build tools, etc.)
- Multi-stage builds for optimization
- Non-Python base images
- Custom build arguments
- Compiled extensions with specific build requirements
- Security hardening (non-root users, minimal base images)
- Complex layer caching strategies
Example: Working Within Limitations
Here’s a complete example that maximizes what Buildr can do:Next Steps
Basic Usage
Learn the fundamentals of the @Containerize decorator
Advanced Configuration
Explore PackageOptions and RuntimeOptions