Curious Minds

Clear answers to science and history questions

Docker Permission Denied: Why It Happens and How to Fix It

· 6 min read · Science

Docker container permission denied errors occur because the user or process inside the container does not have the necessary permissions to access a file, directory, or device, or because the container is trying to perform an action that the host's security settings (such as user namespaces, SELinux, or AppArmor) block. In most cases, the root cause is a mismatch between the user ID (UID) and group ID (GID) inside the container and the ownership or permissions on the host filesystem, or the container process running as a non-root user without the required privileges.

Understanding Docker's Permission Model

Docker containers share the host's kernel but have their own isolated filesystem, process tree, and network stack. By default, a container runs as the root user (UID 0) unless the Dockerfile or runtime command specifies otherwise. However, even root inside a container is not the same as root on the host: Docker uses Linux capabilities, seccomp profiles, and mandatory access control (MAC) systems like SELinux or AppArmor to restrict what containerized processes can do. This layered security model is often the source of permission denied errors.

When a container accesses a bind-mounted host directory, the kernel checks the permissions of the process inside the container against the file's ownership and mode on the host. If the container process runs as a non-root user (say, UID 1000) and the host directory is owned by root with mode 755, the process will get permission denied when trying to write. Similarly, if the container runs as root but the host directory is owned by a regular user with restrictive permissions, root inside the container may still be denied if user namespaces are enabled and remap UIDs.

Common Causes of Permission Denied

  • UID/GID mismatch: The container user's UID does not match the owner of the mounted files. For example, a Node.js image might run as UID 1000, but the host directory is owned by UID 1001.
  • Rootless Docker or user namespaces: When Docker runs in rootless mode or with user namespace remapping enabled, the container's root user is mapped to an unprivileged host UID. Files owned by real root on the host become inaccessible.
  • SELinux or AppArmor: On systems with SELinux enforcing (like Fedora, CentOS, RHEL) or AppArmor (Ubuntu), containers may be denied access to host files even if Unix permissions allow it, because the security context is wrong. The classic symptom is "permission denied" despite `ls -l` showing world-readable permissions.
  • Missing capabilities: Some operations (e.g., binding to privileged ports below 1024, mounting filesystems, changing file ownership) require specific Linux capabilities. A container dropped to non-root or run with `--cap-drop=ALL` will fail with permission denied.
  • Read-only filesystem: If the container's root filesystem is mounted read-only (`--read-only`), any attempt to write outside a tmpfs or volume will fail.
  • Volume ownership: Named volumes are initialized with the ownership of the image's directory at the mount point. If the image directory is owned by root and the container runs as non-root, writes to the volume fail.

Diagnosing the Problem

Start by identifying which user the container process runs as. Run `docker exec id` to see the UID and GIDs. Then check the ownership and permissions of the target file or directory on the host with `ls -ln` (the `-n` flag shows numeric UIDs, avoiding name resolution confusion). Compare the UIDs. If they differ, that is likely the cause.

Next, check whether SELinux or AppArmor is involved. On SELinux systems, `ausearch -m avc -ts recent` or `dmesg | grep -i denied` will show AVC denials. On AppArmor, `dmesg | grep -i apparmor` may reveal denials. If you see denials, the fix is usually to relabel the host directory with `chcon -Rt svirt_sandbox_file_t /path` (for SELinux) or to adjust the AppArmor profile.

Also verify mount options: `docker inspect | grep -A 10 Mounts` shows whether a bind mount is read-only. And check if the container is running with `--read-only` or a restrictive `--security-opt`.

Fixing UID/GID Mismatches

The most robust fix is to ensure the container user's UID matches the owner of the host files. You can do this by:

  1. Building the image with a user whose UID matches the host user (e.g., `RUN useradd -u 1000 appuser`).
  2. Running the container with `--user $(id -u):$(id -g)` to override the user at runtime.
  3. Changing the ownership of the host directory to match the container user: `sudo chown -R 1000:1000 /host/path`.
  4. Using a named volume and pre-populating it with correct ownership via an init container or by running a one-off container as root to `chown` the volume.

For rootless Docker, remember that the container's root is mapped to your unprivileged user. Files owned by real root on the host will be inaccessible. The solution is to avoid bind-mounting root-owned host paths, or to adjust ownership to your user.

Handling SELinux and AppArmor

On SELinux-enabled systems, Docker containers run with the `container_t` type by default. Host files typically have `user_home_t` or `httpd_sys_content_t` types, which containers cannot access. The simplest fix is to add the `:z` or `:Z` suffix to the volume mount (e.g., `-v /host/path:/container/path:z`), which tells Docker to relabel the host directory with the appropriate SELinux context. Use `:z` for shared volumes and `:Z` for private, unshared volumes.

For AppArmor, Docker's default profile usually allows access to bind-mounted paths, but custom profiles may restrict it. You can disable AppArmor for a container with `--security-opt apparmor=unconfined`, but this reduces security and should be used only for debugging.

Capabilities and Privileged Mode

Some permission denied errors stem from missing capabilities. For example, a container running as non-root cannot bind to port 80 without the `NET_BIND_SERVICE` capability. You can add it with `--cap-add=NET_BIND_SERVICE`. Similarly, `chown` requires `CHOWN`, and `setuid` requires `SETUID`/`SETGID`. Avoid `--privileged` unless absolutely necessary, as it grants all capabilities and disables security features.

If a container needs to perform administrative tasks, consider running it as root but with a restricted capability set, or use a dedicated init process that drops privileges after setup.

Frequently Asked Questions

Why does my container get permission denied when writing to a mounted volume?

Because the container process runs as a user whose UID does not own the volume's files, or the volume was initialized with root ownership. Check the UID inside the container and the ownership of the volume directory, then align them.

How do I fix permission denied on a bind mount with SELinux?

Add the `:z` or `:Z` flag to the volume mount to relabel the host directory, or manually run `chcon -Rt svirt_sandbox_file_t /host/path`. Ensure the container's SELinux context allows access.

Can I run a container as root and still get permission denied?

Yes, if user namespaces are enabled (rootless Docker or `--userns-remap`), the container's root is mapped to an unprivileged host UID, so it cannot access files owned by real root. Also, SELinux or AppArmor can deny access even for root inside the container.

What does "permission denied" on a Docker socket mean?

It means your user is not in the `docker` group and lacks permission to access `/var/run/docker.sock`. Add your user to the `docker` group (`sudo usermod -aG docker $USER`) and log out/in, or use `sudo` (not recommended for security).

Why does my container fail to start with permission denied on a file?

The container's entrypoint or command may be trying to execute a file that is not executable or is owned by a different user. Check the file's permissions and the user the container runs as. Also, ensure the file is present in the image or mounted correctly.

Is it safe to use `--privileged` to fix permission issues?

No, `--privileged` gives the container full access to the host, defeating container isolation. Use specific capabilities or adjust permissions instead.

In this article
  1. Understanding Docker's Permission Model
  2. Common Causes of Permission Denied
  3. Diagnosing the Problem
  4. Fixing UID/GID Mismatches
  5. Handling SELinux and AppArmor
  6. Capabilities and Privileged Mode
  7. Frequently Asked Questions