This article demonstrates how to use rebasing to update the operating system base layer without rebuilding subsequent layers in Docker images.
In this lesson, we demonstrate how to use rebasing to update the operating system base layer without rebuilding the subsequent layers, including the application layer. This approach is especially useful when addressing security vulnerabilities, installing new libraries, or upgrading the underlying distribution—all without the overhead of a full rebuild.
Before rebasing, ensure that your application image is built and available in Docker. Run the following command to list your Docker images:
Copy
Ask AI
docker image ls
To inspect the image details, including the buildpacks used and the runtime image, execute:
Copy
Ask AI
pack inspect myapp
The output will include detailed information similar to:
Copy
Ask AI
Inspecting image: myappREMOTE: (not present)LOCAL: Stack: Base Image: Reference: 36862ffaa256b69f1c92251e433dbe12c522f8d6d1476e792599f20c9fcb532c Top Layer: sha256:130264b1764b99aa2091ee0664a5e8dbf6ead305d43cd67407331191739e0d48 Run Images: run-base:v1 Rebasable: true Buildpacks: ID VERSION HOMEPAGE my-js-buildpack 0.0.1 - Processes: TYPE SHELL COMMAND ARGS WORK DIR web (default) node index.js /workspace
The output confirms that the current base (runtime) image is run-base:v1.
The pack inspect command provides critical insight into your image’s structure. Verifying that your image is rebasable is an important prerequisite before proceeding.
Suppose you need to update the base image—for instance, to switch from Ubuntu Jammy to Ubuntu Focal and install additional packages. First, modify your Dockerfile for the runtime image.
# Define the base imageFROM ubuntu:jammy# Install packages that we want to make available at run timeRUN apt-get update && \ apt-get install -y xz-utils ca-certificates && \ rm -rf /var/lib/apt/lists/*# Create user and groupARG cnb_uid=1000ARG cnb_gid=1000RUN groupadd cnb --gid ${cnb_gid} && \ useradd --uid ${cnb_uid} --gid ${cnb_gid} -m -s /bin/bash cnb
# Define the base imageFROM ubuntu:focal# Install packages that we want to make available at run timeRUN apt-get update && \ apt-get install -y xz-utils ca-certificates && \ rm -rf /var/lib/apt/lists/*# Create user and groupARG cnb_uid=1000ARG cnb_gid=1000RUN groupadd cnb --gid ${cnb_gid} && \ useradd --uid ${cnb_uid} --gid ${cnb_gid} -m -s /bin/bash cnb
To confirm that rebasing was successful, inspect your application image again:
Copy
Ask AI
pack inspect myapp
You should see output reflecting the updated base image, similar to:
Copy
Ask AI
Inspecting image: myappREMOTE: (not present)LOCAL:Stack: Base Image: Reference: d5f7d132c2f196de58bb1ca4fb041fa9a5829587f3cb9c01aed442f79d9b8e Top Layer: sha256:8460bddda3ad232a2e8af998246486378f5c3df30c499a08b58a89fb71 Run Images: run-base:v21Rebasable: trueBuildpacks: ID VERSION HOMEPAGE my-js-buildpack 0.0.1 https://github.com/buildpacks/samples/tree/main/buildpacks/hello-world samples/hello-world 0.0.1 https://github.com/buildpacks/samples/tree/main/buildpacks/hello-world samples/hello-moon 0.0.1 https://github.com/buildpacks/samples/tree/main/buildpacks/hello-moon
This demonstrates that only the base image layer was replaced, while the application layer remains unchanged.
Rebasing provides an efficient workflow for updating critical components like the operating system layer without incurring the overhead of a full image rebuild.