# Environment variables not available using docker compose

**URL:** <https://discourse.nodered.org/t/environment-variables-not-available-using-docker-compose/97911>\
**Category:** General\
**Tags:** docker, environment-variable\
**Created:** [2 July 2025 05:08 UTC](https://discourse.nodered.org/t/environment-variables-not-available-using-docker-compose/97911 "2025-07-02T05:08:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigbean17](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bigbean17/32/89814_2.png) [@bigbean17](https://discourse.nodered.org/u/bigbean17)\
**Post date:** [2 July 2025 05:08 UTC](https://discourse.nodered.org/t/environment-variables-not-available-using-docker-compose/97911/1 "2025-07-02T05:08:53Z")

</div>

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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [2 July 2025 13:35 UTC](https://discourse.nodered.org/t/environment-variables-not-available-using-docker-compose/97911/2 "2025-07-02T13:35:20Z")

</div>

There are various ways to do this:

> **[Docker Pass Environment Variables | Cherry Servers](https://www.cherryservers.com/blog/docker-pass-environment-variables)**
>
> This tutorial shows how to pass environment variables to Docker in three ways: with Dockerfile, and -e, or –env and –env-file - with examples.

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:

```auto
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:

```auto
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:

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [30 September 2025 13:36 UTC](https://discourse.nodered.org/t/environment-variables-not-available-using-docker-compose/97911/3 "2025-09-30T13:36:18Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
