Monday, September 9, 2024

GitLab CI/CD: Building and Pushing Docker Images to GitLab Container Registry

 Introduction

In the contemporary software development landscape, automation is crucial for efficiency and speed. GitLab offers a robust CI/CD framework that simplifies building and deploying applications. This guide will walk you through the process of setting up GitLab CI/CD pipelines to automate the building of Docker images and their subsequent upload to the GitLab Container Registry.






Why Use GitLab for CI/CD?

GitLab integrates source control, CI/CD pipelines, and container storage into a single platform, streamlining your development workflow. The GitLab Container Registry allows you to manage Docker images alongside your code, making the transition from development to production seamless and efficient.

Prerequisites

Before starting, ensure you have:

  • A GitLab account and a repository set up
  • Basic familiarity with Docker and Dockerfiles
  • A GitLab Runner properly configured
  • Docker installed on your local machine

Step 1: Enable the GitLab Container Registry

First, ensure that the GitLab Container Registry is activated for your project:

  1. Go to your GitLab project.
  2. Navigate to Settings > Packages & Registries > Container Registry.
  3. Verify that the Container Registry is enabled.

Step 2: Create a Dockerfile

Dockerfile defines the environment and steps for building your Docker image. Here’s a sample Dockerfile:

# Use a lightweight Python image as the base
FROM python:3.9-slim

# Set the working directory in the container
WORKDIR /app

# Copy application files into the container
COPY . /app

# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Expose port 80 for the application
EXPOSE 80

# Command to run the application
CMD ["python", "app.py"]

Step 3: Configure GitLab CI/CD with .gitlab-ci.yml

Create a .gitlab-ci.yml file at the root of your project. This file outlines the steps for building and pushing your Docker image:

# Use Docker image for CI/CD jobs
image: docker:20.10.7

services:
- docker:20.10.7-dind

variables:
DOCKER_DRIVER: overlay2
DOCKER_TLS_CERTDIR: "/certs"
IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG

before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY

stages:
- build
- push

build_image:
stage: build
script:
- docker build -t $IMAGE_TAG .
only:
- main

push_image:
stage: push
script:
- docker push $IMAGE_TAG
only:
- main

Step 4: Set Environment Variables

GitLab automatically provides certain environment variables like $CI_REGISTRY$CI_REGISTRY_USER, and $CI_REGISTRY_PASSWORD for authentication. You can add custom variables in Settings > CI/CD > Variables in your project settings.

Step 5: Run the Pipeline

Once you commit the .gitlab-ci.yml file, GitLab will initiate the pipeline. You can track the pipeline’s progress under CI/CD > Pipelines in your project dashboard.

Step 6: Access Your Docker Images

To view and manage your Docker images:

  1. Go to Packages & Registries > Container Registry in your GitLab project.
  2. You will see your images and their tags.
  3. To pull an image, use the following Docker command:
docker pull registry.gitlab.com/<your-project-path>/<image-name>:<tag>

Explore more detailed content and step-by-step guides on our YouTube channel:-

Conclusion

Utilizing GitLab CI/CD for building and pushing Docker images automates and simplifies your deployment process. This guide provides a basic setup to get you started, but you can further customize the pipeline to include additional steps like testing or deployment to production.

Connect with Me:

No comments:

Post a Comment

Wireless Security Configuration: Protect Your Network Now!

Introduction: In today’s connected world, wireless networks are as common as smartphones, and they’re often the gateway to our personal, pr...