We have a line in our Makefile which reads the env file and exports the variables to the shell:
include .env
export $(shell sed 's/=.*//' .env)
This enables the usage of the variables in a make file task like
connect-db:
docker-compose exec mysql mysql --host=0.0.0.0 --user=${DATABASE_USER} --password=${DATABASE_PASSWORD} ${DATABASE_NAME}
But there also services in the docker-compose file, which are using environment variables as volume names.
yarn:
image: node:10
entrypoint: "yarn"
command: "help"
volumes:
- ${YARN_HOME}:/yarn
- ${PROJECT_DIR}:/opt/project
- ${SSH_AUTH_SOCK}:/ssh_agent
- ${SSH_ID_RSA}:/node/.ssh/id_rsa:ro
environment:
SSH_AUTH_SOCK: ${SSH_AUTH_SOCK}
GIT_SSH_COMMAND: "ssh -i /node/.ssh/id_rsa -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
YARN_CACHE_FOLDER: "/yarn"
PROJECT_DIR: ${PROJECT_DIR}
YARN_HOME: ${YARN_HOME}
working_dir: /opt/project
When I run docker-compose run yarn the container is correctly executed, but if I run a make task with this docker-compose command, an error is logged
make yarn
docker-compose run --rm yarn
ERROR: Cannot create container for service yarn: create .: volume name is too short, names should be at least two alphanumeric characters
When disable the lines with the include .env the task works but the task which uses the variables not.
How can I use environment variables in both situations?