Take a look at the following best practices to speed up image building and to setup 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
FROM 8.16.1-alpine # Set working dir WORKDIR /app # Copy dependency file to WORKDIR ADD package.json # Install dependencies RUN npm install # Copy remaining source code from local WORKDIR to image WORKDIR COPY . .
.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.
Adding paths to the
.dockerignorefile makes sure that DevSpace is not forced to rebuild images when files belonging to theses 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|
Different Images for Development vs. Staging vs. Production
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 one for your project depends on your use case and your priorities.