Search code examples
dockerdockerfileenvsubst

envsubst within docker using build-arg


Is it possible to use envsubst with build-arg during build time of the docker image ? I have a following scenario:

FROM alpine as substitutor
ARG ROOT_PWD
RUN apk add --no-cache envsubst
WORKDIR /app
COPY ./myfile.json .

RUN export ROOT_PASSWORD=${ROOT_PWD}  && \
    envsubst < myfile.json

But it doesn't seem to be working. any ideas ?


Solution

  • I suspect you are using DOCKER_BUILDKIT, in which case by default the output is not shown.

    You can either disable DOCKER_BUILDKIT or you can change the progress output.

    So given:

    $ cat myfile.json
    echo "$ROOT_PASSWORD"
    

    and Dockerfile:

    FROM alpine as substitutor
    ARG ROOT_PWD
    RUN apk add --no-cache envsubst
    WORKDIR /app
    COPY ./myfile.json .
    
    RUN export ROOT_PASSWORD=${ROOT_PWD}  && \
        envsubst < myfile.json
    
    1. Disabling DOCKER_BUILDKIT:

      $ DOCKER_BUILDKIT=0 docker build --no-cache -t foo --build-arg ROOT_PWD=foo . 
      ...
      Step 6/6 : RUN export ROOT_PASSWORD=${ROOT_PWD}  &&     envsubst < myfile.json
      ---> Running in 392f9f557efc
      echo "foo"
      ...
      
    2. Using --progress=plain:

      $ docker build --progress=plain --no-cache -t foo --build-arg ROOT_PWD=bar .
      ...
      #9 [5/5] RUN export ROOT_PASSWORD=foob  &&     envsubst < myfile.json
      #9 sha256:11149db43acce77a19337f355e010081c758738bae15daa2c4ddf8405e85c7c7
      #9 0.559 echo "bar"
      #9 DONE 0.6s
      ...