Skip to content

Instantly share code, notes, and snippets.

@abdennour
Created October 4, 2019 04:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdennour/d57a25472f5d924e0cba9f011db18e2f to your computer and use it in GitHub Desktop.
Save abdennour/d57a25472f5d924e0cba9f011db18e2f to your computer and use it in GitHub Desktop.
Dynamically replace environment variable names by its values inside file
FROM perl:5-slim
WORKDIR /data
COPY myfile.txt .
RUN perl -pe 's/\$([_A-Z]+)/$ENV{$1}/g' < myfile.txt > /tmp/myfile.txt
RUN perl -pe 's/\$(\{)?([a-zA-Z_]\w*)(?(1)\})/$ENV{$2}/g' < /tmp/myfile.txt > myfile.txt
# Only substitute variable that are defined perl -pe 's{\$(\{)?(\w+)(?(1)\})}{$ENV{$2} // $&}ge'
docker build -t substituter:0.1 .
docker run --rm -v $(pwd):/data substituter:0.1
Hello ${NAME}
Thank you ${NAME} for sending ${THING}.
I like this $THING
Thank you again $NAME
Best
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment