How to Deploy GitLab in Docker with Ansible: A Complete Guide

Setting up a self-hosted GitLab instance is like building your own digital workshop for coding, collaboration, and CI/CD pipelines. Using Ansible to automate the deployment of GitLab in a Docker container simplifies the process, saving time and ensuring consistency. This guide walks you through the steps to configure a secure, fully functional GitLab server with SSH, HTTP/HTTPS, and SMTP integration. Let’s get your development hub up and running effortlessly!

Why Use GitLab with Ansible and Docker?

GitLab is a powerful platform for version control, CI/CD, and team collaboration, rivaling tools like GitHub. Running it in Docker ensures a lightweight, portable environment, while Ansible automates the setup, from server configuration to firewall rules. Think of it as hiring a skilled crew to build your development environment with precision. This setup is perfect for teams needing a secure, scalable solution for their projects.

Prerequisites for a Seamless Deployment

Before you start, ensure you have:

  • Ansible 2.9+ installed on your control node.
  • SSH access to a Linux server (e.g., Ubuntu, CentOS) with internet access.
  • A domain name or IP address for accessing GitLab.
  • An SMTP server for email notifications (optional but recommended).

Quick Tip: Test SSH connectivity to your server to avoid delays during deployment.

Step-by-Step Guide to Deploy GitLab

Follow these steps to automate your GitLab setup using Ansible and Docker, as outlined in the GitLab Ansible repository.

1. Clone the Ansible Repository

Begin by downloading the playbook from GitHub:

git clone https://github.com/Shipssv83/Ansible-GitLab-Docker.git
cd Ansible-GitLab-Docker

This repository contains all the playbooks and roles needed for a complete GitLab setup.

2. Install Ansible Galaxy Roles

Install the required roles to support your playbooks:

ansible-galaxy install -r roles/requirements.yml

To update existing roles, run:

ansible-galaxy install -g -f -r roles/requirements.yml

These roles handle server setup, Docker installation, and GitLab configuration.

3. Configure the Inventory File

Create an inventory file to specify your target server:

vim inventory

Add your server details, for example:

[servers]
192.168.1.100 ansible_user=root

Replace 192.168.1.100 with your server’s IP and root with your SSH user.

4. Customize Playbook Variables

Tailor the deployment by editing key variables in your playbook:

vars:
  timezone: "Europe/Warsaw"
  fqdn: "gitlab.example.com"
  gitlab_ssh_host_name: "ssh.example.com"
  gitlab_root_password: "secure_password"
  ssh_port_gitlab: "22"
  http_port: "80"
  https_port: "443"
  ssh_port: "2222"
  gitlab_docker_version: "17.8.2-ce.0"
  git_pos_user: "gitlab_user"
  git_pos_pass: "db_password"
  git_pos_db: "gitlab"
  smtp: "smtp.example.com"
  smtp_port: "587"
  smtp_domain: "example.com"
  registration_token: "your_registration_token"

What do these do?

  • fqdn: Sets the domain for GitLab access (e.g., gitlab.example.com).
  • gitlab_root_password: Defines the admin password for GitLab.
  • smtp and smtp_port: Configure email notifications.
  • gitlab_docker_version: Specifies the GitLab Docker image version.

5. Configure the Server Environment

Run the server setup playbook to prepare the environment:

ansible-playbook -i inventory --user root --extra-vars "host=servers" playbooks/server-install.yml

This playbook installs essential packages and sets the server’s timezone.

6. Install Docker and Docker Compose

Deploy Docker with this playbook:

ansible-playbook -i inventory --user root --extra-vars "host=servers" playbooks/docker-install.yml

This sets up Docker and Docker Compose, creating a foundation for running GitLab.

7. Deploy GitLab in Docker

Launch GitLab with the final playbook:

ansible-playbook -i inventory --user root --extra-vars "host=servers" playbooks/gitlab-docker-install.yml

This playbook:

  • Pulls the GitLab Docker image (version 17.8.2-ce.0 by default).
  • Deploys GitLab with configured ports (SSH, HTTP, HTTPS).
  • Sets up SMTP for email notifications.
  • Configures UFW firewall rules for secure access.

8. Configure the Firewall

The playbook automatically runs firewall-gitlab.yml to open necessary ports (22, 80, 443, 2222). Verify with:

ufw status

Accessing and Setting Up GitLab

Once deployed:

  1. Access GitLab at http://gitlab.example.com or https://gitlab.example.com.
  2. Log in with the root user and your gitlab_root_password.
  3. Create repositories, add users, and configure CI/CD pipelines.
  4. Register a GitLab Runner using the registration_token for automated builds.

Pro Tip: Enable HTTPS with a reverse proxy like Nginx and obtain an SSL certificate (e.g., via Let’s Encrypt) for secure access.

Benefits of This Setup

This deployment delivers:

  • Automation: Ansible eliminates manual configuration errors.
  • Scalability: Docker makes it easy to scale or replicate your GitLab instance.
  • Security: UFW and SSH configurations protect your server.
  • Flexibility: Customize GitLab for your team’s workflows.

It’s like setting up a custom-built coding studio that’s ready to scale with your projects.

Troubleshooting Tips

If you encounter issues:

  • Check Playbook Logs: Review Ansible output for errors.
  • Verify Docker Containers: Use docker ps to confirm GitLab is running.
  • Inspect Firewall Rules: Ensure ports 80, 443, and 22 (or 2222) are open.
  • Test SMTP Settings: Confirm your SMTP server and credentials are correct.

Conclusion: Power Your Development with GitLab

With GitLab, Ansible, and Docker, you’ve built a secure, automated platform for version control and CI/CD. Your team can now collaborate, manage code, and automate workflows with ease. Want to optimize your DevOps setup further? Visit blog.1it.pro for more IT insights or share your GitLab success on 1it.pro!

🚀 Explore more guides on our blog 👉 blog.1it.pro
📧 Contact us: admin@1it.pro for expert IT guidance.
🌐 Explore more: Visit 1it.pro for top-tier IT solutions.

UA EN RU

Зв'язатися з нами

Telegram Email