Docker-compose.yml env_file is not working?

I have spent more than 2 days with more than 20 failed builds to try to use docker compose with CircleCI:

#First:
I used Environment variables in Project Setting, the ubuntu machine was able to received the variables (I checked that by SSH into the build), but the docker environment cannot see the variables.

# Try to show variable in ubuntu machine
ubuntu@xxx.xxx.xxx$ echo $CI_DB_HOST
mysql
# Connect to my web container
ubuntu@xxx.xxx.xxx$ sudo lxc-attach -n "$(docker inspect --format '{{.Id}}' myapp_web_1)" -- bash
# Try to check the variable inside container
root@docker-container$  echo $CI_DB_HOST
# (Empty result)

Second:

So I tried to use the env_file inside docker-comose.yml to load the environment file itself. In the circle.yml file, I run a command to create an environment file from current context

circle.yml

machine:
  services:
    - docker

test:
  pre:
    - ( set -o posix ; set ) | grep CI_ > env/test.env
    - docker-compose -f docker-compose.yml build
  override:
    - docker-compose -f docker-compose.yml run --entrypoint "bash ./run-test.sh" web

Notice the ( set -o posix ; set ) | grep CI_ > env/test.env command, I trying to extract all the variable starts with CI_ into env/test.env file.

Then, in docker-compose.yml, I load it with env_file

docker-compose.yml

web:
  build: .
  ports:
    - "80:80"
  links:
    - mysql:mysql
  volumes:
    - ./mount/test-report:/var/www/test-report
  env_file:
    - ./env/test.env

And test it:
test.env file content:

CI_DB_HOST=mysql
CI_DB_USER=root
CI_DB_PASS=password
CI_DB_NAME=hac_local

The docker container is still cannot see the variables:

root@docker-container$  echo $CI_DB_HOST
# (Empty result)

Questions:

  1. Is it a bug that docker container cannot load variables from Project Setting/Environment variables?
  2. Is it a bug that env_file from docker-compose.yml is not working?

Thanks for reading, appreciate your help!

Two suggestions:

  • Can you try moving your env_file directive to a key-value instead of an array?
web:
  build: .
  ports:
    - "80:80"
  links:
    - mysql:mysql
  volumes:
    - ./mount/test-report:/var/www/test-report
  env_file: ./env/test.env
  • Have you made sure it runs locally?
1 Like

It does work locally. I will try the key-value option.

The key-value solution is not working as well. Any idea? :frowning:

Were you able to find a solution for this?

I used a hack to make it work, the docker-compose.yml env file still not working.

I attempted the same trick you originally tried. Worked like a charm for me.

What exactly is the trick you did? I don’t remember provide any trick in this topic.

Sorry, this worked for me.

The only difference being I’m doing this in the pre of dependencies rather than test. Also expanded on the command a bit for my own purposes but that should be irrelevant.

Once my tests run the env vars are available.

dependencies:
  pre:
    - touch .env
    - ( set -o posix ; set ) | grep CIENV_ | awk -F CIENV_ '{ print $2 }' > .env

But we both know that if we set the environment variables in the Project Setting, then it should be automatically loaded to the docker container, right? Why do we even need to create a .env file manually in pre dependencies or whatever anyway?

Right now the trick does work for me too, but in general, I don’t feel good to have tricks in my project to fix something that should work by default.

I really love CircleCI for providing such a useful service and being free, and they could do it better if they care about small problems from individual users like my case.

I really hope they could take a look and fix this.