This repository contains a docker-compose
configuration for setting up a GitLab Runner with Docker-in-Docker (DinD). This allows for CI/CD pipelines that need to build, run, and manage Docker containers within GitLab CI.
- Docker installed on your host machine
- Docker Compose installed on your host machine
- GitLab account with access to a GitLab instance
- Clone the repository:
git clone https://github.com/yourusername/your-repo.git
cd your-repo
- Configure environment variables:
Create a .env file in the root of the repository and populate it with the following variables:
CI_SERVER_URL=https://gitlab.example.com/ # Replace with your GitLab instance URL
REGISTRATION_TOKEN=your_registration_token # Replace with your GitLab Runner registration token
RUNNER_NAME=your_runner_name # Replace with your desired runner name
- Start the services:
docker-compose up -d
- dind: Docker-in-Docker service to provide Docker daemon capabilities inside a container.
- runner: GitLab Runner service configured to use the DinD service for Docker operations.
- register-runner: One-time service to register the GitLab Runner with the GitLab instance.
After starting the services with docker-compose up -d, the GitLab Runner will be registered and ready to pick up jobs from your GitLab CI pipelines. Ensure your GitLab CI/CD configuration (.gitlab-ci.yml) is set up to use the runner for executing jobs.
Here is an example .gitlab-ci.yml file that installs Docker Compose before running a script:
stages:
- build
build:
stage: build
services:
# Define the Docker-in-Docker service to enable Docker commands within the job.
- name: docker:dind
before_script:
# Install pip, the package installer for Python
- apk add --no-cache py3-pip
# Install Docker Compose using pip
- pip install docker-compose
# Verify Docker Compose installation
- docker-compose --version
script:
# Start services defined in the docker-compose.yml file in detached mode
- docker-compose up -d
# Run a specific service defined in the docker-compose.yml file
- docker-compose run your-service-name
# Shut down all services defined in the docker-compose.yml file
- docker-compose down
Note: Installing Docker Compose is crucial for managing multi-container Docker applications and running the services defined in the .gitlab-ci.yml file example.
To stop the services, run:
docker-compose down
This will stop and remove all the containers defined in the docker-compose.yml file.