简体   繁体   English

Docker容器中的用户和文件权限配置(docker-compose版本3)

[英]User and file permission configuration in Docker containers (docker-compose version 3)

Docker states that its containers are 码头工人 ,它的容器

"...by default, quite secure; especially if you take care of running your processes inside the containers as non-privileged users (ie, non-root)" “...默认情况下,非常安全;特别是如果您负责将容器内的进程作为非特权用户(即非root用户)运行”

As a result, I've tried to: 结果,我试图:

  1. Use official images that handle user execution appropriately 使用能够正确处理用户执行的官方图像
  2. Avoid mounting host volumes in production, and using COPY instead 避免在生产中安装主机卷,而是使用COPY

When I COPY code and configuration files to my container volumes, all of the directories and files are created "...with a UID and GID of 0" (root ownership), as stated in the documentation here . 当我COPY代码和配置文件到我的集装箱量,所有的目录和文件的创建” ......一个UID和0"的GID(根所有权),如文档中说明这里

Question: Are there still security risks if the files and directories are owned by root but the running process is owned by a non-root user, like www-data ? 问题:如果文件和目录由root拥有但是运行进程由非root用户拥有,如www-data ,是否还存在安全风险? For example, with php-fpm : 例如,使用php-fpm

root@7bf71145c18c:/var/www/html# ls -l
total 1220
-rw-rw-r-- 1 root root    5931 May 10 12:28 index.php
drwxrwxr-x 3 root root    4096 May 10 12:28 logs
-rw-rw-r-- 1 root root      28 May 10 12:28 robots.txt

root@7bf71145c18c:/var/www/html# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 14:14 ?        00:00:00 php-fpm: master process (/usr/lo
www-data     5     1  0 14:14 ?        00:00:00 php-fpm: pool www
www-data     6     1  0 14:14 ?        00:00:00 php-fpm: pool www
root         7     0  0 14:19 ?        00:00:00 bash
root        12     7  0 14:19 ?        00:00:00 ps -ef

If so, I'm wondering how (and where) to correctly chown and chmod my files. 如果是这样,我想知道如何(和在哪里)正确chownchmod我的文件。 From my research, this seems less straightforward if you're using docker-compose, which I am - I'm using both a docker-compose YAML file (version 3) in conjunction with container-specific Dockerfiles. 从我的研究来看,如果你使用的是docker-compose,这似乎不太直白,我就是这样 - 我正在使用docker-compose YAML文件(版本3)和特定于容器的Dockerfiles。 It seems that when I chown at the end of a container's Dockerfile, the changes don't go into effect - probably because the volume (which is defined in docker-compose.yml) is created afterwards. 看来,当我chown在容器的Dockerfile结束,所做的更改不会生效-可能是因为体积(这是在泊坞窗,compose.yml定义)是继创建。 Here's some snippets of both configurations: 以下是两种配置的一些片段:

docker-compose.yml: 泊坞窗,compose.yml:

version: '3'
services:
  fpm:
    build: ./fpm
    container_name: "fpm"
    volumes:
      - data_volume:/var/www/html
    ports:
      - "9000"
    restart: always
volumes:
  data_volume:

./fpm Dockerfile: ./fpm Dockerfile:

FROM php:7-fpm
RUN mkdir -p /var/www/html/
COPY . /var/www/html/
RUN find /var -exec chown www-data:www-data {} \;
RUN find /var -type d -exec chmod 755 {} \;
RUN find /var -type f -exec chmod 644 {} \;

With the different docker-compose versions, it's hard to sort through the various web articles, bug fixes, and feature requests surrounding this topic. 使用不同的docker-compose版本,很难对围绕此主题的各种Web文章,错误修复和功能请求进行排序。 I'm hoping to get some definitive direction/answers here, geared toward docker-compose version 3. 我希望在这里获得一些明确的方向/答案,面向docker-compose第3版。

Question: Are there still security risks if the files and directories are owned by root but the running process is owned by a non-root user, like www-data? 问题:如果文件和目录由root拥有但是运行进程由非root用户拥有,如www-data,是否还存在安全风险?

Not really. 并不是的。 You might just need to make sure that php-fpm has read permission to these files. 您可能只需要确保php-fpm具有对这些文件的读取权限。

You are copying the contents of /var/www/html into the image and setting ownership/permissions at build time (Dockerfile). 您正在将/var/www/html的内容复制到映像中,并在构建时设置所有权/权限(Dockerfile)。 This is OK. 还行吧。 That's the usual use case for creating images. 这是创建图像的常用用例。

But your docker-compose.yml mounts data_volume into the running container, replacing the /var/www/html from the image. 但是,你的docker-compose.yml坐骑data_volume到正在运行的容器,替换/var/www/html从图像。 So, whatever you had in that directory will be hidden. 因此,无论您在该目录中拥有什么,都将被隐藏。 You will see the contents of the mounted volume, instead. 您将看到已安装卷的内容。

You might want to choose what strategy you need to persist container data. 您可能希望选择持久化容器数据所需的策略。 Populating /var/www/html at build time is probably OK for most of the time. 在构建时/var/www/html在大多数情况下可能都可以。 But, if your application writes data somewhere in that directory, then you might consider changing that path. 但是,如果您的应用程序将数据写入该目录中的某个位置,那么您可以考虑更改该路径。 When the container is destroyed, any data written to outisde mounted volumes will be lost. 当容器被销毁时,写入outisde已安装卷的任何数据都将丢失。 So, make sure yoru app writes to a directory that is mounted as a volume from docker-compose.yml . 因此,请确保yoru app写入从docker-compose.yml作为卷挂载的目录。

Named volumes in docker are initialized to the contents of the image at their mount point. docker中的命名卷初始化为其挂载点处图像的内容。 After that, unless the volume is completely empty, that initialization step is never run again to avoid data loss. 之后,除非卷完全为空,否则永远不会再次运行该初始化步骤以避免数据丢失。

So when you first created data_volume pointing to /var/www/html , it got a copy of that directory, including the file permissions. 因此,当您第一次创建指向/var/www/html data_volume时,它获得了该目录的副本,包括文件权限。 But unless you delete or empty the data_volume , any changes you make to the Dockerfile will only update the image and the volume will overlay that directory with the contents of the volume. 但除非您删除或清空data_volume ,否则您对data_volume任何更改都只会更新映像,而卷将使用卷的内容覆盖该目录。

If you don't need the contents of data_volume , you can docker-compose down -v to both remove the container and the volumes. 如果您不需要data_volume的内容,可以使用data_volume docker-compose down -v来删除容器卷。 Then when you run docker-compose up -d again, the volume will be created with the files with the new permissions. 然后,当您再次运行docker-compose up -d时,将使用具有新权限的文件创建卷。

If you do need the contents of data_volume to be preserved, then you can mount the volume and run the commands on the volume itself: 如果确实需要保留data_volume的内容,则可以装入卷并在卷本身上运行命令:

docker run -it --rm -v $(basename $(pwd))_data_volume:/var/www/html busybox

The above assumes you are in the same folder as your docker-compose.yml and the directory is all lower case characters. 以上假设您与docker-compose.yml位于同一文件夹中,并且该目录均为小写字符。 Otherwise, replace $(basename $(pwd))_data_volume with the volume name shown in docker volume ls . 否则,将$(basename $(pwd))_data_volume替换$(basename $(pwd))_data_volume docker volume ls显示的卷名。 From inside the above container, you can run your find commands to update ownership and permissions. 从上面的容器中,您可以运行find命令来更新所有权和权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM