3

I'm working on setting GitLab CE in a Docker container using Docker Compose. My OS is Ubuntu Server 24.04.

As part of the installation, I defined the shell variable $GITLAB_HOME in my .bash_profile. However, when I run docker-compose, GITLAB_HOME is not defined. What do I need to fix that? Output below.

dthacker@gitlab-docker:~/compose-lab$ echo $GITLAB_HOME
/srv/gitlab
dthacker@gitlab-docker:~/compose-lab$ sudo docker compose up -d
WARN[0000] The "GITLAB_HOME" variable is not set. Defaulting to a blank string. 
WARN[0000] The "GITLAB_HOME" variable is not set. Defaulting to a blank string. 
WARN[0000] The "GITLAB_HOME" variable is not set. Defaulting to a blank string. 

and my docker_compost.yml file

    image: gitlab/gitlab-ce:17.4.3-ce.0
    container_name: gitlab
    restart: always
    hostname: 'gitlab.example.com'
    environment:
      GITLAB_OMNIBUS_CONFIG: |
        # Add any other gitlab.rb configuration here, each on its own line
        external_url 'https://gitlab.example.com'
    ports:
      - '80:80'
      - '443:443'
      - '22:22'
    volumes:
      - '$GITLAB_HOME/config:/etc/gitlab'
      - '$GITLAB_HOME/logs:/var/log/gitlab'
      - '$GITLAB_HOME/data:/var/opt/gitlab'
    shm_size: '256m'
smac89
  • 1,085

1 Answers1

6

As much as the comments seem to have resolved the issue, I would caution against using implicitly defined environment variables for such things.

The docker compose command can take a --env-file parameter. I would suggest you rely on that more than modifying sudoers or using the --preserve-env argument of the sudo command. The --env-file parameter allows you to be explicit about the variables that your compose command relies on.

Using this method, makes your compose file more robust and portable.


docker.env

GITLAB_HOME=/srv/gitlab

Then your command would be:

sudo docker compose --env-file ./docker.env up -d

Why does sudo hide environment variables?

To address your main concern, the reason why docker compose seems to ignore your environment variables is because of the policies which control the behaviour of the sudo command. Without going into too much detail, the main policy which controls how sudo handles environment variables is called the sudoers policy. This is also the default policy. Within the sudoers policy, is a flag called env_reset, which controls whether or not environment variables are preserved. This flag is enabled by default.

The result of this combination of default flag and policy is that commands which are executed with sudo do not inherit all the environment variables which were present when sudo was invoked.

Quoting from the docs:

By default, the env_reset flag is enabled. This causes commands to be executed with a new, minimal environment. On AIX (and Linux systems without PAM), the environment is initialized with the contents of the /etc/environment file. The HOME, MAIL, SHELL, LOGNAME and USER environment variables are initialized based on the target user and the SUDO_* variables are set based on the invoking user. Additional variables, such as DISPLAY, PATH and TERM, are preserved from the invoking user's environment if permitted by the env_check or env_keep options.

smac89
  • 1,085