简体   繁体   中英

Can't set a different name for .env file inside docker-compose

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.

Completely my fault, I was mixing.env and env_file.

To solve my problem I created a.env inside the same directory of the docker-compose with all the variables called by the compose.

Inside the env_file section I pass the.env file used by the services.

Basically, we must keep in mind that.env is different from env_file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM