Building Images
Using GitLab CI, it is possible to automatically build container (OCI) images and upload them to the local GitLab Container Registry.
For building images, we recommend using buildah with the image ict/images/buildah, see Recommended Approach.
Recommended Approach
We have prepared the image ict/images/buildah, which includes the buildah tool and performs some steps for you automatically. It is also continuously updated, and the ICT department maintains fixes for potential issues between updates.
Example usage in .gitlab-ci.yml:
build:
image: $CI_REGISTRY/ict/images/buildah:latest 1
variables:
IMAGE_TAG: my-image:latest 2
script:
- buildah build --tag $IMAGE_TAG -f Containerfile 3
- buildah push $IMAGE_TAG $CI_REGISTRY_IMAGE/$IMAGE_TAG 4- Alpine Linux with a preconfigured buildah tool that sets up the environment[1] and logs you into
$CI_REGISTRY. - The name of your new image.
- Command that builds your image according to the definition in the
Containerfile. If you use aDockerfile, this argument can be omitted. - Command that pushes the image to the current project’s Container Registry.
Example Containerfile:
# Create from Docker Hub image alpine:v3.23.
# This gets overridden when using the 'buildah --from' argument
FROM alpine:3.23
# Install ca-certificates & curl
RUN apk update && apk add ca-certificates curl
# Set TimeZone variable to Europe/Prague
ENV TZ='Europe/Prague'
# Set EU/Prague as localtime
RUN ln -sf /usr/share/zoneinfo/Europe/Prague /etc/localtimeGeneral Approach
The general approach (without using the buildah image above) is as follows:
- Install the
buildah,fuse-overlayfs, andnetavarkpackages (Alpine does not install network and filesystem drivers by default to minimize size). - Set the environment variable
BUILDAH_ISOLATION=chroot.[2] - Log in to the GitLab registry:
buildah login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY.- If the image is built using an image from Docker Hub and the project belongs to a group,
also log in to the GitLab Dependency Proxy:
buildah login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_DEPENDENCY_PROXY_SERVER.
- If the image is built using an image from Docker Hub and the project belongs to a group,
also log in to the GitLab Dependency Proxy:
- Build the image, for example:
buildah build --tag pkg:stable; when using the Dependency Proxy, you can add the--fromargument. - Upload the image to the Registry:
buildah push pkg:stable $CI_REGISTRY_IMAGE/pkg:stable.
Tips
Running a Command Inside an Image
If you need to "run" an image within a pipeline (i.e., create a container from it and execute a command inside), for example to verify functionality, do the following:
ctr=$(buildah from $IMAGE_TAG)
buildah run $ctr -- command arg1 arg2 argn