I have a droplet defined with Docker. I would like to define an environment variable (encrypted if possible). My docker-compose in the Postgres section uses commands such as - POSTGRES_USER=${POSTGRES_USER} The settings tab on the droplet has - what is it for = Service or API environment = staging. There is no setting option to add the environment variables. on my macBook I can set env variable using vi ~/.zshrc. If this cannot be done using the control panel is there a command line option to achieve this. Here is the section of the file .
The sql script defines the database name and schema used.
postgres:
image: postgres:15.4
_#command: postgres -c stats_temp_directory=/tmp_
restart: always
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
- POSTGRES_MULTIPLE_EXTENSIONS=${POSTGRES_MULTIPLE_EXTENSIONS:-postgis,hstore,postgis_topology}
volumes:
- ./postgres-data:/var/lib/postgresql/data
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
ports:
- 33066:5432
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
I’m the creator of node’s dotenv. You can do this now with dotenvx
source
Hi there,
To use encrypted environment variables with Docker, you cannot directly encrypt the variable values inside the
docker-compose.yml
file.Instead, you’d typically rely on external secrets management tools to store and retrieve the encrypted values, and then pass those values into your containers during runtime.
One option here would be HashiCorp Vault:
An alternative solution is to use something like
openssl
to encrypt your variables, eg:This will return a base64 encoded encrypted value. You can then store the encrypted value anywhere safe, for example, in a
.env
file, in your version control (because it’s encrypted), or in any secrets management tool.After that you could decrypt the value as follows:
I would personally stick to a solution like the HashiCorp Vault though.
Hope that this helps!
Best,
Bobby