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:
- Is it a bug that docker container cannot load variables from Project Setting/Environment variables?
- Is it a bug that
env_file
fromdocker-compose.yml
is not working?
Thanks for reading, appreciate your help!