Introduction: In the rapidly evolving world of DevOps, effectively managing source code is essential. GitLab stands out as a powerful and adaptable platform for source code management, offering a comprehensive solution for teams that want to streamline their source control, continuous integration, and collaboration processes. In this blog, we'll walk you through the steps to set up your own GitLab self-managed instance on an AWS EC2 instance using Docker Compose. This method provides flexibility, scalability, and easy management, allowing DevOps teams to concentrate on what truly matters: maintaining high code quality and fostering collaboration.

Why Choose GitLab? GitLab is an all-encompassing platform for source code management, enabling teams to collaborate efficiently, manage repositories, track issues, and automate CI/CD pipelines. Hosting your own GitLab instance gives you complete control over your development environment, ensuring data privacy and smooth integration with your existing infrastructure.

The Benefits of Docker Compose: Docker Compose is a tool designed for defining and managing multi-container Docker applications, which simplifies the deployment of complex systems like GitLab. With Docker Compose, you can define your application's services, networks, and storage in a single docker-compose.yml file, making the setup process straightforward and repeatable.

Prerequisites

Before we dive in, make sure you have the following:

  • AWS Account: You need an AWS account to create an EC2 instance.
  • EC2 Instance: Launch an EC2 instance with suitable configurations.
  • Domain Name (Optional): If you want to access GitLab using a custom domain, configure DNS settings accordingly.
  • SSH Key Pair: For secure access to your EC2 instance.
  • Basic Understanding: Familiarity with AWS, Docker, and GitLab basics.

Step 1: Launch your EC2 Instance

You can read about launching an EC2 instance here.

Step 2: SSH into Your EC2 Instance

You can remotely access your EC2 instance using any SSH client; I find MobaXterm particularly useful for managing the instance smoothly.

Before we start setting up GitLab, let's make sure Docker and Docker Compose are installed on your AWS EC2 instance.

  1. Installing Docker:
sudo yum update -y
sudo amazon-linux-extras install docker
sudo systemctl start docker
sudo usermod -aG docker ec2-user
sudo systemctl enable docker

The above commands update the system, install Docker, add the current user to the Docker group, start the Docker service, and enable it to start on system boot in an Amazon Linux environment.

2. Installing Docker Compose:

sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Step 3: Creating Docker Compose File for GitLab

In this step, we’ll create a Docker Compose file for GitLab. First, let’s set up a separate directory named gitlab to organize our project.

  1. Create a new directory:
mkdir gitlab
cd gitlab

2. Create docker-compose.yml File:

Inside the gitlab directory, create a file named docker-compose.yml using your favorite text editor. You can use nanovim, or any other editor you prefer.

sudo nano docker-compose.yml

3. Add GitLab Configuration to docker-compose.yml:

Copy and paste the following configuration into the docker-compose.yml file. Replace the hostname with your domain or public IP.

version: '3.8'

services:
  web:
    image: 'gitlab/gitlab-ce:latest'
    restart: always
    hostname: '52.49.119.200'
    environment:
      GITLAB_OMNIBUS_CONFIG: |
        external_url 'http://52.49.119.200'
    ports:
      - '80:80'
    volumes:
      - '/srv/gitlab/config:/etc/gitlab'
      - '/srv/gitlab/logs:/var/log/gitlab'
      - '/srv/gitlab/data:/var/opt/gitlab'
    networks:
      - gitlab

networks:
  gitlab:
    name: gitlab-network

Save and exit the editor (in Nano, you can do this by pressing CTRL + O, then Enter to save, and CTRL + X to exit).

Step 4: Running Docker Compose

Navigate to the directory where your docker-compose.yml file is located (in this case, the gitlab directory) and execute the following command:

docker-compose up -d

Step 5: Access GitLab via Web Browser:

Open a web browser and enter the following URL, replacing <YOUR_EC2_PUBLIC_IP> with your actual EC2 instance's public IP address:

Before accessing the GitLab web interface, you are required an admin password to login to the GitLab dashboard. You retrieve the GitLab login password with the following command.

docker exec -it gitlab-web-1 grep 'Password:' /etc/gitlab/initial_root_password

Now, open your web browser and access the GitLab web interface using the URL http://your-server-ip. You will be redirected to the GitLab login page.

Provide your username (root), and password (the one you got above) and click on the Sign in button. Once you are logged in, you are redirected to the GitLab dashboard as shown below.

The link has been copied!