Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stack.env does not work #6701

Open
traverseda opened this issue Mar 28, 2022 · 69 comments
Open

stack.env does not work #6701

traverseda opened this issue Mar 28, 2022 · 69 comments
Assignees
Labels

Comments

@traverseda
Copy link

traverseda commented Mar 28, 2022

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.

@zeh235
Copy link

zeh235 commented Apr 2, 2022

same issue here

@derekpovah
Copy link

Seeing the same behavior on EE 2.12.1

@PrivateButts
Copy link

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.

@gaby
Copy link

gaby commented Apr 27, 2022

Only way I have been able to get this to work is by passing

-v /opt/data/env.json:/opt/data/env.json to the portainer server container. That hack doesn't work with portainer-agent though, seems like the server tries to get the file from it's local fs instead of the agent fs.

@qosmio
Copy link

qosmio commented May 14, 2022

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).

EXAMPLE
$ cat /tmp/z
VAR1="TEST"
VAR2="TEST"
awk -F= 'BEGIN{print "    environment: "}{print "      "$1": \"${"$1"}\""}' /tmp/z

Which should output

    environment:
      VAR1: "${VAR1}"
      VAR2: "${VAR2}"

@JuniorC07
Copy link

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:

version: '3.4'
services:
  some-project:
    env_file:
      -  stack.env
    build:
      context: .
    image: some-project
    restart: always
    ports:
      - '8080:80'

After(Working):
returning to the root directory with ../stack.env

version: '3.4'
services:
  some-project:
    env_file:
      - ../stack.env
    build:
      context: .
    image: some-project
    restart: always
    ports:
      - '8080:80'

Obs: I know it doesn't answer your question, but it could be related.

@gaby
Copy link

gaby commented May 15, 2022

My problem is that when you specify env_file: myvars.env

The server works fine, but the agent uses the same env instead of its local one. :-S

@qosmio
Copy link

qosmio commented May 24, 2022

Obs: I know it doesn't answer your question, but it could be related.

Unfortunately, correct, I'm not specifying a local directory/project. Just letting Portainer manage everything (using its own volume) when I enter in the environment fields.

Another solution was to manually add the "stack.env" file into Portainer's volume... which defeats the purpose of managing stacks through Portainer IMO.

I see #6911 is related to this issue and looks like an internal ticket has been created? Hoping that's the case.

@wolfcry0
Copy link

wolfcry0 commented Jul 28, 2022

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.

@tamarahenson tamarahenson self-assigned this Aug 9, 2022
@samdulam
Copy link
Collaborator

Blogpost has been updated to reflect that this currently only works for Docker Standalone and not for Docker Swarm.

@gaby
Copy link

gaby commented Aug 16, 2022

@samdulam It also doesnt work for portainer agents, only the server.

@tamarahenson
Copy link

@gaby

I wanted to follow up on this request. I created a New Environment >> Docker Standalone >> Agent >> Create Stack >> Docker Compose >> Load stack.env >> without issue. If you are using the Docker Swarm >> Agent >> the stack.env will not work.

Thanks!

@reejosamuel
Copy link

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

patzm added a commit to patzm/dockerfiles that referenced this issue Oct 15, 2022
@Klionheart
Copy link

Hello, just a couple more, hopefully useful, use cases :)

Using "stack.env" does not currently work when:

  • Using Portainer CE 2.16.1
  • Using a Docker Standalone environment
  • Trying to deploy a stack through a git repository.

It does work, however, by using "../stack.env" as path instead.


Using "stack.env" on the other hand, works just fine when:

  • Using Portainer CE 2.16.1
  • Using a Docker Standalone environment
  • Trying to deploy a stack directly with Web editor.

@tamarahenson
Copy link

@Klionheart

Thank you for the additional information. I am going to further investigate the stack.env with Git. Updates to follow.

Thanks!

@tamarahenson
Copy link

@Klionheart

Is this what you are trying to do here:

Screen Shot 2022-11-17 at 4 28 27 PM

Thanks!

@marte26
Copy link

marte26 commented Nov 18, 2022

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

@Klionheart
Copy link

Klionheart commented Nov 18, 2022

@Klionheart

Is this what you are trying to do here:

Screen Shot 2022-11-17 at 4 28 27 PM

Thanks!

@tamarahenson the idea is to do:

image

where the docker-compose.yaml referenced is:

version: "3"

volumes:
    db-data:

services:
  db:
    image: postgres:12
    volumes:
      - db-data:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=$DB_NAME
      - POSTGRES_PASSWORD=$DB_PASSWORD
  web:
    image: healthchecks:v2.4.1
    env_file:
        - stack.env
        [...]

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.

@tamarahenson
Copy link

@Klionheart

Here is my testing. I am unable to reproduce in Portainer CE or BE:

Screen.Recording.2022-11-18.at.2.09.33.PM.mov

This is my wordpress-env.yml
https://github.com/tamarahenson/automation-scripts/blob/main/stack-code-examples/wordpress-env.yml

    env_file:
      - stack.env

This is my stack.env that resides in the same path stack-code-examples
https://github.com/tamarahenson/automation-scripts/blob/main/stack-code-examples/stack.env

My mistake earlier was trying to call the stack.env in the Additional paths. Those are for .yml only.

Thanks!

@Klionheart
Copy link

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
Copy link

@Klionheart

Thank you for the additional information. I figured out how to reproduce this.

[1] I changed the name of my stack.env to stack.env.bak in my repo
[2] Create Stack with wordpress.yml in my repo using - stack.env
[3] Upload my local stack.env
[4] Received error
[5] Updated my wordpress.yml to - ../stack.env
[6] Deployed without error

TODO: Update our Blog with further instructions for Git Repositories

Thanks!

@Pwuts
Copy link

Pwuts commented Jan 26, 2023

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

I am experiencing the exact opposite on Portainer 2.16.2, Docker Standalone environment connected through portainer/agent:latest:

  • env_file: stack.env works
  • env_file: ../stack.env -> file not found

This thread and the documentation could really use some clarification on this.

@fcaspani01
Copy link

My workaround is manually insert stack.env file inside portainer's docker volume then i updated related docker stack and now it works.

Screenshot 2023-02-13 at 10 16 36

Screenshot 2023-02-13 at 10 20 21

@a-camacho
Copy link

Same issue here.

@rulim34
Copy link

rulim34 commented Oct 12, 2023

This bug just randomly started affecting us. We are using Docker standalone. After some quick testing, it seems that using stack.env is no longer compatible with "Enable relative path volumes" when deploying from a repository. If I disable relative path volumes, it deploys. This combination worked before without issue, but I can't even clone the stack that originally worked on now. I simply get the error that stack.env could not be found.

None of the workarounds here work for me. I tried ../stack.env, didn't work. I originally had the host filesystem mounted into /host but I tried removing that and mounting the relative path location (just /mnt) into Portainer, didn't work. I even tried manually creating the file as a dirty fix, but Portainer just deletes it and then complains it doesn't exist.

I just encountered the exact same conditions here. The stack won't deployed (the stack.env not found) when I enabled the relative path volumes.

@darioackermann
Copy link

darioackermann commented Oct 14, 2023

It's a bit disappointing to see this issue still being open. I am happy to provide a reproduction if needed but this is a long-term dealbreaker for us. Can anyone of Portainer Crew please comment why this is not worked on? Stacks are not unimportant...

@samdulam
Copy link
Collaborator

Can you give details of the stack? Include screenshots of the repo folder structure along with Stack config screen in Portainer.
I'll try and look into all the different examples and come back to you with an update.

Also don't forget to mention the platform details: Docker Standalone / Swarm, OS and CPU Architecture.
If you are using relative paths then details of that too.. whether that path is local disk or a NFS/CIFS mount

@UrsaSolar
Copy link

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 filesystem

Brand new repo with only example.yml: https://github.com/portainer/portainer/assets/81395386/c988aa42-3a7f-4823-8bf1-1d165102f630

Full portainer config screen: https://github.com/portainer/portainer/assets/81395386/8dc12d24-f044-4a28-972f-103194877433

example.yml itself contains only the following:

services:
  hello_world:
    image: hello-world
    env_file:
      - stack.env

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.

@darioackermann
Copy link

When was the relative paths feature introduced?
For us it seems like this

  • Our original stack was created before this feature was released
  • After the feature was introduced (and old stacks taking a default I guess?) things broke
  • Deleting and recreating the stack under same name (with relative paths off) seems to have fixed the problem.

@samdulam
Copy link
Collaborator

@UrsaSolar
When you don't have a stack.env file in the repo, why are you referencing it in the compose YAML?
Are you using EXAMPLE_VAR in the compose for variable substitution?

@samdulam
Copy link
Collaborator

@darioackermann
Relative paths support was introduced in 2.17 and it does not get turned on by default.

Do you also reference stack.env in the compose file but not have it in the repo? If so, why?

@Geekimo
Copy link

Geekimo commented Oct 16, 2023

@UrsaSolar When you don't have a stack.env file in the repo, why are you referencing it in the compose YAML? Are you using EXAMPLE_VAR in the compose for variable substitution?

Maybe because the documentation states that this file is generated by portainer when deploying a stack ?

@samdulam
Copy link
Collaborator

@Geekimo
Can you send me a link to that document please?

@Geekimo
Copy link

Geekimo commented Oct 16, 2023

@samdulam https://docs.portainer.io/user/docker/stacks/add#:~:text=Alternatively%2C%20on%20Docker%20Standalone%20environments%20you%20can%20add%20stack.env%20as%20an%20env_file%20definition%20to%20add%20all%20the%20environment%20variables%20that%20you%20have%20defined%20individually%20as%20well%20as%20those%20included%20in%20an%20uploaded%20.env%20file%3A
@UrsaSolar is using Docker standalone, so it should work in his case nope ? In my case it looks like there is an issue regarding docker stack support of env_file, but if it is not supported, why am I having an error with docker trying to load the file ?

@samdulam
Copy link
Collaborator

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 a stack.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.

@darioackermann
Copy link

@darioackermann Relative paths support was introduced in 2.17 and it does not get turned on by default.

Do you also reference stack.env in the compose file but not have it in the repo? If so, why?

We had an empty stack.env in our repo, containing a comment stating it is a placeholder. The point of it being empty is that we do not want to expose credentials (in our case AWS Credentials) in our git repo, but rather add it through the portainer GUI. This seems to have worked before.

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.

I see the point and I generally agree it makes sense, but I'd consider it an awesome feature to enable this for git-deployed stacks as well - such that exposing credentials on git is not necessary.

@samdulam
Copy link
Collaborator

@darioackermann
You should not see any error as long as stack.env exists even if its empty.
If you are adding AWS creds as ENV vars in Portainer UI, how are they then referred in the compose YAML?

@a-camacho
Copy link

Hi guys,

I am having the same issue. But I am not deploying from repository.

Am I right that I should be able to :

  • Create a stack using Portainer
  • Use "advanced env vars" editor bellow stack, to add all my environment variables
  • I then should be able to use env_file: stack.env in my docker-compose, so my env vars added in Portainer UI are used in one or more containers.

Am I doing something wrong ?
I see everyone talk about repositories and "relative path", but I think this usage is supported too, right ?

Thanks a lot

@samdulam
Copy link
Collaborator

Hi guys,

I am having the same issue. But I am not deploying from repository.

Am I right that I should be able to :

  • Create a stack using Portainer
  • Use "advanced env vars" editor bellow stack, to add all my environment variables
  • I then should be able to use env_file: stack.env in my docker-compose, so my env vars added in Portainer UI are used in one or more containers.

Am I doing something wrong ? I see everyone talk about repositories and "relative path", but I think this usage is supported too, right ?

Thanks a lot

should work for docker but won't work for docker swarm.

@a-camacho
Copy link

Hi guys,
I am having the same issue. But I am not deploying from repository.
Am I right that I should be able to :

  • Create a stack using Portainer
  • Use "advanced env vars" editor bellow stack, to add all my environment variables
  • I then should be able to use env_file: stack.env in my docker-compose, so my env vars added in Portainer UI are used in one or more containers.

Am I doing something wrong ? I see everyone talk about repositories and "relative path", but I think this usage is supported too, right ?
Thanks a lot

should work for docker but won't work for docker swarm.

Oh right, so that is my problem. Don't think it's well documented.

So with Docker Swarm, the only way to use environment variable editor from Portainer, is to add all env vars to editor, and then use them in docker-compose.yaml with $ ?

Thanks, it seems it is clear in comments : https://www.portainer.io/blog/using-env-files-in-stacks-with-portainer

@darioackermann
Copy link

@darioackermann You should not see any error as long as stack.env exists even if its empty. If you are adding AWS creds as ENV vars in Portainer UI, how are they then referred in the compose YAML?

We add env_file to the service

  service-that-needs-aws-creds:
    image: xyz.dkr.ecr.eu-central-1.amazonaws.com/xyz
    build: ./services/xyz/                              # for docker-compose build
    environment:
      - CUSTOM_ON_404=index.html
      - ....
    env_file:
      # Secrets in Portainer
      - stack.env
    restart: unless-stopped

This seems to be working with relative paths off and after recreating the stack from scratch.
So our issue seems resolved.

@UrsaSolar
Copy link

@UrsaSolar When you don't have a stack.env file in the repo, why are you referencing it in the compose YAML?

I was following this Portainer blog post, which is the only thing I could find when searching for anything regarding Portainer and variables. I ultimately want to keep secrets out of the git repo.

Are you using EXAMPLE_VAR in the compose for variable substitution?

I've moved to using variable substitution in most the affected stacks to get it working again. At first I wasn't aware of individual variable substitution, as the above post gave the impression that Portainer only supported adding all or nothing when it came to variables. In fact, I don't even remember where I leaned about variable substitution now, I can't find anything in the docs after a quick skim.

My expectation was that it would be able to both clone the repo and create stack.env at the same time, not that the features would be mutually exclusive, as nothing indicated they would be. This somehow did work at some point, as it broke a few existing stacks doing exactly that, which I had to delete and rebuild.

Personally I think variable substitution is a better solution for what I'm doing anyways, so my ultimate opinion is that this is really a documentation and UI issue. I was originally uncomfortable with the fact that everything had to get all the variables, but didn't deem it a dealbreaker. Substitution is much better for me. But I wanted to report the breakage all the same.

@marlluslustosa
Copy link

marlluslustosa commented Oct 31, 2023

The solution I'm using is to consume the portainer API directly from the pipeline of a git repository. So to create a stack I place the environment variables as secrets variables (Gitlab>Conf>CI/CD>secrets) in the repository, this allows me to obfuscate their call during execution. And then I add them as complementary variables via payload using the parameters below.

"env": [
{
"name": "test_CI_gitdefined",
"value": "value_test_CI_gitdefined"
}
StackCreateDockerSwarmRepository

And then in docker-compose.yml:
environment:
MY_ENVIRONMENT_VARIABLE: ${test_CI_gitdefined}

This was the only way to do GitOps using Git+Portainer+Swarm.
To make this work well, I also wrote extra code to consume this API and make the pipeline magic work.

I think that the passage of variables could be better adapted by the portainer team. this is an important feature

@MatthewCorbiell
Copy link

volumes:
  - /opt/portainer-config:/opt/portainer-config:ro # Paths must match!

...

  • The path inside portainer MUST equal the path on the host.

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.

tsachleben added a commit to tsachleben/portainer-stacks that referenced this issue Dec 24, 2023
@christianstrauch
Copy link

It seems the issue is caused by the env file not being generated (or accessible) across nodes in a swarm. I ended up manually putting the stack.env onto the portainer data volume, into the appropriate compose folder.

@ecker00
Copy link

ecker00 commented Apr 15, 2024

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 causes open /data/compose/3/stack.env: no such file or directory and it is not possible to update/deploy the stack.

@voslucas
Copy link

voslucas commented Apr 23, 2024

Same issue... I also use a swarm setup.

@devtronic
Copy link

@christianstrauch, @ecker00 and @voslucas : Did you tried this? #6701 (comment)

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 🙂

@hdavy2002
Copy link

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 causes open /data/compose/3/stack.env: no such file or directory and it is not possible to update/deploy the stack.

Same issue with me. I spend the whole day yesterday trying to fix this. Issue remains with no proper solution

@esterlinpol
Copy link

Hello there, I experienced the same situation and expent a lot of time investigating, turns out that I was referencing an .env file in the code as overlooked it.

This is not needed as you can import the variables in the new stack interface from a file and it will define all the values in the env files making it unneeded to specify the env file in the new stack code.

If you really need to put the env file in the new stack code, you can also take note of the missing file error, and recreate the path adding +1 to the path in the portainer container files.

I hope this helps you guys out there!

@shortbus
Copy link

shortbus commented May 2, 2024

"you can also take note of the missing file error, and recreate the path adding +1 to the path in the portainer container files"

what do you mean add +1, can you give an example?

@esterlinpol
Copy link

"you can also take note of the missing file error, and recreate the path adding +1 to the path in the portainer container files"

what do you mean add +1, can you give an example?

Yes and sorry for not including it originally.

When a new stack is deployed, it gives a number to the path:

/data/compose/3/stack.env: no such file or directory

So I noticed that if you wish to redeploy it with the env file on the server, you need to place the env file under the next one:

/data/compose/4/stack.env: no such file or directory

I hope this helps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests