Environment variables not available using docker compose

Hi all,

I'm migrating my Node-RED deployment from systemd to Docker Compose, and I'm running into an issue with environment variables.

Previously, when using systemctl, I used an EnvironmentFile=/etc/nodered/.env in my service unit. This allowed me to access environment variables directly in Node-RED via env.get("MY_VAR") inside function nodes.

Now, in my Docker Compose setup, I have:

services:
  app:
    image: ${IMAGE}
    env_file:
      - ../../.env
    environment:
      - NODE_RED_CREDENTIAL_SECRET=${NODE_RED_CREDENTIAL_SECRET}

I verified with docker compose config that the variables are injected correctly into the container. But when I use env.get("MY_VAR"), it returns "".

Is there a way to make env.get() work with environment variables passed via Docker (like it did with systemd)? I have over 20 environment variables, so manually declaring each one in settings.js feels quite unmaintainable. The simplest and most scalable solution for me is to use a .env file and access variables using env.get() as before.

Is there any recommended way to make env.get() work with variables from Docker’s env_file, similar to how it worked with systemd's EnvironmentFile?

Thank you!

There are various ways to do this:

If you are using Docker Compose you have more options:

By specifying the variable name without a value, Docker Compose will look up the variable in the current environment.2 For example:

db:
  environment:
    - KB_DB_TAG_VERSION

Another method is to create a .env file in the same directory as your docker-compose.yml file. This file can contain key-value pairs of environment variables. For instance:

KB_DB_TAG_VERSION=kb-1.3.20-v1.0.0

You can also prepend the environment variables to the docker-compose command. For example:

KB_DB_TAG_VERSION=kb-1.3.20-v1.0.0 docker-compose up

Additionally, you can use the env_file section in your docker-compose.yml file to specify the path to an environment file. This allows you to load variables from a file into your Docker containers.3 For example:

services:
  app:
    image: alpine:latest
    env_file:
      - config.env

Docker Compose also supports interpolation, allowing you to combine different values into one. For example, you can set a fallback value in case a variable isn't set in your shell.