Permission denied on accessing host directory inside containers

Permission denied on accessing host directory inside containers

What’s going on?

Creating a container by Podman or Docker with -v option, the access to your data volume was denied.

$ mkdir -p /tmp/directory
$ podman run -itd --name test -v /tmp/directory/:/directory:rw fedora:28 /bin/bash
$ podman exec test ls /directory
ls: cannot open directory '/directory': Permission denied
Error: exit status 2

Solutions

Because of the restriction of SELinux, you can be blocked to access the host directories from a container.
That’s a normal case to make your system stronger.

In such a case, you can easily turn off the SELinux feature by sudo setenforce 0.
But the better way is to add an SELinux rule to your host directory and you can do this by:

chcon -Rt svirt_sandbox_file_t /tmp/directory/

All the subdirectories and files in /tmp/directory/ will be granted with correct rules so that you can access them inside your containers.

One more thing

Linux will take care of SELinux Context when you copy a file into your directory regardless cp command or copy&paste in GUI is used.

But I found if you copy the file by 3rd party tools, such as Beyond Compare, The SELinux Context will be kept unchanged. Please note that it could bring troubles to you.

你可能感兴趣的:(问题笔记)