How to Use: envsubst

Install Link to heading

# Manjaro / Arch
sudo pacman -S gettext

# Alpine
apk add --update gettext

Usage Link to heading

Create a template input file ./truth.template containing:

Pizza is ${WHAT}

Export your environment variable:

export WHAT=love

Substitute it inside an output file:

envsubst < ./truth.template > ./output.file

You can see the result:

cat ./output.file
# Pizza is love

Real world usage Link to heading

Use it to generate random passwords and save them in env files:

# Generate password
export SECRET_PASSWORD=$(tr -dc "a-zA-Z0-9" < /dev/urandom | head -c 32)

# Generate template
echo 'POSTGRES_PASSWORD=${SECRET_PASSWORD}' > ./secrets.env.template

# Substitute
envsubst < ./secrets.env.template > ./secrets.env

cat ./secrets.env
# POSTGRES_PASSWORD=password1lol

Now you can use that secrets.env file in your docker entrypoint like detailed here: