Build an image with an attachment
In this step, we will learn how to build a Docker image and attach metadata to it. Attachments are a way to include additional information with your image, such as build provenance, software bill of materials (SBOM), or other relevant data.
First, let's create a simple Dockerfile. Navigate to the ~/project
directory if you are not already there.
cd ~/project
Now, create a file named Dockerfile
using the nano
editor.
nano Dockerfile
Add the following content to the Dockerfile
:
FROM alpine:latest
CMD ["echo", "Hello, Docker Attachments!"]
This Dockerfile is very simple. It uses the alpine:latest
image as the base and defines a command to print a message when the container starts.
Save the file and exit nano
(Press Ctrl + X
, then Y
, then Enter
).
Next, we need to create a file that we will attach to the image. Let's create a simple text file named attachment.txt
.
nano attachment.txt
Add some content to attachment.txt
, for example:
This is an example attachment for the Docker image.
It can contain any relevant metadata.
Save the file and exit nano
.
Now, we will build the Docker image and attach the attachment.txt
file. We will use the docker build
command with the --attest
flag. The --attest
flag allows you to specify an attachment to include with the image build. The format is type=type,dest=destination,src=source
.
In our case, we will use type=sbom
(Software Bill of Materials) as the type, dest=/sbom/attachment.txt
as the destination path within the attachment, and src=attachment.txt
as the source file on our local filesystem. We will also tag the image as my-attached-image:latest
.
Before building, ensure you have the necessary buildx plugin installed. If not, you might need to install it. However, for basic attachments, the built-in builder should suffice.
Let's build the image:
docker build -t my-attached-image:latest --attest type=sbom,dest=/sbom/attachment.txt,src=attachment.txt .
The .
at the end of the command specifies the build context, which is the current directory (~/project
).
You should see output indicating that the image is being built and the attachment is being processed.
After the build is complete, you can verify that the image was created by listing your local images.
docker images
You should see my-attached-image
in the list.