portainer: `stack.env` does not work
Bug description
Following along with the tutorial here: https://www.portainer.io/blog/using-env-files-in-stacks-with-portainer I get stack.env: no such file or directory
Expected behavior
stack.env
should work as per the above blog post.
Portainer Logs
level=info msg="2022/03/28 12:47:58 http error: Unable to start stack (err=open /data/compose/51/stack.env: no such file or directory"
level=info msg="2022/03/28 12:47:57 http error: Unable to start stack (err=open /data/compose/51/stack.env: no such file or directory"
level=info msg="2022/03/28 12:52:49 http error: open /data/compose/57/stack.env: no such file or directory"
level=info msg=" (err=open /data/compose/57/stack.env: no such file or directory"
level=info msg=") (code=500)"
level=info msg=" (err=open /data/compose/51/stack.env: no such file or directory"
level=info msg="2022/03/28 12:49:46 http error: open /data/compose/51/stack.env: no such file or directory"
level=info msg="2022/03/28 12:53:58 http error: open /data/compose/58/stack.env: no such file or directory"
level=info msg=" (err=open /data/compose/58/stack.env: no such file or directory"
level=info msg="2022/03/28 12:49:31 http error: open /data/compose/51/stack.env: no such file or directory"
level=info msg="2022/03/28 12:48:45 http error: Unable to start stack (err=open /data/compose/51/stack.env: no such file or directory"
level=info msg="2022/03/28 12:48:25 http error: Unable to start stack (err=open /data/compose/51/stack.env: no such file or directory"
Steps to reproduce the issue:
Be running in swarm mode.
Create a new stack with the content.
version: "3.8"
services:
test:
env_file:
- stack.env
Add at least one environment variable to the environment variables section.
Try to deploy.
Technical details:
- Portainer version: Portainer CE 2.11.0
- Docker version (managed by Portainer): API version 1.41
- Platform (windows/linux): linux
- Command used to start Portainer (
docker run -p 9443:9443 portainer/portainer
): Uhh, it’s been running for a while as a stack… - Browser: Firefox
- Use Case (delete as appropriate): Using Portainer at Home
- Have you reviewed our technical documentation and knowledge base? Yes
Additional context
Assessing the possibility of running a dockerized mail server through portainer for my employer.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 20
- Comments: 67
Links to this issue
Commits related to this issue
- stack.env seems to be generated in the root according to https://github.com/portainer/portainer/issues/6701#issuecomment-1126852560 — committed to patzm/dockerfiles by patzm 2 years ago
- [firefly] Fix path of stack.env file See: https://github.com/portainer/portainer/issues/6701#issuecomment-1126852560 — committed to JuliusFreudenberger/infrastructure by JuliusFreudenberger 10 months ago
- Remove env.stack from authentik config https://github.com/portainer/portainer/issues/6701 — committed to tsachleben/portainer-stacks by tsachleben 6 months ago
to me, the error reason was the path of docker-compose.yml(using git repository), seted on Compose path field
Compose path: /some-project/docker-compose.yml
Before:
After(Working): returning to the root directory with …/stack.env
Obs: I know it doesn’t answer your question, but it could be related.
My workaround is manually insert stack.env file inside portainer’s docker volume then i updated related docker stack and now it works.
Tried using local (docker.sock) environment and Portainer agent environment. Both give errors saying
stack.env
can’t be found.With portainer agent, it worked after trying the suggestion from JuniorC07. changing the path of
stack.env
to../stack.env
Any updates on this? It is reproducible for me in Portainer Business Edition 2.18.3
I am also experiencing the same issue running Community Edition 2.18.1.
I have tried using “.env” & “stack.env” and it is still throwing errors.
What fixed it for me is in the compose web editor in Portainer, I changed ALL instances of:
to
I am using Portainer Community Edition 2.18.3. You can’t just change one .env to stack.env, or it will still throw errors. You have to change all of them.
Seeing the same behavior on EE 2.12.1
I can see the problem. When deploying a stack using web editor you can define Env Vars in Portainer UI and refer to a
stack.env
file. Portainer then creates astack.env
file along with the compose file and writes all the vars to that.This is not the case when deploying from Git, if you refer to
stack.env
it is expected to be in the repo. We clone the repo but do not add any files to that location as it will get overridden at next update.We should however clarify this in the documentation.
It seems Portainer itself is checking the existence of
env_file
entries. Since Portainer is running as a container, it can’t find the files unless you add a volume to the host file.Example solution: Your stack:
Portainer stack:
Important:
I am experiencing the exact opposite on Portainer 2.16.2, Docker Standalone environment connected through
portainer/agent:latest
:env_file: stack.env
worksenv_file: ../stack.env
-> file not foundThis thread and the documentation could really use some clarification on this.
Hello, just a couple more, hopefully useful, use cases 😃
Using “stack.env” does not currently work when:
It does work, however, by using “…/stack.env” as path instead.
Using “stack.env” on the other hand, works just fine when:
Ditto on EE 2.12.1, running on a swarm
When I go into the portainer vol, the compose folder that it’s looking for doesn’t exist. If I create the stack and then add env_file to the compose, it’s never created in the compose folder created for the id.
If I create an empty file in that folder the stack is able to be updated with the env_file definition, but the file isn’t updated with the env set in the gui.
@christianstrauch, @ecker00 and @voslucas : Did you tried this? https://github.com/portainer/portainer/issues/6701#issuecomment-1704036982
Portainer itself is running as a docker service, which means that you need to add volumes that matches the host path. Otherwise portainer can not “see” the files 🙂
Seems like a bug. Trying to deploy on CE v2.19.4, and the Stack YAML editor says
To reference the .env file in your compose file, use ‘stack.env’.
, but doing just that causesopen /data/compose/3/stack.env: no such file or directory
and it is not possible to update/deploy the stack.Thank you! I didn’t even think about the fact that portainer itself is a container and the fact that by default it might assume the pathing provided in env_file is a portainer container path and not a host path.
I added /nfs/configs:/nfs/configs:ro as one of my portainer bind mounts and then immediately all of my .env files starting with /nfs/configs pathing were found.
On my end this is easily reproduced using the following:
Docker Standalone, using a server & a couple agents CPU Arch: x86_64 Relative path:
/mnt/portainer
on local filesystemBrand new repo with only
example.yml
: https://github.com/portainer/portainer/assets/81395386/c988aa42-3a7f-4823-8bf1-1d165102f630Full portainer config screen: https://github.com/portainer/portainer/assets/81395386/8dc12d24-f044-4a28-972f-103194877433
example.yml
itself contains only the following:The error: https://github.com/portainer/portainer/assets/81395386/10d641c4-414a-47f1-8554-d667eba3e2e9
Either removing the
env_file
entry or disabling relative paths results in a successful deployment. Combining the two results in an error. This worked previously, as I had a stack configured that way which broke not too long ago. I could no longer pull and redeploy that stack, or create a new stack with the same config.I tried rolling back the versions to test but it resulted in some instability. I haven’t had time to spin up fresh environments to play with and see if I can figure out when it broke.
For some reason I have no luck using
stack.env
or../stack.env
. Both result infile not found
. I tried the web editor and also uploading my own compose file. Any ideas?Portainer 2.16.1, Docker Standalone environment.
Edit:
I really need this to work as I have to run a dozen services and there are many env vars some of which are used by multiple services. I could go and list them all one by one for each service in the compose file, but maintaining them and updating them will be a nightmare.
There currently is a difference on using
env_file: stack.yml
between Portainer on Docker Standalone and Portainer on Docker Swarm, see also #6814.According to this other issue it is “due to a lack of env file support in docker stack deploy”.
But I think there are some big misunderstandings between using a .env file to substitute/insert values into a docker-compose.yml and using an
env_file
for directly inserting environment variable into a container.It’s perfectly valid and possible to do the latter. Assume a stack.yml (or docker-compose.yml, the file name does not matter) contains:
And there is a stack.env file containing e.g.:
Running
docker stack deploy -c stack.yml test
succeeds and prints in the log (docker service logs test_test
):@fcaspani01 nice that it works but extremely bodgy and non-portable workaround.
@Klionheart
Thank you for the additional information. I figured out how to reproduce this.
[1] I changed the name of my
stack.env
tostack.env.bak
in my repo [2] Create Stack withwordpress.yml
in my repo using- stack.env
[3] Upload my localstack.env
[4] Received error [5] Updated mywordpress.yml
to- ../stack.env
[6] Deployed without errorTODO: Update our Blog with further instructions for Git Repositories
Thanks!
Hi again @tamarahenson, I think I see the misunderstanding here.
I’m not trying to reference an existing “stack.env” inside the git repository. In fact I absolutely do not want an .env file in the git repository since it contains secrets.
The idea is to do as documented in https://www.portainer.io/blog/using-env-files-in-stacks-with-portainer where it is explained that you can add Environment variables to the local Portainer stack, and then reference them en masse by using “stack.env” as an env_file, instead of manually referencing them one by one, in the docker-compose.yaml.
This indeed works with Web editor in Docker Standalone, but not in “Repository” mode where you need to use “…/stack.env” instead.
Hopefully this is more clear, thank you for your time 😃
@tamarahenson the idea is to do:
where the docker-compose.yaml referenced is:
which should work according to https://www.portainer.io/blog/using-env-files-in-stacks-with-portainer
but results in error:
error: open /data/compose/51/stack.env: no such file or directory
If I put “- …/stack.env” as the env_file in the compose.yaml, instead, it works, I guess because the stack.env file is generated as /data/compose/stack.env.
Interestingly, if I just use the same docker-compose.yaml directly in portainer (with Web Editor) it instead works with “- stack.env” as expected.
Hey, just wanted to say that I have the same problem. I’m deploying a docker-compose.yml file that sits in a subdirectory of a git repo. Trying to use stack.env as the env_file doesn’t work but …/stack.env does
PS: I’m using Portainer BE 2.16.1
I am also experiencing the same issue, referencing ‘stack.env’ as per the blog post results in an error:
open /data/compose/89/stack.env: no such file or directory
EE 2.14.0, docker swarm mode.
This is a bit confusing, as I’m fairly certain I am following the blog post instructions correctly.
Same issue for 2.13.1 on EE. I had to revert to specifying environment variables manually… If anyone needs to quickly generate required YAML, you can use the following.
Switch to “Advanced Mode” copy everything into a file somewhere (i.e. /tmp/z).
Which should output
same issue here