Question

How to define encrypted environment variable - not application platform

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

Submit an answer


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!

Sign In or Sign Up to Answer

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.

Bobby Iliev
Site Moderator
Site Moderator badge
October 30, 2023

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:

https://www.digitalocean.com/community/tutorials/how-to-securely-manage-secrets-with-hashicorp-vault-on-ubuntu-20-04

An alternative solution is to use something like openssl to encrypt your variables, eg:

echo "YOUR_SECRET_VALUE" | openssl enc -aes-256-cbc -a -salt -pbkdf2 -pass pass:YOUR_ENCRYPTION_PASSWORD

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:

echo "ENCRYPTED_VALUE" | openssl enc -d -aes-256-cbc -a -salt -pbkdf2 -pass pass:YOUR_ENCRYPTION_PASSWORD

I would personally stick to a solution like the HashiCorp Vault though.

Hope that this helps!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel