无法在docker-compose内为.env文件设置不同的名称

rseugnpd  于 2022-12-11  发布在  Docker
关注(0)|答案(1)|浏览(205)

I'm refactoring and writing a new dockercompose for our project and I'm facing some really strange problems.
My dockercompose has different services inside, each one has the own .env file. All the .env files are inside the same directory of the docker-compose file of course.
To keep it simple for my test, now I'm working with just ONE service inside the docker-compose.
If I name the .env file just ".env", everything works fine BUT if I try to name it "userservice.env", the docker compose CAN find the file but it can't read some variables such as the git hub access key and username.
That's my docker-compose:

version: '3.8'

services:
  db:
    image: mariadb:10.5.2
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: password
    container_name: exampledb
    ports:
      - "4040:3306"
    volumes:
      - ./initmysqldb:/docker-entrypoint-initdb.d
  adminer:
    image: adminer
    restart: always
    ports:
      - "${DOCKER_ADMINER_HOST_PORT:-38088}:8080"
  app-user-area:
    image: user_area
    build:
      context: ./user-area
      network: host
      args:
        GITLAB_ACCESS_KEY: "${GITLAB_ACCESS_KEY}"
        GITLAB_LOGIN: "${GITLAB_LOGIN}"
        NODE_VER: "${NODE_VER}"
    ports:
      - "${DOCKER_APP_HOST_PORT:-8088}:4000"
      - "${DOCKER_DELVE_HOST_PORT:-48088}:40000"
    env_file: 
      - userservice.env
    restart: always
    links:
      - db
      - adminer

The .env file:

....other info

# GITLAB
# ------------------------------------------------------------------------------
GITLAB_ACCESS_KEY=git_access_key
GITLAB_LOGIN=git_login

...other info

The error that the console gives to me if I name the file "userservice.env" (and then I call it as well inside the dockercompose of course) is:

The "GITLAB_ACCESS_KEY" variable is not set. Defaulting to a blank string.

And that's happened only if the ".env" file is called as "userservice.env". The strange thing is that apparently the dockercompose CAN find the .env file (if I name it "example.env" without change the name inside the docker-compose, the process can't run with "userservice.env not found" error) but it can't just read the environment variable inside.
In the end, other strange things is that if I add other services and one of them has a ".env" file and the others have "custom1.env", "custom2.env"... files, everythings works fine! It looks like that docker needs at least ONE file called ".env".
Any help would be appreciated.

lf5gs5x2

lf5gs5x21#

完全是我的错,我把.env和env_file混在一起了。
为了解决我的问题,我在docker-compose的同一个目录中创建了一个.env,其中包含了compose调用的所有变量。
在env_file部分中,我传递了服务使用的.env文件。
基本上,我们必须记住.env不同于env_file

相关问题