Friday, August 15, 2025

How to workwith gitlabe container registry?

 Here’s the step-by-step procedure for building a Docker image, pushing it to GitLab Container Registry, and then using it in a Kubernetes YAML.

1️⃣ Prepare GitLab for Container Registry

Make sure your GitLab project has Container Registry enabled.

In GitLab: Settings → General → Visibility, project features, permissions → enable Container Registry.

2️⃣ Log in to GitLab Container Registry

Get your GitLab credentials (username = GitLab username or CI_JOB_TOKEN in CI/CD, password = Personal Access Token or GitLab password).

Replace:

registry.gitlab.com with your GitLab registry host (usually registry.gitlab.com for SaaS)

NAMESPACE/PROJECT with your GitLab project path.

docker login registry.gitlab.com

Example:

docker login registry.gitlab.com

Username: your_gitlab_username

Password: your_access_token

3️⃣ Build Your Docker Image

In your local environment:

docker build -t registry.gitlab.com/<namespace>/<project>/<image-name>:<tag> .

Example:

docker build -t registry.gitlab.com/mygroup/myproject/webex-bot:latest .

4️⃣ Push Image to GitLab Registry

docker push registry.gitlab.com/<namespace>/<project>/<image-name>:<tag>

Example:

docker push registry.gitlab.com/mygroup/myproject/webex-bot:latest

You can now see the image in your GitLab project under Packages & Registries → Container Registry.

5️⃣ Use the Image in Kubernetes Deployment YAML

You’ll reference the full registry path in your Deployment manifest.

Example deployment.yaml:


apiVersion: apps/v1

kind: Deployment

metadata:

  name: webex-bot

spec:

  replicas: 2

  selector:

    matchLabels:

      app: webex-bot

  template:

    metadata:

      labels:

        app: webex-bot

    spec:

      containers:

      - name: webex-bot

        image: registry.gitlab.com/mygroup/myproject/webex-bot:latest

        ports:

        - containerPort: 8080

      imagePullSecrets:

      - name: gitlab-registry-secret




6️⃣ Create Kubernetes Image Pull Secret


Since GitLab registry requires authentication, create a pull secret in Kubernetes:


kubectl create secret docker-registry gitlab-registry-secret \

  --docker-server=registry.gitlab.com \

  --docker-username=your_gitlab_username \

  --docker-password=your_access_token \

  --docker-email=you@example.com


This secret matches the imagePullSecrets entry in your Deployment YAML.



7️⃣ Deploy to Kubernetes


kubectl apply -f deployment.yaml




✅ Final Flow Recap:

1. Enable Container Registry in GitLab.

2. Login to GitLab registry (docker login).

3. Build Docker image with GitLab registry path.

4. Push to GitLab registry.

5. Reference image in Kubernetes Deployment YAML.

6. Create image pull secret.

7. Deploy to Kubernetes.



references:

ChatGPT 



No comments:

Post a Comment