25

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.

sdc
  • 2,603
  • 1
  • 27
  • 40

2 Answers2

40

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

sdc
  • 2,603
  • 1
  • 27
  • 40
  • 6
    How do we use args from a file? – Raj Srujan Jalem Jun 04 '20 at 15:13
  • 1
    Thanks I also had a typo under `args`. But this post made me sure I am on the right track and corrected my config. Especially doing `- VAR=$VAR` instead of `- VAR=${VAR}`. Still the real problem was that I typoed like `- REPOSITORY_URL=$REPOSITYORY_URL`. Posting what you did wrong also helps others! – kuzdogan Jan 27 '22 at 14:45
  • Ultimately this ends up being really simple, but there are so many damn ways to do it wrong. Arghh – dgo May 26 '22 at 16:13
-1
`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
eglease
  • 2,445
  • 11
  • 18
  • 28