Set up Gitlab instance with Docker Compose
Set up Gitlab instance with Docker Compose

There are many ways to set up a GitLab instance, but using Docker Compose is one of the easiest and most efficient methods, especially for local development or small-scale deployments.
This guide will walk you through the steps to set up a GitLab instance using Docker Compose.
Prerequisites
Install Docker and Docker Compose
To install Docker, follow the instructions on the official Docker website: https://docs.docker.com/get-docker/
To install Docker Compose, follow the instructions on the official Docker Compose website: https://docs.docker.com/compose/install/
Create directories for GitLab data
GitLab requires persistent storage for its configuration, logs, and data. These will be mounted as volumes in the Docker container.
Create the necessary directories on your host machine:
mkdir ~/gitlab
cd ~/gitlab
mkdir config logs data
Create a Docker Compose file
Create a directory for your GitLab instance and navigate into it:
mkdir gitlab-docker
cd gitlab-docker
Create a file named docker-compose.yml
touch docker-compose.yml
Open the docker-compose.yml file in your editor and add the following content:
services:
gitlab:
image: gitlab/gitlab-ce:latest
container_name: gitlab
restart: always
hostname: 'gitlab'
environment:
GITLAB_OMNIBUS_CONFIG: |
external_url 'http://gitlab'
ports:
- '80:80'
- '443:443'
- '22:22'
volumes:
- ~/gitlab/config:/etc/gitlab
- ~/gitlab/logs:/var/log/gitlab
- ~/gitlab/data:/var/opt/gitlab
Let's break down the content of the docker-compose.yml file:
- We'll use the gitlab community edition image
gitlab/gitlab-ce:latest. - The
container_nameis set togitlab, which will be the name of the Docker container. - The
restart: alwaysoption ensures that the container restarts automatically if it stops or if the Docker daemon restarts. - The
hostnameis set togitlab, which will be used in the URL to access the GitLab instance. - The
environmentsection allows you to set the external URL for GitLab. This is the URL you will use to access your GitLab instance. - The
portssection maps the container's ports to the host machine's ports. Port 80 is for HTTP, port 443 is for HTTPS, and port 22 is for SSH. - The
volumessection mounts the host directories we created earlier to the appropriate directories in the container, ensuring that your GitLab data persists even if the container is removed.
Start GitLab
To start your GitLab instance, run the following command in the directory where your docker-compose.yml file is located:
docker-compose up -d
This command will start the GitLab container in detached mode (-d), allowing it to run in the background.
Access GitLab
Once the container is up and running you can access the gitalb instance by navigating to http://gitlab in your web browser.
Give the container a few minutes to initialize. You will get a login page and to get started you will need a root user password to log in.
Get the root password
Gitlab creates a password for the root user on initialization. You can use this password to login to your instance.
To get the password run the following commands:
docker exec -it gitlab
cat /etc/gitlab/initial_root_password
Copy the password displayed and use it to log in to your GitLab instance, with the username root.
At this point you can successfully access your GitLab instance and start using it.
Stop GitLab
To stop your GitLab instance, run the following command in the directory where your docker-compose.yml file is located:
docker-compose down
This command will stop and remove the GitLab container, but your data will remain intact in the mounted volumes.
Restart GitLab
To restart your GitLab instance, run the following command in the directory where your docker-compose.yml file is located:
docker-compose restart
This command will stop and then start the GitLab container again, allowing you to apply any configuration changes or updates.
Conclusion
You have successfully set up a GitLab instance using Docker Compose. This setup allows you to easily manage your GitLab instance, including starting, stopping, and restarting it as needed. You can now create projects, manage users, and utilize all the features GitLab has to offer.



