Introduction to Docker Integration in GitLab CI/CD Pipelines

Introduction to Docker Integration in GitLab CI/CD Pipelines

ยท

4 min read

Introduction

Brief Overview of Docker
Docker is a platform that allows developers to package applications and their dependencies into a standardized unit called a container. Containers are lightweight, portable, and can run on any environment that supports Docker, ensuring consistency across development, testing, and production environments.

Importance of Docker in DevOps
Docker plays a crucial role in DevOps by enabling continuous integration and continuous deployment (CI/CD). It helps streamline the software development lifecycle, reduce conflicts between different development environments, and improve scalability and resource utilization.

Getting Started with Docker

What is Docker?
Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all parts it needs, such as libraries and other dependencies, and ship it all out as one package.

Key Concepts

  • Images: Read-only templates used to create containers. They are built from Dockerfiles.

  • Containers: Running instances of Docker images. They are isolated from the host system and other containers.

  • Dockerfile: A text file that contains instructions for building a Docker image.

Setting Up Docker

Installing Docker
To install Docker, follow the instructions for your operating system on the Docker installation page.

Basic Docker Commands

  • docker run: Run a container from an image.

  • docker build: Build an image from a Dockerfile.

  • docker images: List all Docker images on your system.

  • docker ps: List all running containers.

Integrating Docker with GitLab CI/CD

Overview of CI/CD
CI/CD is a method to frequently deliver apps to customers by introducing automation into the stages of app development. The main concepts attributed to CI/CD are continuous integration, continuous deployment, and continuous delivery.

Benefits of Using Docker in CI/CD Pipelines

  • Consistency: Ensures the same environment in development, testing, and production.

  • Scalability: Easily scale applications horizontally.

  • Isolation: Each container runs in isolation, which reduces conflicts and improves security.

Step-by-Step Guide

Setting Up a GitLab Project

  1. Create a GitLab Account: Sign up at GitLab.

  2. Create a New Project:

    • Click on the "New Project" button.

    • Select "Create blank project".

    • Fill in the project name (e.g., DockerCIPipeline), description (optional), and set the visibility level.

    • Click "Create project".

Writing a Dockerfile

Create a file named Dockerfile in your project directory with the following content:

# Use an official Python runtime as a parent image
FROM python:3.8-slim

# Set the working directory
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

Creating a .gitlab-ci.yml File for Docker Integration

Create a .gitlab-ci.yml file in your project directory with the following content:

stages:
  - build
  - push

variables:
  DOCKER_DRIVER: overlay2
  IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG

build_image:
  stage: build
  image: docker:latest
  services:
    - docker:dind
  script:
    - docker build -t $IMAGE_TAG .
    - echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY
    - docker tag $IMAGE_TAG $CI_REGISTRY_IMAGE:latest

push_image:
  stage: push
  image: docker:latest
  services:
    - docker:dind
  script:
    - echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY
    - docker push $IMAGE_TAG
    - docker push $CI_REGISTRY_IMAGE:latest

Pushing Docker Images to GitLab's Container Registry

  1. Set Up CI/CD Variables:

    • Go to your GitLab project.

    • Navigate to Settings > CI/CD > Variables.

    • Add the following variables:

      • CI_REGISTRY: Your GitLab container registry URL.

      • CI_REGISTRY_USER: Your GitLab username.

      • CI_REGISTRY_PASSWORD: Your GitLab access token (create one from your GitLab profile settings).

  2. Commit and Push Changes:

   git add Dockerfile .gitlab-ci.yml
   git commit -m "Add Dockerfile and CI/CD pipeline configuration"
   git push origin main

Monitoring and Verification

Monitor the Pipeline:

  1. Go to your GitLab project page.

  2. Navigate to CI/CD > Pipelines.

  3. You should see a new pipeline triggered by your recent push.

  4. Click on the pipeline to monitor its progress and view job logs.

Verify Docker Image:

  1. After the pipeline completes, go to Packages & Registries > Container Registry in your GitLab project.

  2. You should see your Docker image listed there.

Conclusion

Recap of Key Points:

  • Docker containers offer a consistent, isolated environment for your applications.

  • GitLab CI/CD pipelines can be configured to build and push Docker images automatically.

  • Monitoring and verifying pipelines and Docker images ensure a smooth CI/CD process.

Next Steps:

  • Explore advanced Docker features like multi-stage builds.

  • Integrate more complex testing frameworks.

  • Experiment with deploying Docker containers to cloud platforms.

Resources

ย