Skip to content

Working with container images

Container images can be retrieved from public registries like quay.io, docker.io and gcr.io, or from private registries like GitLab if you develop your own applications. Custom images can also be stored in the internal OKD registry if needed.

In most cases we advise using a private registry like GitLab to host your own container images.

Registry login for the internal OKD registry is described in Getting started.

Build and push images from GitLab CI (buildah)

Add the following in your gitlab pipeline: (.gitlab-ci.yml).

stages:
  - build-image

build:
  stage: build-image
  image: containers.redpill-linpro.com/cloud/k8s/buildah-rootless:latest
  variables:
    STORAGE_DRIVER: overlay
    BUILDAH_FORMAT: docker
    BUILDAH_ISOLATION: chroot
    KUBERNETES_SERVICE_ACCOUNT_OVERWRITE: "buildah-sa"
  before_script:
    # Log in to the GitLab container registry
    - buildah login -u "$CI_REGISTRY_USER" --password $CI_REGISTRY_PASSWORD $CI_REGISTRY
  script:
    - buildah images
    - buildah build -f Dockerfile -t $IMAGE_TAG
    - buildah images
    - buildah push $IMAGE_TAG
  only:
    - main

Copy an image to the internal OKD registry

variables:
  DEST_TAG: registry.apps.paas3.rlnc.eu/namespace/image:latest

stages:
  - deploy

Deploy:
  stage: deploy
  image:
    name: quay.io/containers/skopeo:latest
    entrypoint: [""]
  script:
    - printf 'Copying image to %s\n' ${DEST_TAG}
    - skopeo copy --dest-tls-verify=false --src-creds "${CI_REGISTRY_USER}:${CI_REGISTRY_PASSWORD}" --dest-creds "gitlab:${OCTOKEN}" docker://${IMAGE_TAG} docker://${DEST_TAG}

The ${OCTOKEN} is a CI/CD variable we have added in our gitlab project. This token is a non-expiry token generated by connecting a secret to the relevant service account.

Use an external registry in your deployment

An external image registry can be hosted at various locations. Redpill Linpro's GitLab instance, for example, uses containers.redpill-linpro.com.

To use an image from a private registry, the deployment needs access via an imagePullSecret.

Create the pull secret

Ad-hoc:

kubectl create secret docker-registry $SECRETNAME \
  --docker-server=$REGISTRY \
  --docker-username=$TOKENU \
  --docker-password=$TOKENP \
  -n $NAMESPACE

To generate the manifest instead of creating the secret directly (useful if you want to store it in git):

oc create secret docker-registry "$SECRETNAME" \
  --docker-server="$REGISTRY" \
  --docker-username="$TOKENU" \
  --docker-password="$TOKENP" \
  -n "$NAMESPACE" \
  --dry-run=client -o yaml > secretparsed.yaml

Defining the secret in your Kubernetes repository is recommended, but remember to encrypt it, see Secrets with KSOPS.

apiVersion: v1
kind: Secret
metadata:
  name: my-registry-secret
  namespace: <optional>
data:
  .dockerconfigjson: <base64-encoded-docker-config-json>
type: kubernetes.io/dockerconfigjson

Reference the secret in your deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  template:
    spec:
      containers:
        - name: my-container
          image: externalregistry.com/development/incident-program
      imagePullSecrets:
        - name: <secret-name>

Point to an ImageStream in a Deployment

Ref. upstream documentation

If you are using a Deployment, you can run the following to trigger automatic redeployments when your "ImageStream" gets a new image:

oc set triggers deployment/<name of deployment> --from-image=<containerimage>:latest -c <container name>

and add the following under "annotations":

annotations:
  alpha.image.policy.openshift.io/resolve-names: "*"

Push locally built image to internal OKD registry

  1. Create an empty ImageStream in your namespace

    oc create imagestream containertest
    
  2. Login to the internal OKD registry. See Getting started for method.

  3. Add a tag to your local image with correct remote path (ie., registry.apps.paas3.rlnc.eu/).

    podman tag docker.io/library/mongo-express registry.apps.paas3.rlnc.eu/dummy/containertest:2.0
    
  4. Push to internal OKD registry

    podman push registry.apps.paas3.rlnc.eu/dummy/containertest:2.0