docker run --env-file cannot read the .env file
Fix it fast
Most common cause, 1 of 2Docker 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.
# 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/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.
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.
command -v docker
snap list 2>/dev/null | grep -w docker
realpath .env
ls -l .envThen find the line you see in this list and open its fix:
- 1Docker is the snap package, and snap confinement blocks that path
command -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 - 2The file is owned by another user or not readable for you
ls -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 denieddocker: --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.
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.
# 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.
docker run --rm --env-file ~/myapp/.env alpine envConfirmed by raw.githubusercontent.com
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.
ls -l .env
ls -ld .Fix
Make the file yours and keep it private, since it holds secrets.
sudo chown "$(id -un)":"$(id -gn)" .env
chmod 600 .envVerify
The same docker run command now starts the container.
docker run --rm --env-file .env alpine envConfirmed by raw.githubusercontent.com
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
- Docker: docker container run, --env-file
- Docker CLI source: kvfile.go (os.Open of the env-file)
- Docker CLI source v29.10.0: opts.go (--env-file error prefix)
- Snap: home interface
- snapd source: home interface AppArmor rules
- Canonical docker snap: snapcraft.yaml (plugs)
- Snap: get started (snap list, snap remove, /snap/bin)
- Docker: install Docker Engine on Ubuntu
- Linux man page: open(2), EACCES
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.