Installing GitLab in Docker with Sudo

Intended Audience

  • Users interested in installing GitLab via Docker Compose
  • Users who use sudo with Docker, rather than running Docker commands as root

Summary

Environment variables aren’t passed to Docker when you issue a command like “sudo docker compose up -d”. The GitLab installation specifies that you should use an environment variable ($GITLAB_HOME) for installation, rather than defining the path directly.

If you have followed the GitLab instructions (https://docs.gitlab.com/ee/install/docker.html), you will issue the below command as part of the “Set up the volumes location” section, which sets the environment variable:

export GITLAB_HOME=/srv/gitlab

This variable isn’t going to be passed to Docker though when you try to bring up your containers using Compose.

The Issue

When you attempt to bring up your Compose file, you receive the following error:

> sudo docker compose up -d
WARNING: The GITLAB_HOME variable is not set. Defaulting to a blank string.

The problem was that your environment variable previously set wasn’t passed to Docker when you used sudo.

Option 1 – Pass the “-E” option

Sudo has an option to pass “-E” or “–preserve-env” which will retain the environment variables that you set. Therefore, to bring up your Docker Compose file, you can issue the command:

sudo -E docker compose up -d

This will ensure the variable GITLAB_HOME is correctly passed to Compose.

Option 2 – Permanently add GITLAB_HOME variable

First, you’ll need to modify your user profile. Use any text editor to modify ~/.profile. For example:

Add the following line to the bottom of the profile file, substituting where you would like the GitLab files to live. I chose /srv/:

export GITLAB_HOME=/srv/gitlab

Now modify the sudoers file so this variable is always passed when using sudo:

Add the following lines to the file:

# This preserves the GITLAB_HOME variable for Docker
Defaults  env_keep += "GITLAB_HOME"

That’s it. Now you can use sudo for Docker Compose without having to remember to pass the -E option.

Leave a Comment