Skip to content

Building in Gitlab

To learn how to use containers and :fontawesome-brands-docker:Docker on your local machine, refer to our tutorial section.

We use our own installation of :fontawesome-brands-gitlab:GitLab for Source Code Management, Continuous Integration automation, containers registry and other development lifecycle tasks. It fully uses Nautilus Cluster resources, which provides our users plenty of storage and fast builds. All data from our GitLab except container images are backed up nightly to Google storage, which means there’s almost zero chance that you might lose your code in our repository.

Step 1: Create a Git repo

  1. To use our GitLab installation, register at https://gitlab.nrp-nautilus.io
  2. Use GitLab for storing your code like any git repository. Here’s GitLab basics guide.
  3. Create a new project in your GitLab account

Step 2: Use Containers Registry

What makes GitLab especially useful for kubernetes cluster in integration with Containers Registry. You can store your containers directly in our cluster and avoid slow downloads from DockerHub (although you’re still free to do that as well).

If you wish to use our registry, in your https://gitlab.nrp-nautilus.io project go to Deploy -> Container Registry menu and read instructions on how to use one.

Step 3: Continuous Integration automation

To fully unleash the GitLab powers, introduce yourself to Continuous Integration automation and more advanced DevOps article.

  1. Create the .gitlab-ci.yml file in your project, see Quick start guide. The runners are already configured.
    There’s a list of CI templates available for most common languages.
  2. If you need to build your Dockerfile and create a container from it, adjust this .gitlab-ci.yml template (remove --cache=true if you don’t need layer caching):
image: ghcr.io/osscontainertools/kaniko:debug
stages:
- build-and-push
build-and-push-job:
stage: build-and-push
variables:
GODEBUG: 'http2client=0'
script:
- echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
- /kaniko/executor --cache=true --push-retry=10 --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA --destination $CI_REGISTRY_IMAGE:latest

The above Kaniko builder has severe speed problems pushing to GitLab, which is resolved by setting the environment variable GODEBUG="http2client=0".

Each job on the shared runners runs in its own virtual machine. By default it gets 4 CPUs, 8Gi of memory and 50Gi of ephemeral storage. A job that needs more asks for it in its variables, up to the runner’s caps:

VariableDefaultCap
KUBERNETES_CPU_REQUEST / KUBERNETES_CPU_LIMIT1 / 42 / 12
KUBERNETES_MEMORY_REQUEST / KUBERNETES_MEMORY_LIMIT8Gi / 8Gi50Gi
KUBERNETES_EPHEMERAL_STORAGE_REQUEST / KUBERNETES_EPHEMERAL_STORAGE_LIMIT0 / 50Gi1Ti

Image builds are the usual reason to raise them. Kaniko writes every layer to the pod’s disk, and a build that unpacks a large archive holds it several times at once. A build that goes over its ephemeral-storage limit is evicted with Container build exceeded its local ephemeral storage limit, and one that runs out of memory ends with exit code 137. Set the request and the limit of ephemeral storage to the same value, so that the job is placed on a node with that much disk (see Adjust ephemeral-size to your data volume), and ask for what the build uses, because the nodes’ disks are shared.

Two Kaniko settings keep large builds small. --compressed-caching=false stops Kaniko from keeping each compressed layer in memory, at the cost of compressing it again from disk. The job variable FF_KANIKO_SHARED_BASE_CACHE: "0" stops Kaniko v1.29 and later from first downloading the whole base image to disk, which adds the image’s size to the build’s disk use and ends the build if the connection drops during that download. The debug image tag follows each release, and Kaniko’s README schedules that variable for deprecation in v1.30.0, after which it may have no effect, so look for shared-base: lines in the build log after an upgrade.

The below example is the variant for using Docker (as there is only one dedicated build server available, only use when image compatibility with the Docker builder is an important priority):

image: docker:dind
default:
tags:
- docker
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker buildx create --driver docker-container --bootstrap --use
stages:
- build-and-push
build-and-push-job:
stage: build-and-push
script:
- cd $CI_PROJECT_DIR && docker buildx build -f Dockerfile --push --provenance=false --platform linux/amd64 -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA -t $CI_REGISTRY_IMAGE:latest .

More advanced example

  1. Go to Build -> Jobs tab to see in amazement your job running and image being uploaded to your registry.
  2. From the Deploy -> Container Registry tab get the URL of your image to be included in your pod definition:
spec:
containers:
- name: my-container
image: gitlab-registry.nrp-nautilus.io/<your_group>/<your_project>:<optional_tag>

If the project is private, let your pods pull the image with a deploy token: see Private Repos.

Deploy from CI/CD to your namespace

To let CI/CD jobs deploy to your namespace, for example to roll out each new image, connect the GitLab agent for Kubernetes to your namespace.

Multiarch builds

Nautilus has several ARM64 nodes, which require a specifically build images to run on.

Refer to Creating Multi-arch Container Manifests Using Kaniko and Manifest-tool to create multiarch builds in Kaniko.

Docker can build images for multiple architectures and automatically create a manifest which will allow using the same image path on different architectures, provided by the buildx tool.

Here’s the example of such CI definition:

image: docker:dind
default:
tags:
- docker
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker buildx create --driver docker-container --bootstrap --use
stages:
- build-and-push
build-and-push-job:
stage: build-and-push
script:
- cd $CI_PROJECT_DIR && docker buildx build -f Dockerfile --push --provenance=false --platform linux/amd64,linux/arm64 -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA -t $CI_REGISTRY_IMAGE:latest .

Build better containers

Make yourself familiar with Docker containers best practices.

Use multi-stage builds when necessary.

Use S3 to store large files collections and access those during builds

Refer to S3 documentation.

Other development information

Read the Guide from the Netherlands eScience Center for best practices in developing academic code.

Also a thesis on measuring container registry performance.

U.S. National Science Foundation

This work was supported in part by National Science Foundation (NSF) awards CNS-1730158, ACI-1540112, ACI-1541349, OAC-1826967, OAC-2112167, CNS-2100237, CNS-2120019.