Take a look at the following best practices to speed up image building and to set up DevSpace for an ideal workflow.
You can drastically speed up image building by optimizing your Dockerfiles with the following tips:
- Use a small base image (e.g. alpine version)
- Use layer caching for dependencies: Add dependency file, install dependencies, then add the rest of your source code as shown in the example snippet below
.dockerignore For Better Caching#
DevSpace respects the
.dockerignore file when defined on the root level of your context directory. This file follows a similar syntax as the
.gitignore file but instead of excluding files from git, the
.dockerignore file defines files and folders which should not be included in the context for building an image.
Skip Image Building
Adding paths to the
.dockerignore file makes sure that DevSpace is not forced to rebuild images when files belonging to these paths change.
It can often be useful to:
.dockerignoreto prevent config changes from triggering image rebuilding (
devspace initdoes this by default)
- Add temporary files (e.g.
.dockerignore(DevSpace ALWAYS ignores
.devspace/temp folder even if not specified in
- Add dependency folders to
.dockerignore, here are a few examples of dependency folders for different languages:
Recommended Paths for
|Language / Dependency Tool|
|PHP / composer|
|Node.js / npm|
|Python / pip|
Generally, there are several options to achieve this:
- using multiple Dockerfiles (e.g.
- using one Dockerfile in combination with
ENTRYPOINToverrides for each environment
- using one Dockerfile in combination with multi-stage builds with targets for each environment
All options have their benefits and caveats. Which one is the best for your project depends on your use case and your priorities.