Dockerfile can't find .env file

Im trying to deploy my Phoenix Application with the help of a Dockerfile, however, where ever I put my COPY .env ., it’s always returning (in the logs)
** (File.Error) could not read file ".env": no such file or directory
My Dockerfile is in the same directory as the .env file = in the root directory of the project (no subfolders etc.)

Here is my Dockerfile A2AyRrRFsh | SourceBin

You might’ve already done this, but have you checked the .dockerignore file?

.env isn’t ignored there

Hi @vKxni,

Can you try moving the CMD ["/app/bin/server"] line to the last line of the Dockerfile so the command runs after .env copy

same error appears

Hi @vKxni

Looking at your Dockerfile, .env is being copied to /app. Is this expected?

im not really sure, where do I have to put - thats my question

@vKxni can you look through your code and find where your app is expecting the .env file? This appears to be where your error is coming from, not Dockerfile.

iex(4)> Dotenv.load(".env")
** (File.Error) could not read file ".env": no such file or directory
    (elixir 1.13.2) lib/file.ex:355: File.read!/1
    lib/dotenv.ex:218: Dotenv.read_env_file/1
    lib/dotenv.ex:135: Dotenv.load/1

In my config/runtime.exs. Locally everything is working fine without any .env problems

Can you try changing the COPY in your Dockerfile to:

COPY .env /app/config/

And on which line should that be?

Hi @vKxni,

This may be a stupid question, but why are you importing a .env file? Elixir doesn’t use it by default and neither does Docker.

If you want special ENV values, then set them in the fly.toml file. If they are secrets, then add them as secrets.

I use a library for that - dotenv_parser. How would I do that in the toml file?

Hi @vKxni,

The environment variable documentation can be found here: App Configuration (fly.toml) · Fly Docs

Related to the Dockerfile:

  • remove line #92
  • add the following at line #86:

COPY .env /app/config/

What’s app tho? I dont have such directory

app is a directory that exists in the Docker container.

In your linked Dockerfile, see lines 76 and 83

Ok thanks, i will give it a try and then post an update ASAP

hi again, sadly its returning me the same error.

(File.Error) could not read file ".env": no such file or directory
2022-08-19T10:01:33Z   [info]    (elixir 1.13.2) lib/file.ex:355: File.read!/1
2022-08-19T10:01:33Z   [info]    (dotenv_parser 2.0.0) lib/dotenv_parser.ex:98: DotenvParser.load_file/1

here is my dockerfile: TQNJG0IO4I | SourceBin

In runtime.exs before you call DotenvParser.load_file(".env"), could you print out the result of File.cwd()?

I’d also recommend using the full path to your .env file in that call:
DotenvParser.load_file("/app/config/.env")

1 Like