Docker - GitLab CI/CD


Introduction

Integrating Docker with GitLab CI/CD enables seamless automation of the build, test, and deployment processes for containerized applications. By using Docker in your GitLab pipelines, you can create a consistent and repeatable workflow that enhances the efficiency and reliability of your software delivery pipeline. This tutorial covers the essentials of Docker and GitLab CI/CD integration, including setup, pipeline creation, and best practices for effective CI/CD.


1. All Steps

Here's a quick overview of the steps involved in integrating Docker with GitLab CI/CD:


2. Setting Up GitLab Runner with Docker

GitLab Runners execute the jobs defined in your CI/CD pipelines. You can set up a GitLab Runner using Docker to provide a clean and isolated environment for each job:

Installing Docker

Ensure that Docker is installed and running on your host system. Follow the official Docker installation guide for your operating system.

Running GitLab Runner in a Docker Container

Pull the official GitLab Runner image and run it in a Docker container:

# Pull the GitLab Runner Docker image
docker pull gitlab/gitlab-runner:latest

# Run GitLab Runner container
docker run -d --name gitlab-runner --restart always \
  -v /srv/gitlab-runner/config:/etc/gitlab-runner \
  -v /var/run/docker.sock:/var/run/docker.sock \
  gitlab/gitlab-runner:latest

3. Registering GitLab Runner

Register the GitLab Runner with your GitLab instance to enable it to pick up jobs from your project:

# Register the runner
docker run --rm -it -v /srv/gitlab-runner/config:/etc/gitlab-runner \
  gitlab/gitlab-runner:latest register

4. Creating a GitLab CI/CD Pipeline

GitLab CI/CD pipelines are defined using a .gitlab-ci.yml file in your project's root directory. Here's how to create a simple CI/CD pipeline that builds and deploys a Dockerized application:

stages:
  - build
  - test
  - deploy

variables:
  DOCKER_DRIVER: overlay2

before_script:
  - docker login -u "$CI_REGISTRY_USER" -p "$CI_JOB_TOKEN" "$CI_REGISTRY"

build:
  stage: build
  script:
    - docker build -t "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA" .
    - docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA"

test:
  stage: test
  script:
    - docker run --rm "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA" npm test

deploy:
  stage: deploy
  script:
    - docker pull "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA"
    - docker run -d -p 80:80 "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA"

This pipeline consists of three stages: build, test, and deploy. It builds a Docker image, runs tests inside a container, and deploys the image.


5. Using Docker-in-Docker (DinD)

Docker-in-Docker allows you to run Docker commands within a Docker container. This setup is useful for building Docker images in CI/CD pipelines:

stages:
  - build

build:
  stage: build
  image: docker:latest
  services:
    - docker:dind
  variables:
    DOCKER_DRIVER: overlay2
  script:
    - docker build -t myapp .
    - docker run myapp

This example shows how to use DinD in a GitLab CI/CD pipeline to build and run a Docker image.


6. Managing Secrets and Environment Variables

Securely manage secrets and environment variables in GitLab CI/CD to protect sensitive information such as API keys and credentials:

Using GitLab CI/CD Variables

Define environment variables in the "Settings" > "CI/CD" > "Variables" section of your GitLab project. Access them in your pipeline using the $VARIABLE_NAME syntax.

variables:
  MY_SECRET: "$MY_SECRET"

7. Deploying to Kubernetes with GitLab CI/CD

GitLab CI/CD can be used to deploy applications to Kubernetes clusters, enabling automated deployment and scaling:

stages:
  - deploy

deploy:
  stage: deploy
  image: bitnami/kubectl
  script:
    - kubectl config set-cluster my-cluster --server=https://my-cluster:6443
    - kubectl config set-credentials my-user --token="$KUBE_TOKEN"
    - kubectl config set-context my-context --cluster=my-cluster --user=my-user
    - kubectl config use-context my-context
    - kubectl apply -f deployment.yaml

This example demonstrates how to deploy a Dockerized application to a Kubernetes cluster using GitLab CI/CD.


8. Integrating Docker Compose with GitLab CI/CD

Docker Compose can be integrated into GitLab CI/CD pipelines to manage multi-container applications:

stages:
  - build
  - deploy

build:
  stage: build
  script:
    - docker-compose -f docker-compose.yml build

deploy:
  stage: deploy
  script:
    - docker-compose -f docker-compose.yml up -d

This pipeline builds and deploys a Docker Compose application using GitLab CI/CD.


9. Monitoring and Logging with GitLab CI/CD

GitLab CI/CD provides monitoring and logging capabilities to track pipeline execution and diagnose issues:


10. Best Practices for Docker and GitLab CI/CD Integration

Follow these best practices to optimize Docker and GitLab CI/CD integration for efficient and reliable pipelines:


11. Summary

Docker and GitLab CI/CD integration provides a powerful solution for automating the build, test, and deployment processes for containerized applications. By leveraging the capabilities of both tools and following best practices, you can create efficient and reliable CI/CD pipelines that enhance your software delivery workflow.