docker icon indicating copy to clipboard operation
docker copied to clipboard

.env file is unable to use environment variables declared within itself

Open Southpaw1496 opened this issue 2 years ago • 7 comments

I recently started using Mattermost with Docker. As instructed in the Deployment Guide, I cloned the repository, copied the env.example file to a .env file, filled out the environment variables and started the server. However, it didn't start properly and after viewing the logs with docker logs, the error turned out to be

Error: failed to load configuration: failed to create store: unable to load on store creation: invalid config: Config.IsValid: model.config.is_valid.site_url.app_error, 
Usage:
  mattermost [flags]
  mattermost [command]

After some thought, I noticed that MM_SETTINGS_SITEURL (line 38) was using https://${DOMAIN} as its value, so I hardcoded my domain into it instead of using the environment variable. After that, I received this error:

{"timestamp":"2022-04-24 15:34:38.346 Z","level":"error","msg":"Failed to ping DB","caller":"sqlstore/store.go:272","error":"parse \"postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}?sslmode=disable&connect_timeout=10\": net/url: invalid userinfo","retrying in seconds":10}

This error clearly shows that the file is interpreting the environment variables literally instead of as environment variables, so I hardcoded the credentials into MM_SQLSETTINGS_DATASOURCE and Mattermost was able to successfully connect to the database and launch. There are also two other variables which use other variables, namely CERT_PATH (line 41) and KEY_PATH (line 42), but since I don't use Gitlab SSO, I haven't tried them.

I'm using Docker 20.10.14 on Debian GNU/Linux 11 (bullseye).

Southpaw1496 avatar Apr 30 '22 09:04 Southpaw1496

I've faced same issue and can confirm that POSTGRES_USER, POSTGRES_PASSWORD and POSTGRES_DB are ignored and MM_SQLSETTINGS_DATASOURCE is postgres://:@postgres:5432/?sslmode=disable&connect_timeout=10 inside mattermost container

florius0 avatar May 05 '22 11:05 florius0

Caused by https://github.com/docker/compose/issues/9442

florius0 avatar May 05 '22 16:05 florius0

As a workaround one may use the following script

IFS=$'\n'
for e in `grep -v '^#' .env`; do export `echo $e | envsubst`; done

docker compose -f docker-compose.yml -f docker-compose.nginx.yml build
docker compose -f docker-compose.yml -f docker-compose.nginx.yml down
docker compose -f docker-compose.yml -f docker-compose.nginx.yml up -d

It reads and interpolates .env file into environment variables.

florius0 avatar May 06 '22 09:05 florius0

I'm having trouble with the following error-messages:

WARN[0000] The "POSTGRES_IMAGE_TAG" variable is not set. Defaulting to a blank string. WARN[0000] The "RESTART_POLICY" variable is not set. Defaulting to a blank string. WARN[0000] The "POSTGRES_DATA_PATH" variable is not set. Defaulting to a blank string. WARN[0000] The "RESTART_POLICY" variable is not set. Defaulting to a blank string. WARN[0000] The "MATTERMOST_CONFIG_PATH" variable is not set. Defaulting to a blank string. WARN[0000] The "MATTERMOST_DATA_PATH" variable is not set. Defaulting to a blank string. WARN[0000] The "MATTERMOST_LOGS_PATH" variable is not set. Defaulting to a blank string. WARN[0000] The "MATTERMOST_PLUGINS_PATH" variable is not set. Defaulting to a blank string. WARN[0000] The "MATTERMOST_CLIENT_PLUGINS_PATH" variable is not set. Defaulting to a blank string. WARN[0000] The "MATTERMOST_BLEVE_INDEXES_PATH" variable is not set. Defaulting to a blank string. WARN[0000] The "MATTERMOST_CONTAINER_READONLY" variable is not set. Defaulting to a blank string. error while interpolating services.mattermost.read_only: failed to cast to expected type: invalid boolean:

Is this related to this issue?

hubortje avatar Sep 15 '22 06:09 hubortje

Your docker-compose version is too old, please see this thread for details. You will need to have at least 1.26.0, which is the first version that supports variable substitution in your .env files.

anx-ag avatar Oct 20 '22 16:10 anx-ag