Quickstart
Use caching
One of Dagger's most powerful features is its ability to cache data across pipeline runs. Dagger lets you define one or more directories as cache volumes and persists their contents across runs. This enables you to reuse the contents of the cache volume(s) every time the pipeline runs, and thereby speed up pipeline operations.
You may have noticed that the example pipeline executes the npm install
command to download the application's dependencies every time the pipeline runs. Since these dependencies are usually locked to specific versions in the application's manifest, re-downloading them on every pipeline run is inefficient and time-consuming.
This step is, therefore, a good candidate for a cache volume. Let's update the pipeline accordingly.
The npm install
command is appropriate for a React application, but other applications are likely to use different commands. Modify your Dagger pipeline accordingly.
This revised pipeline now uses a cache volume for the application dependencies.
- It uses the client's
CacheVolume()
method to initialize a new cache volume. - It uses the
Container.WithMountedCache()
method to mount this cache volume at thenode_modules/
mount point in the container. - It uses the
Container.WithExec()
method to define thenpm install
command. When executed, this command downloads and installs dependencies in thenode_modules/
directory. Since this directory is defined as a cache volume, its contents will persist even after the pipeline terminates and can be reused on the next pipeline run.
Run the pipeline by executing the command below from the application directory:
dagger run go run ci/main.go
This revised pipeline now uses a cache volume for the application dependencies.
- It uses the client's
cacheVolume()
method to initialize a new cache volume. - It uses the
Container.withMountedCache()
method to mount this cache volume at thenode_modules/
mount point in the container. - It uses the
Container.withExec()
method to define thenpm install
command. When executed, this command downloads and installs dependencies in thenode_modules/
directory. Since this directory is defined as a cache volume, its contents will persist even after the pipeline terminates and can be reused on the next pipeline run.
Run the pipeline by executing the command below from the application directory:
dagger run node ci/index.mjs
This revised pipeline now uses a cache volume for the application dependencies.
- It uses the client's
cache_volume()
method to initialize a new cache volume. - It uses the
Container.with_mounted_cache()
method to mount this cache volume at thenode_modules/
mount point in the container. - It uses the
Container.with_exec()
method to define thenpm install
command. When executed, this command downloads and installs dependencies in thenode_modules/
directory. Since this directory is defined as a cache volume, its contents will persist even after the pipeline terminates and can be reused on the next pipeline run.
Run the pipeline by executing the command below from the application directory:
dagger run python ci/main.py
This revised pipeline produces the same result as before.
Run the pipeline a few times. Notice that on the first run, the application dependencies are downloaded as usual. However, since the dependencies are cached, subsequent pipeline runs will skip the download operation and be significantly faster (assuming that there are no other changes to the application code).
In addition to cache volumes, Dagger has a separate cache for pipeline operations. Changes in cache volumes do not invalidate the Dagger pipeline operations cache.
- Go
- Node.js
- Python