dockerfile style guide
dockerfile style guide
This will be a work in progress for some time
Dockerfile Do’s
- Follow the official best practices
- Use staged builds
- One line per target/arg on provisioning commands (apt, pip, cmake)
- Avoid cache cruft with installers (
rm -rf /var/lib/apt/lists/*for apt,--no-cache-dirfor pip) - Use
.dockerignoreand limit size of your docker context (I could do an entire post on this) - Use tini instead of
bashfor entrypoints - Mount large assets (models, databases) rather than include in the image
- Split long lines with
\
Dockerfile Don’t’s
- Put source code and build artifacts in the same parent dir
- Separate operations that add files and
rminto separateRUNdirectives. This creates bloat due to the way overlayfs works - Use command-line flags in entrypoint for anything which might vary between deploys
Interactive Container Dockerfile Don’t’s
- Don’t put anything in
/root(or whatever is$HOMEfor app user) you are not willing to mount-over- this facilitates things like mounting your
$HOMEinto a named volume for persistence
- this facilitates things like mounting your
Exceptions
- Generally avoid adding any large assets to the image directly. However, if you do not control the deployment of the image, it may be adviseable to include these in your production image. If you do so, put them before any steps which copy in code that changes frequently
- Use
pip install -e(unless you are making an interactive/dev container)
Written on December 18, 2019
