Search code examples
dockerdocker-composevolumes

Why is File Mounted on Container via docker-compose not Accessible?


In my docker-compose file, I try to mount a file from the host into the docker container.

The docker-compose file I have something like this:

version "2"

services:
    myservice:
        image:  images/previmage:1.0.0
        volumes:
            - /opt/files/aaa.conf:/aaa.conf

After the service is started, I look at the contents at the root of the container using docker from the host:

sudo docker container exec myservice_1 ls /

The result of that ls command for the aaa.conf entry shows that it looks like it is there, but permissions are not what I expect:

drwxr-xr-x.  2 root   root     6 Apr 11 2018 opt
-??????????  ? ?      ?        ?           ? aaa.conf
ls:  cannot access /aaa.conf:  Permission denied

Similarly, if I try other commands like 'cat aaa.conf', I get Permission denied.

I understand that permissions for the file need to be set on the host side. On the host I made permissions both 755 and then 777, but I still get Permission denied.

Is this the expected behavior?


Edit [running on AWS/EC2]

sudo docker container exec myservice_1 cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

sudo docker container exec myservice_1 id -u
33016

Solution

  • I had same problem, It's for SELinux (Check this post)

    Disable SELinux for a specific container

    You can disable SELinux for a specific container by adding --security-opt label:disable to your docker run command:

    docker container run --security-opt label:disable myservice_1
    

    Adding SELinux Rule (Recommended)

    According to this post, You can also use this command to enable access to the files

    chcon -Rt svirt_sandbox_file_t /path/to/volume
    

    Completely disable SELinux!

    Not recommended, but also works:

    su -c "setenforce 0"