Skip to content
subuilds.dev

← logbuild #3 passed8c58975

Setting Up GitLab Container Registry: A Complete Guide for Self-Hosted Instances

Learn how to set up GitLab Container Registry on a self-hosted instance. Covers Docker image management, CI/CD use, and common problems such as Cloudflare Tunnel settings.

· 11 min read

Introduction#

Self-hosted GitLab comes with a built-in Container Registry. A container registry is a place that stores Docker images. With it, you keep images on your own server and do not pull them from Docker Hub. This guide covers setup, usage, and common mistakes, mainly behind Cloudflare Tunnel.

Why Use GitLab Container Registry?#

  • Avoid Docker Hub rate limits - No limit on pulls
  • Faster CI/CD pipelines - Images cached on your network
  • Consistent versions - All projects share the same base images
  • One place for all images - You manage images in one project
  • Better security - Images stay on your own servers

In Simple Words#

The problem
Every pipeline pulls its base images from Docker Hub. Docker Hub limits your pulls, pulls are slow, and projects use different image versions.
The root cause
The images live on a server outside your network. Each project also picks its own image tag.
The solution
Turn on the Container Registry that comes with self-hosted GitLab. Keep all base images in one project and push them with a CI job. Every other project pulls from there.
Think of it like this
A cook who walks to a far shop for salt before every meal is slow. Some days the shop says “no more today”. Keep a store room at home: fill it once, and every cook takes from the same shelf.

Prerequisites#

You need:

  • GitLab EE/CE instance (version 15.0+)
  • Admin access to GitLab
  • Docker installed locally
  • Basic Docker knowledge

Part 1: Enabling Container Registry#

Step 1: Verify Registry is Enabled#

New GitLab versions turn on the Container Registry by default. To check:

  1. Go to Admin Area → Settings → General
  2. Open Visibility and access controls
  3. Look for Container Registry settings

Or at project level:

  1. Go to any project
  2. Go to Deploy → Container Registry
  3. If the registry page loads, it’s enabled

Step 2: Server Configuration (Admin)#

If the registry is turned off, configure it on the server.

For Omnibus GitLab (the all-in-one GitLab package), edit /etc/gitlab/gitlab.rb:

ruby
# Enable Container Registry
registry_external_url 'https://registry.gitlab.example.com'

# Or use the same domain with a different port
# registry_external_url 'https://gitlab.example.com:5050'

gitlab_rails['registry_enabled'] = true
gitlab_rails['registry_host'] = "registry.gitlab.example.com"
gitlab_rails['registry_port'] = "443"

Then reconfigure:

bash
sudo gitlab-ctl reconfigure

Step 3: Verify Registry Status#

bash
# Check registry status
sudo gitlab-ctl status registry

# Check registry configuration
sudo gitlab-rake gitlab:container_registry:check

Part 2: Creating a Centralized Docker Images Repository#

Why Centralize Base Images?#

A base image is the image that your builds and CI jobs start from, for example node. One project stores all shared base images. Every other project pulls from it.

  1. Without your own registry, every project pulls base images from Docker Hub. Docker Hub limits how many pulls you can make.
  2. Create one project, your-group/docker-images. Its Container Registry will store all shared base images.
  3. A CI job in that project pulls each image from Docker Hub once. It tags the image and pushes it to your registry. Examples: node:24.12.0-bookworm-slim, python:3.12-slim, golang:1.21-alpine.
  4. project-a, project-b and project-c now pull from your registry. The images come from your own network, and all projects use the same versions.
4 / 4

Benefits#

Aspect Centralized Per-Project
Storage One copy Copies in every project
Updates Update once Update each project
Consistency Always the same version Versions can become different
CI Speed Fast (cached) Slower

Step 1: Create the Project#

  1. Create a new project: your-group/docker-images
  2. Create it with a README
  3. Check that Container Registry is enabled for this project

Step 2: Set Up CI Pipeline for Image Uploads#

Create .gitlab-ci.yml in your docker-images project:

yaml
stages:
  - build

variables:
  DOCKER_TLS_CERTDIR: '/certs'

# Node.js 24 (Recommended for most projects)
push-node-24:
  stage: build
  image: docker:24-cli
  services:
    - docker:24-dind
  timeout: 30m
  before_script:
    - echo "Logging into GitLab Container Registry..."
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  script:
    - echo "Pulling node:24.12.0-bookworm-slim from Docker Hub..."
    - docker pull node:24.12.0-bookworm-slim

    - echo "Tagging for GitLab registry..."
    - docker tag node:24.12.0-bookworm-slim $CI_REGISTRY_IMAGE/node:24.12.0-bookworm-slim

    - echo "Pushing to GitLab Container Registry..."
    - docker push $CI_REGISTRY_IMAGE/node:24.12.0-bookworm-slim

    - echo "Image available at: $CI_REGISTRY_IMAGE/node:24.12.0-bookworm-slim"
  only:
    - main
  retry:
    max: 2
    when:
      - runner_system_failure
      - stuck_or_timeout_failure

# Add more images as needed
push-python-312:
  stage: build
  image: docker:24-cli
  services:
    - docker:24-dind
  timeout: 30m
  before_script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  script:
    - docker pull python:3.12-slim
    - docker tag python:3.12-slim $CI_REGISTRY_IMAGE/python:3.12-slim
    - docker push $CI_REGISTRY_IMAGE/python:3.12-slim
  only:
    - main

Step 3: Trigger the Pipeline#

bash
git add .gitlab-ci.yml
git commit -m "ci: add docker image push pipeline"
git push origin main

Watch the pipeline in the GitLab UI. The images appear in the Container Registry when the job finishes.

Part 3: Using Images from Your Registry#

In GitLab CI/CD Pipelines#

Use the full registry URL of the image:

yaml
# Use your centralized Node.js image
image: registry.gitlab.example.com/your-group/docker-images/node:24.12.0-bookworm-slim

stages:
  - build
  - test
  - deploy

build:
  stage: build
  script:
    - npm ci
    - npm run build

In Local Development#

bash
# Login to your GitLab registry
docker login registry.gitlab.example.com

# Pull an image
docker pull registry.gitlab.example.com/your-group/docker-images/node:24.12.0-bookworm-slim

# Use in docker run
docker run -it --rm \
  registry.gitlab.example.com/your-group/docker-images/node:24.12.0-bookworm-slim \
  node --version

In Dockerfiles#

dockerfile
FROM registry.gitlab.example.com/your-group/docker-images/node:24.12.0-bookworm-slim

WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

Part 4: Authentication Methods#

  1. Go to User Settings → Access Tokens
  2. Create a token with these scopes:
    • read_registry (pulling)
    • write_registry (pushing)
  3. Log in:
bash
docker login registry.gitlab.example.com -u your-username -p your-token

Method 2: Deploy Token (For Automation)#

  1. Go to Project Settings → Repository → Deploy tokens
  2. Create a token with the scopes you need
  3. Use it in CI or automation scripts

Method 3: CI/CD Variables (Automatic)#

GitLab CI provides these variables automatically:

Variable Description
$CI_REGISTRY Registry URL
$CI_REGISTRY_USER CI user for authentication
$CI_REGISTRY_PASSWORD CI password (token)
$CI_REGISTRY_IMAGE Full image path for the project
yaml
before_script:
  - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY

Part 5: Image Naming Best Practices#

Use Specific Version Tags#

bash
# Good - Specific and reproducible
node:24.12.0-bookworm-slim

# Avoid - Can change unexpectedly
node:24
node:latest

Include OS Variant#

bash
# Clear about the base OS
node:24.12.0-bookworm-slim    # Debian Bookworm (stable)
node:24.12.0-alpine3.21       # Alpine Linux
python:3.12-slim              # Minimal Debian

Image Variant Selection Guide#

Variant Size Use Case
bookworm-slim ~250 MB Default - Best compatibility
alpine ~130 MB Smallest size, some compatibility problems
bookworm (full) ~1.1 GB When you need build tools

Part 6: Special Considerations for Cloudflare Tunnel#

Pushes to the Docker Registry can fail behind Cloudflare Tunnel.

The Problem#

bash
docker push registry.gitlab.example.com/group/project/image:tag
# Push starts, then fails with "unauthorized: authentication required"

Symptoms:

  • Login succeeds
  • The push starts, and layers begin to upload
  • It fails in the middle of the transfer with an “unauthorized” error
  • The registry namespace is created but empty

Why This Happens#

Cloudflare Tunnel can cause:

  • Upload size limits (100MB on free plan)
  • Timeout limits on requests that run for a long time
  • Problems with chunked transfer encoding (uploads sent in pieces)

Solutions#

Runners on the internal network do not use the tunnel at all:

yaml
push-image:
  stage: build
  image: docker:24-cli
  services:
    - docker:24-dind
  script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker pull node:24.12.0-bookworm-slim
    - docker tag node:24.12.0-bookworm-slim $CI_REGISTRY_IMAGE/node:24.12.0-bookworm-slim
    - docker push $CI_REGISTRY_IMAGE/node:24.12.0-bookworm-slim

Solution 2: Push from Internal Network#

bash
# Use internal GitLab URL directly
docker login http://192.168.x.x:5050
docker tag image:tag 192.168.x.x:5050/group/project/image:tag
docker push 192.168.x.x:5050/group/project/image:tag

Solution 3: Configure Cloudflare Tunnel#

Add settings for the registry to your cloudflared config:

yaml
# config.yml
ingress:
  - hostname: registry.gitlab.example.com
    service: http://localhost:5050
    originRequest:
      noTLSVerify: true
      connectTimeout: 30s
      tcpKeepAlive: 30s
      noHappyEyeballs: true
  - service: http_status:404

Solution 4: Separate Registry Exposure#

Make the registry reachable through a different path:

  • Direct port forwarding (port 5050)
  • VPN for internal access
  • A separate subdomain with a different tunnel config

Part 7: Registry Management#

Viewing Images#

  1. Go to Deploy → Container Registry
  2. Browse images and tags
  3. View image details (size, layers, vulnerabilities)

Cleaning Up Old Images#

Manual Cleanup#

  1. Open Container Registry in the GitLab UI
  2. Select images/tags to delete
  3. Click the trash icon

Automatic Cleanup Policy#

  1. Settings → Packages and registries → Container Registry tag expiration policy
  2. Configure:
    • Keep tags matching: latest, v\d+\.\d+\.\d+
    • Remove tags older than 90 days
    • Keep most recent 10 tags

Storage Monitoring#

Check usage at Project Settings → General → Storage, or with the API:

bash
curl --header "PRIVATE-TOKEN: <token>" \
  "https://gitlab.example.com/api/v4/projects/<project-id>" | jq '.statistics'

Part 8: Troubleshooting#

Issue: “unauthorized: authentication required”#

During login:

  • Check the token scopes
  • Check the token expiry
  • Check the registry URL

During push (after successful login):

  • Check the storage quota
  • Check project permissions (Developer role or higher)
  • Try GitLab CI and not a manual push
  • If you use Cloudflare Tunnel, see Part 6

Issue: “manifest unknown”#

  • Check that the image tag exists
  • Check the spelling of the image name
  • Make sure you’re logged in

Issue: Slow pulls in CI#

  • Put runners on the same network as the registry
  • Check the runner cache config
  • Use Dependency Proxy for external images

Debugging Commands#

bash
# Check Docker credentials
cat ~/.docker/config.json | jq

# Test registry authentication
curl -u "username:token" https://registry.gitlab.example.com/v2/

# Check GitLab registry logs (server access required)
sudo gitlab-ctl tail registry

Part 9: Automated Image Updates#

Schedule Regular Updates#

  1. Go to CI/CD → Schedules
  2. Create a schedule: “Weekly base image updates”
  3. Set cron: 0 2 * * 0 (Sundays at 2 AM)
  4. Target branch: main

Update Workflow#

When new versions come out:

  1. Update the image tags in the CI pipeline
  2. Test in a feature branch
  3. Merge if the tests pass
  4. The pipeline pushes the new images automatically
yaml
# Example: Adding a new Node.js version
push-node-24-13:
  extends: .push-image-template
  variables:
    SOURCE_IMAGE: node:24.13.0-bookworm-slim
    TARGET_TAG: node:24.13.0-bookworm-slim

Conclusion#

GitLab Container Registry keeps Docker images on your own servers. Keep base images in one place to avoid Docker Hub rate limits, make pipelines faster, and use the same versions everywhere.

The main points:

  1. Use one central project for base images
  2. Push with CI pipelines, above all behind proxies
  3. Pin exact version tags so every build uses the same image
  4. Set up cleanup policies to manage storage
  5. Watch for Cloudflare Tunnel problems if you use it

References#