How to use the OCM CLI container image
On this page
Goal
This How-To will show you how to use the OCM CLI container image to create and get a component version.
You will end up with
- A component version created in an OCM repository using the OCM CLI container image.
Estimated time: ~5 minutes
Prerequisites
- OCM CLI installed.
- Access to an OCI registry. We will use GitHub Container Registry (GHCR) in this example.
- Required credentials/keys available.
- Docker CLI installed ( Docker Engine/ Docker Desktop).
- A container image CLI installed, e.g. Docker, Podman, or nerdctl.
- Internet access to pull the OCM CLI container image and the application image used in the example.
Steps
Create a simple component constructor
We will start by creating a simple component constructor. This will be used to create a component version in the next step.
cat > component-constructor.yaml << EOF components: - name: ocm.software/how-to-container-image-usage version: 1.0.0 provider: name: test.domain resources: - name: image type: ociImage version: 1.0.0 access: type: ociArtifact imageReference: ghcr.io/stefanprodan/podinfo:6.7.1 EOFConfigure credentials in an OCM config
We want to create the component version in a repository that requires authentication. In the following example, we will use GitHub Container Registry (GHCR) as the repository and use a Personal Access Token (PAT) for authentication.
cat > .ocmconfig << EOF type: generic.config.ocm.software/v1 configurations: - type: credentials.config.ocm.software consumers: - identity: type: OCIRegistry hostname: ghcr.io port: "443" scheme: https credentials: - type: Credentials properties: username: <user> password: <PAT> EOFIf you do not use
ghcr.ioas your registry, make sure to update theidentityfields in the configuration accordingly.Create the component version using the OCM CLI container image
Now we can use the OCM CLI container image to create the component version in the repository. We will mount the current directory containing the
component-constructorand.ocmconfigto the container image and run theadd cvcommand.docker run --rm \ -v "$(pwd)":/workspace:ro \ -w /workspace \ ghcr.io/open-component-model/cli:latest \ add cv \ --constructor component-constructor.yaml \ --repository ghcr.io/<user> \ --config ".ocmconfig"Expected output
COMPONENT │ VERSION │ PROVIDER ───────────────────────────────────────────┼─────────┼───────────── ocm.software/how-to-container-image-usage │ 1.0.0 │ test.domainYou can also mount certificates to the container image by adding
-v /etc/ssl/certs/:/etc/ssl/certs/:roto your command (Depending on your OS, the path to the certificates may be different).Get the component version using the OCM CLI container image
After we created the component version, we can use the OCM CLI container image to get the component version from the repository.
docker run --rm \ -v "$(pwd)":/workspace:ro \ -w /workspace \ ghcr.io/open-component-model/cli:latest \ get cv \ ghcr.io/<user>//ocm.software/how-to-container-image-usage:1.0.0 -oyaml \ --config ".ocmconfig"Expected output
- component: componentReferences: null name: ocm.software/how-to-container-image-usage provider: test.domain repositoryContexts: null resources: - access: imageReference: ghcr.io/stefanprodan/podinfo:6.7.1@sha256:862ca45e61b32392f7941a1bdfdbe5ff8b6899070135f1bdca1c287d0057fc94 type: ociArtifact digest: hashAlgorithm: SHA-256 normalisationAlgorithm: genericBlobDigest/v1 value: 862ca45e61b32392f7941a1bdfdbe5ff8b6899070135f1bdca1c287d0057fc94 name: image relation: external type: ociImage version: 1.0.0 sources: null version: 1.0.0 meta: schemaVersion: v2