I am experiencing an odd issue with docker-compose's .env
file. I am able to use the first variable key=pair in my .env file, but only the first variable. This is my folder structure
|- root
| |- .env
| |- docker-compose.yaml
| |- service-1
| |- Dockerfile
.env:
GIT_TOKEN=c3e13c4e33935
DB_PWD=mypassword
docker-compose.yaml:
version: '3'
web-server:
container_name: service-1
image: sdc/service-1:0.1
build:
context: ./service-1
args:
- GIT_TOKEN=$GIT_TOKEN
- DB_PWD=$DB_PWD
service-1/Dockerfile:
FROM node:boron
ARG GIT_TOKEN
ARG DB_PWD
RUN git clone https://${GIT_TOKEN}@github.com/chrxn/sdc.git
RUN echo {"database_password:" $DB_PWD } > crews.txt
The problem is that the GIT_TOKEN variable is working perfectly, but the DB_PWD variable is not. Even if put the GIT_TOKEN variable in the echo line, the token is saved to a file (so I know it isn't an echo/bash interpolation issue) Any help is greatly appreciated. I have read everything I can find related to Docker's environment variables.
NOTE: I've modified a few things. My database password is not mypassword and that isn't a real git repo
References:
I really would like to stick to Docker build arguments instead of environmental variables so that the values are not stored in the container's environment variables.
If you are using docker-compose (which now comes bundled with Docker), . env is the default filename for the file that contains variables that are made available to the parser for the docker-compose. yml file ONLY, and not to the build process or container environment variables.
Overriding a single value in your docker-compose . env file is reasonably simple: just set an environment variable with the same name in your shell before running your docker-compose command.
From Dockerfile reference: The ARG instruction defines a variable that users can pass at build-time to the builder with the docker build command using the --build-arg <varname>=<value> flag. The ENV instruction sets the environment variable <key> to the value <value> .
As Kingsley Uchnor said, you can have multiple Dockerfile , one per directory, which represent something you want to build.
Facepalm 🤦♀️ - This is working perfectly. I was putting the - DB_PWD=$DB_PWD
argument under the wrong service in my docker-compose.yaml
file. I will leave this here as a reference on how to use the .env
file with docker build arguments -- and as a reminder to my self that I'm an idiot. I'm embarrassed --100 SOF reputation
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