Dockerized GitLab: How to Easily Set Up Your Own GitLab Server

Lal Zada
6 min readAug 31, 2024

--

Source: https://unsplash.com/es/fotos/buque-de-carga-rojo-y-azul-en-el-mar-durante-el-dia-HjBOmBPbi9k

In this article I’ll show you how to set up your own GitLab inside a Docker container. You need to first find docker image for the GitLab Community Edition on https://hub.docker.com

This is the Community Edition and we will be working with it but if you are going to set up the Enterprise Edition, the docker commands and steps will remain the same.

Download GitLab Docker Image

First copy the docker pull command from the docker hub

docker pull gitlab/gitlab-ce

Then open your favorite terminal and run the above command which will look like this.

This will download the GitLab docker image from the docker hub. It could take around 5~10 minutes depending on your network speed. The docker image size is 1.25 GB for the latest version.

Once the docker pull command runs successfully, you should have the GitLab docker image on your local machine by inspecting using this command.

docker image ls | grep gitlab-ce

Run GitLab Container from Docker Image

Now lets run the GitLab server by creating a container from the downloaded docker image i.e gitlab/gitlab-ce

docker run -p 8000:80 gitlab/gitlab-ce

The GitLab server inside the docker container is set up to run on the port 80 by default. If you need to access the GitLab server on your host machine from within the running container, you need to do the port mapping from host machine to the container by using -p 8000:80.

On the host side, we are setting port to 8000. On the container side, its port 80

Once you run the above docker run command, this could take around 5 minutes to set up the GitLab server inside the docker container.

We will wait for five minutes and then we will see how it goes.

When you try to visit http://localhost:8000 after around 5 minutes, you could see a 502 response . This means that the container is running. Everything is fine with the container but the GitLab server inside that container is not ready yet to accept requests.

When you retry after a minute, you can see that the GitLab server is loaded with the login screen.

The default username is root which is set by default. To get the password you need to run a simple docker command inside the running GitLab container.

First get the details of the running container using the following command.

docker ps | grep gitlab-ce

Copy the container ID 6befb2ecb255 and open a new terminal and run this command.

docker exec -it 6befb2ecb255 cat /etc/gitlab/initial_root_password

This will run cat /etc/gitlab/initial_root_password inside the GitLab container which we are referring to by using its unique ID 6befb2ecb255.

The output should look like this.

You should see a long random password as highlighted in this screenshot.

Now you can login to your GitLab server running on https://localhost:8000 by using these credentials

user: root
password: GvFenAjjvPp0i01/573UUQsQkMyDWNL50AXYMVjG70k= # replace your password

After login, you should be able to view GitLab Dashboard.

Now try creating some repositories.

Stop the GitLab container by running this command

docker stop {CONTAINER_ID}

And then restart this same GitLab container by running

docker start --attach {CONTAINER_ID}

You should be able to see all your created repositories are there in your GitLab server which you created before stopping and restarting the container. Sounds Good!

What if you delete the GitLab container while you have some important data already there?

Try deleting your GitLab container by running these commands

Stop container

docker stop {CONTAINER_ID}

Delete container

docker rm {CONTAINER_ID}

Now re-run the following command to start the GitLab server

docker run -p 8000:80 gitlab/gitlab-ce

Once the GitLab server is loaded, visit it at http://localhost:8000.

First, the credentials will not work as the password will be regenerated and you need to fetch it again the same way we tried in the beginning of this article.

Second, when you login with your new password, you’ll not see any previously created respositories.

Why?

Container data is not persistent. The moment you delete the GitLab container, all its data was deleted alongside the container including GitLab credentials and repositories.

ohh man…

What is the solution?

docker volumes

If you want to be safe in case of any disaster or for any reason the container may be deleted, you need to start your GitLab container by attaching a docker volume where the docker volume will permanently save the data from the GitLab container.

Stop your running container, delete it and retry using the following command.

docker run -p 8000:80 \
-v ./gitlab/config:/etc/gitlab \
-v ./gitlab/data:/var/opt/gitlab \
gitlab/gitlab-ce

-v flag is used to specify the volume using a source and destination path where source path will be on the host machine and destination path will be from the container.

In this case, i am trying to persist the /etc/gitlab from the GitLab container into my host machine in the folder ./gitlab/config

/etc/gitlab contains initial root password and some other secrets and certs.

I am also trying to persist the /var/opt/gitlab from the GitLab container into my host machine in the folder ./gitlab/data

/var/opt/gitlab contains all data including repositories, caches, registry etc.

After starting the GitLab container by attaching volume, everything will be persistent regardless of container restarts, terminates or deleted.

In the next post, i am going to transition all these barebone docker commands into a Docker compose file and so we can easily manage our containers using docker compose CLI instead of running all these commands manually.

Source Code

Watch it on YouTube

Thank you for making it till the end 🎉

If this article helped you, make sure to:

--

--

Lal Zada

Tech article every week - A software engineer over a decade experience in building apps, infrastructure and CI/CDs