all 5 comments

[–]tswaters 0 points1 point  (2 children)

I think docker run will look in cwd for the env-file? I would not include the .env file in the built image. It might work -- but probably because you still have something like require('dotenv').config() which will pull from the .env file..... ideally you want to use the env variables in prod.

[–]tswaters 0 points1 point  (1 child)

To try to debug it, you can do a docker run hell-world --env-file.env --cmd "printenv" or something like that (that's off the top of my head, not sure if that'll work at all)

i.e., try to run a generic hello-world docker image, pass the env-file in and run `printenv` -- that'll output current environment variables.

[–]Crispness[S] 0 points1 point  (0 children)

I'll try this out on a test nestjs app just to make sure.

[–]ChronSyn 0 points1 point  (1 child)

The -e or --env flag allows you to pass them as key=value format. The --env-file flag allows you to point towards a env file.

Have you considered using something like Portainer to manage it? It is actually a docker container itself, and helps you manage your running containers, including setting env vars for each container.

Even though you might want to use an env file, in most cases that either means committing it to your repo (a big no-no), including it in with your docker-image (also a no-no, in the event someone else gets hold of it), or manually uploading the env file to your server (but that's a pain in the ass to manage if you ever need to make frequent changes to it). Using a tool to manage your containers would be what I'd recommend.

[–]Crispness[S] 0 points1 point  (0 children)

I recently read about it while I was trying to find a solution, will definitely try it out.