Use the Docker image guide to protect package installs during an image build. This guide adds Kubernetes workload identity and cloud reporting to a PMG-enabled image.
Requirements
- PMG v0.28.0 or later
- A Linux container image
- A workload that runs a supported package manager
- A writable home directory for the container user, or writable
PMG_CONFIG_DIRandPMG_CACHE_DIRpaths
Build a PMG-enabled image
Install PMG in each image that runs package manager commands.This example shows the minimum PMG version that supports Kubernetes identity. Check the PMG releases for the current stable version. Replace
v0.28.0 before you build the image.PATH value routes package manager commands through PMG. Keep the PMG shim directory before the real package manager directory.
The npm ci command in this example runs during the image build. PMG protects it as a Docker build command. PMG uses Kubernetes identity only for commands that run after Kubernetes starts the container.
Add the workload identity
PMG can group all replicas of one workload under one endpoint. It can also attach the Pod name and Pod UID to each install session.1
Create the Cloud Secret
Skip this step if you do not use SafeDep Cloud. Also omit Create the Kubernetes Secret in the target namespace.Delete the local file after Kubernetes creates the Secret. Do not commit the file to source control.
PMG_CLOUD_ENABLED, SAFEDEP_API_KEY, and SAFEDEP_TENANT_ID from the workload.Create a local file named safedep-cloud.env.2
Add PMG to the workload
Add the PMG settings and Kubernetes fields to the container that runs package installs.Set
KUBE_WORKLOAD_NAME to the workload name. Set KUBE_WORKLOAD_KIND to Deployment, StatefulSet, DaemonSet, Job, CronJob, ReplicaSet, or Pod.Kubernetes does not expose the owning workload through a Downward API field. The workload manifest must set these two values.3
Set the cluster name when needed
Set a cluster name when one SafeDep tenant receives events from more than one cluster.Use the same value for all workloads in one cluster.
4
Deploy the workload
Save the manifest as
checkout.yaml and apply it.How PMG names the endpoint
PMG uses one of these endpoint IDs.k8s:<namespace>/<workload>k8s:<cluster>/<namespace>/<workload>
checkout Deployment use the same endpoint ID. Each install session still includes its Pod name and Pod UID.
PMG can derive common workload names from generated Pod names. Set KUBE_WORKLOAD_NAME so the identity stays stable for every workload type.
Do not set cloud.endpoint_id or PMG_CLOUD_ENDPOINT_ID. A configured endpoint ID has higher priority than the Kubernetes identity. See the PMG configuration reference for the full priority order.
Sync short-lived workloads
PMG starts automatic cloud sync after package manager commands. This mode works for long-running Pods. A Job can stop before the automatic sync process finishes. Run a final sync before a short-lived container exits. Disable automatic sync in the short-lived container.npm ci fails. It also keeps the package manager exit status when PMG blocks a package.
An abrupt Pod deletion can remove events that PMG has not synced. Use an explicit final sync for Jobs and other short-lived workloads.
Verify the deployment
Check the PMG version and system setup./usr/local/lib/pmg/bin/npm.
Test the block with the benign SafeDep test package. The command must fail because PMG marks this package as malicious for testing.
k8s:<namespace>/checkout or k8s:<cluster>/<namespace>/checkout.
Troubleshooting
The package manager does not use the PMG shim
The package manager does not use the PMG shim
Check the Keep
PATH inside the container./usr/local/lib/pmg/bin before the package manager directory. Call pmg npm or pmg pip directly when a version manager changes PATH.The endpoint does not use a Kubernetes name
The endpoint does not use a Kubernetes name
Remove
PMG_CLOUD_ENDPOINT_ID from the image and workload. Also remove cloud.endpoint_id from the PMG config.Set KUBE_NAMESPACE and KUBE_WORKLOAD_NAME in the container environment.Each Pod appears as a different endpoint
Each Pod appears as a different endpoint
Set one
KUBE_WORKLOAD_NAME value for all replicas. Do not use the Pod name as the workload name.PMG cannot write its runtime data
PMG cannot write its runtime data
Make the container user’s home directory writable. You can also set
PMG_CONFIG_DIR and PMG_CACHE_DIR to writable paths.Do not let an init process create these directories as another user.A Job does not send its final events
A Job does not send its final events
Run
pmg cloud sync --timeout 60s before the container exits. Confirm that the container has the Cloud Secret and network access during the sync.Docker and shared VMs
Install PMG in an image or on a shared Linux host.
Package Guard
View package install events in SafeDep Cloud.
PMG configuration
Review all PMG configuration keys and endpoint identity rules.
PMG overview
Learn how PMG blocks malicious packages.

