TL/DR: Can I use .env
file variables in docker-compose's environment
directives?
I have a Dockerfile
which uses the ARG
variable to copy files based on an environment variable.
In my docker-compose
I want to run two versions of this container with different configuration.
Is there a way where I can set different filepaths in a single .env
file and have it build twice, like the example below?
Or is there a smarter way to accomplish this?
/
/.env
/docker-compose.yml
/app
/app/Dockerfile
/version1/data
/version2/data
/.env
VERSION_1_PATH=./version1/data
VERSION_2_PATH=./version2/data
/app/Dockerfile
FROM node:latest
ARG APP_PATH # pull this environment variable
COPY $APP_PATH /var/app/ # use it to copy source to the same generic destination
/docker-compose.yml
version: "3"
services:
version1:
build: ./app
container_name: version1
env_file:
- '.env'
environment:
APP_PATH: ${VERSION_1_PATH}
version2:
build: ./app
container_name: version2
env_file:
- '.env'
environment:
APP_PATH: ${VERSION_2_PATH}
You can add args
in compose file when to define build, something like follows:
version: '3'
services:
version1:
build:
context: ./app
args:
- APP_PATH=${VERSION_1_PATH}
version2:
build:
context: ./app
args:
- APP_PATH=${VERSION_2_PATH}
And no need to define .env
in env_file
if just want it be used in build as .env
could default be used in docker-compose.yml
. And, environment
also not for build
, it's for running container
.
One example, FYI.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With