Docker容器访问挂载文件权限问题

336 阅读2分钟

问题描述

在使用docker-compose部署项目时,yaml文件如下:


version: '3'

services:

  purchasing-contract-consumer:

    image: my-registry.com/consumer:latest

    environment:

      - TZ=Asia/Shanghai

      - app_env=prod

    restart: always

    working_dir: /app

    command: python app.py

    volumes:

      - type: bind

        source: /home/admin/deploy/consumer/application.log

        target: /app/application.log

启动应用时,报错:


PermissionError: [Errno 13] Permission denied: '/app/application.log'

原因分析

在我的应用中,需要在容器中对application.log文件进行写入,这个文件被挂载到宿主机上。因为我的宿主机系统是CentOS,默认启用了SELinux。在SELinux策略下,容器进程的类型是container_t类型,而宿主机上的文件默认是user_home_t类型,二者类型不匹配,容器进程无法访问宿主机上挂载的文件。

解决方案

方案1,禁用SELinux,不推荐。

临时禁用SELinux方案如下:


sudo setenforce 0

方案2,在宿主机上修改application.log文件类型为svirt_sandbox_file_t


chcon -t svirt_sandbox_file_t application.log

  


如果需要永久修改application.log文件类型


semanage fcontext -a -t svirt_sandbox_file_t "application.log"

restorecon application.log

将文件类型修改成svirt_sandbox_file_t之后,因为docker容器进程是container_t类型,SELinux允许container_t类型的进程访问svirt_sandbox_file_t类型的文件。

方案3,挂载时使用:Z,这将把挂载的文件设置成container_file_t类型,确保容器进程可以访问挂载文件。更新后的yaml文件如下。(推荐)


version: '3'

services:

  purchasing-contract-consumer:

    image: my-registry.com/consumer:latest

    environment:

      - TZ=Asia/Shanghai

      - app_env=prod

    restart: always

    working_dir: /app

    command: python app.py

    volumes:

      -  /home/admin/deploy/consumer/application.log:/app/application.log:Z

运行后,查看SELinux上下文类型


[admin@myhost consumer]$ ls -lZ

-rw-rw-r--. admin admin system_u:object_r:container_file_t:s0:c716,c748 application.log

drwxr-xr-x. root  root  system_u:object_r:container_file_t:s0:c97,c362 config

-rwxr-xr-x. admin admin unconfined_u:object_r:user_home_t:s0 docker-compose.yml

-rw-rw-r--. admin admin unconfined_u:object_r:user_home_t:s0 qtsb-mail.tar

-rwxrwxr-x. admin admin unconfined_u:object_r:user_home_t:s0 start.sh

  


使用:Z挂载的文件类型是container_file_t,可以被容器进程访问。默认文件类型是user_home_t,无法被容器进程访问。

在使用方案3解决问题时,不能显示指定bing mount。如下


    volumes:

      - type: bind

        source: /home/admin/deploy/consumer/application.log

        target: /app/application.log:Z #无效,容器无法修改宿主机上application.log的SELinux类型

  


    volumes:

      -  /home/admin/deploy/consumer/application.log:/app/application.log:Z #有效,容器成功修改宿主机上application.log的SELinux类型

  



示例代码在Gitee上同步,你也可以访问曾彪彪的个人博客点击查看作者更多文章