Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Docker build args from docker-compose .env file

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:

  • .env file
  • ARG
  • Similar example but setting container environment variables
  • False issue raised because .env and env_file is so confusing
  • combine ARG and ENV

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.

like image 295
sdc Avatar asked Nov 23 '17 08:11

sdc


People also ask

Does Docker build .ENV files?

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.

How do I override an .ENV file?

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.

What is the difference between ENV and arg in Dockerfile?

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> .

Can you have multiple Dockerfiles?

As Kingsley Uchnor said, you can have multiple Dockerfile , one per directory, which represent something you want to build.


1 Answers

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

like image 96
sdc Avatar answered Sep 19 '22 17:09

sdc