0

I am trying to dynamically generate a json file during my build step of my container via Dockerfile like this:

FROM alpine:3.9

// ... snipped

SHELL ["/bin/bash", "-c"]
RUN echo $'{\n\
  "type": "some_type",\n\
  "project_id": "$PROJECT_ID",\n\
  "private_key_id": "$PRIVATE_KEY_ID"\n\
}' > /etc/my_creds.json

EXPOSE 80

This works fine, so when I shell into my container and cat /etc/my-creds.json file, it appears the environment variables $PROJECT_ID and $PRIVATE_KEY_ID were written literally, they did not get replaced with the environment variable values that were present.

I.e. the file looks like this in the container:

enter image description here

Any ideas what I might be doing wrong here?

1 Answer 1

0

The variables will be expanded by the shell if you use double-quotes and escaping, for example:

FROM ubuntu
ENV FOO=bar
RUN echo "{\"foo\": \"$FOO\"}" >foo.json
$ docker build -t foo .
$ docker run foo cat foo.json
{"foo": "bar"}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.