2

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.

Pikappa
  • 284
  • 1
  • 11
  • Does it works if you add an empty .env alongside with the others ? – Brewal Dec 04 '22 at 15:52
  • When you say "and then I call it as well inside the dockercompose of course". How du you call it ? With `env_file` key ? Because inside your `app-user-area` container, you are using `args` (build time), not `environement`. – Brewal Dec 04 '22 at 16:00
  • @Brewal nope, is not working. The GITLAB_ACCESS_KEY and GITLAB_LOGIN are set inside the .env file as shown. And inside the docker-compose I use them inside the args section. If the .env file is named just ".env" everything works fine, if is named "user.env" for example, the compose can't find them. – Pikappa Dec 04 '22 at 17:00
  • But how do you tell compose to use `user.env` in your example ? I don't think it just "works" out of the box (catching every .env files) yes ? – Brewal Dec 04 '22 at 17:07
  • @Brewal I solved the problem, completely my fault. I'll answer the question – Pikappa Dec 05 '22 at 13:43

1 Answers1

2

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

Pikappa
  • 284
  • 1
  • 11
  • 1
    To be a bit more accurate, `env_file` option can be used in place of `environment` option. But you have to use a .env file to use env variables directly in the yaml config. In your case, as you are using some variables as build `args`, you must have a .env file. But it is not required – Brewal Dec 05 '22 at 17:14