brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: in a Portainer stack, env_file stack.env is not the same as a .env file

Portainer
~/tips/tip-portainer-stack-env

- stack.env

Explains how environment variables reach containers in a Portainer stack through stack.env.

Variables in a Portainer stack look like normal Compose variables, but they do not always behave that way. The difference is worth knowing before you debug for an hour.

In the stack editor you can add environment variables in the UI, or load them with Load variables from .env file. In your Compose file you can reference them as ${VAR_NAME} and Portainer does not modify your file. For passing them into a container, Portainer documents this line:

services:
  app:
    env_file:
      - stack.env

The docs say env_file: stack.env works on Docker standalone and Podman, but not on Docker Swarm. A related FAQ says that stack.env only sets variables under the environment field. It does not do substitution for things like port numbers or image versions, so those need ${...} in the Compose file itself.

The caveat is that your stack is not run from a folder on disk with a .env next to it. If a variable seems ignored, check which of the two mechanisms you are actually using.

My rule of thumb: if the value goes inside the container, use the environment mechanism. If the value changes the Compose file itself, use ${...} and set it in the Portainer variables.

Further reading: Add a stack, .env vs stack.env, On this site: migrating a stack in Portainer does not move its volumes, On this site: Detach from Git in Portainer only downloads the main compose file

More Portainer tips Choose Polling in a Portainer GitOps update when your Git host can't reach Portainer Put Portainer environments in groups and give users access to a whole group Portainer's Edge Agent checks in every 5 seconds, and you can change that docker run -d -p 9001:9001 --name portainer_agent --restart=always \ Deploy the container → Replace All Portainer tips (28) All topics

Link