Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Servers, nginx and Docker

docker run --env-file cannot read the .env file

Your error

docker: open .env: permission denied1 more line it can show

Fix it fast

Most common cause, 1 of 2

Docker is the snap package, and snap confinement blocks that path

Quick: keep the env file in an ordinary project folder that you own and point --env-file there.

Fix
# quick: a normal project folder under your home
mkdir -p ~/myapp
cp ~/.env ~/myapp/.env
docker run --env-file ~/myapp/.env my-image

# lasting: replace the snap with Docker's packages
sudo snap remove docker
# then follow https://docs.docker.com/engine/install/ubuntu/
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

docker: open .env: permission denied (--env-file: open .env in Docker CLI 29.10) means the docker client failed to open it. Snap-installed Docker reads files through the snap home interface, which skips hidden entries at the top of your home folder, such as ~/.env. A file made with sudo can block you. Move the file, use Docker's packages, or chown it.

  • Applies to Docker CLI 29.10, Docker Engine 29.10, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Find out which docker binary runs, where the file really lives and who owns it. Those three answers split the two causes.

Run this first
command -v docker
snap list 2>/dev/null | grep -w docker
realpath .env
ls -l .env

Then find the line you see in this list and open its fix:

  1. 1Docker is the snap package, and snap confinement blocks that pathcommand -v docker prints /snap/bin/docker, and realpath shows the file directly in your home folder (~/.env) or under a hidden folder such as ~/.config
  2. 2The file is owned by another user or not readable for youls -l .env shows root (or another user) as owner with a mode such as -rw-------, for example after the file was written with sudo

What does the error look like?

  • docker: open .env: permission denied
  • docker: --env-file: open .env: permission denied

Cause 1 of 2

Docker is the snap package, and snap confinement blocks that path

You seecommand -v docker prints /snap/bin/docker, and realpath shows the file directly in your home folder (~/.env) or under a hidden folder such as ~/.config

Check

open .env: permission denied from a snap-installed docker comes from snap confinement, not from the file mode. The Docker CLI opens the --env-file itself, and the docker snap's definition gives it the home interface for files under $HOME. snapd's rules for that interface leave out snap data and the top-level hidden entries of $HOME, so ~/.env or ~/.config/app/.env is refused while ~/myapp/.env is allowed.

Check
ls -l "$(command -v docker)"

Fix

Quick: keep the env file in an ordinary project folder that you own and point --env-file there. Lasting: remove the snap and install Docker Engine from Docker's apt repository, as Docker's Ubuntu guide describes.

Fix
# quick: a normal project folder under your home
mkdir -p ~/myapp
cp ~/.env ~/myapp/.env
docker run --env-file ~/myapp/.env my-image

# lasting: replace the snap with Docker's packages
sudo snap remove docker
# then follow https://docs.docker.com/engine/install/ubuntu/

Verify

The run starts and the variables are present in the container.

Verify
docker run --rm --env-file ~/myapp/.env alpine env

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 2

The file is owned by another user or not readable for you

You seels -l .env shows root (or another user) as owner with a mode such as -rw-------, for example after the file was written with sudo

Check

open .env: permission denied with the regular Docker package means your user lacks read access to the file or to a folder above it. The CLI reads the env-file with os.Open in your own process, and open(2) returns EACCES when the requested access is not allowed or a directory in the path cannot be searched.

Check
ls -l .env
ls -ld .

Fix

Make the file yours and keep it private, since it holds secrets.

Fix
sudo chown "$(id -un)":"$(id -gn)" .env
chmod 600 .env

Verify

The same docker run command now starts the container.

Verify
docker run --rm --env-file .env alpine env

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Install Docker Engine on servers from Docker's apt repository instead of the snap, and check with command -v docker after setup.
  • Create and edit .env files as the user who runs docker, never with sudo, and keep them at mode 600.
  • Keep env files inside the project folder, not loose in the home folder or under a dot folder.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, Dispatch Pro.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.