Docker 用户权限说明

Docker user permissions explanation

我有一个由 docker-compose 命令创建的 LEMP 堆栈:

cadvisor:
  image: google/cadvisor:latest
  container_name: lemp_cadvisor
  ports:
    - "8082:8080"
  volumes:
    - "/:/rootfs:ro"
    - "/var/run:/var/run:rw"
    - "/sys:/sys:ro"
    - "/var/lib/docker/:/var/lib/docker:ro"
ssh:
  build: ./ssh
  container_name: lemp_ssh
  ports:
    - "2222:22"
  volumes:
    - /home/core/www/:/var/www/:rw
    - /home/core/.ssh/:/root/.ssh/:ro
phpmyadmin:
  build: ./phpmyadmin
  container_name: lemp_phpmyadmin
  links:
    - ssh
  volumes:
    - /var/www/phpmyadmin
    - ./phpmyadmin/var/www/phpmyadmin/config.inc.php:/var/www/phpmyadmin/config.inc.php:ro
mariadb:
  build: ./mariadb
  container_name: lemp_mariadb
  env_file: ./mariadb/mariadb.env
  links:
    - ssh
  volumes:
    - /var/run/mysqld
    - /home/core/mysql/:/var/lib/mysql/:rw
    - ./mariadb/etc/mysql/my.cnf:/etc/mysql/my.cnf:ro
php:
  build: ./php
  container_name: lemp_php
  links:
    - ssh
  volumes:
    - /var/run/php-fpm
    - ./php/usr/local/php7/etc/php-fpm.conf:/usr/local/php7/etc/php-fpm.conf:ro
    - ./php/usr/local/php7/etc/php.ini:/usr/local/php7/etc/php.ini:ro
    - ./php/usr/local/php7/etc/php-fpm.d/www.conf:/usr/local/php7/etc/php-fpm.d/www.conf:ro
  volumes_from:
    - ssh
    - mariadb
    - phpmyadmin
nginx:
  build: ./nginx
  container_name: lemp_nginx
  links:
    - ssh
  ports:
    - "8080:80"
    - "8081:443"
  volumes:
    - ./nginx/etc/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
    - ./nginx/etc/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf:ro
    - ./nginx/etc/nginx/conf.d/php.conf:/etc/nginx/conf.d/php.conf:ro
    - ./nginx/etc/nginx/conf.d/cert/:/etc/nginx/conf.d/cert/:ro
  volumes_from:
    - php

我将我的 Wordpress 文件存储在主机上的 /home/core/www/my-wordpress-install-dir 文件夹中。

从CoreOS主机SSH会话查看文件,文件夹权限如下:

core@core-01 ~/www/my-wordpress-install-dir $ ls -al
total 272
drwxrwxrwx  5 core core  4096 Dec 16 14:36 .
drwxr-xr-x  4 core core  4096 Dec 13 23:54 ..
-rw-r--r--  1 core core   418 Dec 13 23:09 index.php
-rw-r--r--  1 core core 19930 Dec 13 23:09 license.txt
-rw-r--r--  1 core core  7358 Dec 13 23:09 readme.html
-rw-r--r--  1 core core  5035 Dec 13 23:09 wp-activate.php
drwxr-xr-x  9 core core  4096 Dec 13 23:09 wp-admin
-rw-r--r--  1 core core   271 Dec 13 23:09 wp-blog-header.php
-rw-r--r--  1 core core  1369 Dec 13 23:09 wp-comments-post.php
-rw-r--r--  1 core core  2764 Dec 13 23:09 wp-config-sample.php
-rw-rw-rw-  1   33   33  3216 Dec 16 14:36 wp-config.php
drwxr-xr-x  4 core core  4096 Dec 13 23:09 wp-content
-rw-r--r--  1 core core  3286 Dec 13 23:09 wp-cron.php
drwxr-xr-x 16 core core  4096 Dec 13 23:09 wp-includes
-rw-r--r--  1 core core  2380 Dec 13 23:09 wp-links-opml.php
-rw-r--r--  1 core core  3316 Dec 13 23:09 wp-load.php
-rw-r--r--  1 core core 33710 Dec 13 23:09 wp-login.php
-rw-r--r--  1 core core  7887 Dec 13 23:09 wp-mail.php
-rw-r--r--  1 core core 13021 Dec 13 23:09 wp-settings.php
-rw-r--r--  1 core core 28594 Dec 13 23:09 wp-signup.php
-rw-r--r--  1 core core  4035 Dec 13 23:09 wp-trackback.php
-rw-r--r--  1 core core  3061 Dec 13 23:09 xmlrpc.php

但是,如果我 docker 执行到 lemp_ssh 容器中,则权限如下:

root@15a6b53835e4:/var/www/my-wordpress-install-dir# ls -al
total 272
drwxrwxrwx.  5      500      500  4096 Dec 16 14:36 .
drwxr-xr-x.  4      500      500  4096 Dec 13 23:54 ..
-rw-r--r--.  1      500      500   418 Dec 13 23:09 index.php
-rw-r--r--.  1      500      500 19930 Dec 13 23:09 license.txt
-rw-r--r--.  1      500      500  7358 Dec 13 23:09 readme.html
-rw-r--r--.  1      500      500  5035 Dec 13 23:09 wp-activate.php
drwxr-xr-x.  9      500      500  4096 Dec 13 23:09 wp-admin
-rw-r--r--.  1      500      500   271 Dec 13 23:09 wp-blog-header.php
-rw-r--r--.  1      500      500  1369 Dec 13 23:09 wp-comments-post.php
-rw-r--r--.  1      500      500  2764 Dec 13 23:09 wp-config-sample.php
-rw-rw-rw-.  1 www-data www-data  3216 Dec 16 14:36 wp-config.php
drwxr-xr-x.  4      500      500  4096 Dec 13 23:09 wp-content
-rw-r--r--.  1      500      500  3286 Dec 13 23:09 wp-cron.php
drwxr-xr-x. 16      500      500  4096 Dec 13 23:09 wp-includes
-rw-r--r--.  1      500      500  2380 Dec 13 23:09 wp-links-opml.php
-rw-r--r--.  1      500      500  3316 Dec 13 23:09 wp-load.php
-rw-r--r--.  1      500      500 33710 Dec 13 23:09 wp-login.php
-rw-r--r--.  1      500      500  7887 Dec 13 23:09 wp-mail.php
-rw-r--r--.  1      500      500 13021 Dec 13 23:09 wp-settings.php
-rw-r--r--.  1      500      500 28594 Dec 13 23:09 wp-signup.php
-rw-r--r--.  1      500      500  4035 Dec 13 23:09 wp-trackback.php
-rw-r--r--.  1      500      500  3061 Dec 13 23:09 xmlrpc.php

我在主机上进行了一些更改后复制回 wp-config.php。我的此权限问题可能是我的 SSH2 会话无法在 Wordpress 管理员中上传文件的原因。

我将此添加到我的 wp-config.php:

define('FTP_PUBKEY','/root/.ssh/wp_rsa.pub');
define('FTP_PRIKEY','/root/.ssh/wp_rsa');
define('FTP_USER','www-data');
define('FTP_PASS','');
define('FTP_HOST','10.0.2.2:22');

如果我在主机和容器中查看它们,请解释为什么文件权限如下。谢谢!

权限(第一列)对我来说看起来是一样的(除了末尾的额外点,这很可能只是主机和容器之间 ls 二进制文件的差异)。

区别在于文件的所有者和组。所有者和组存储为 ID,名称在 /etc/group/etc/passwd 中查找。如果系统在这些文件中找不到 id 的名称,它会使用该编号。

在您的情况下,主机的名称为 UID/GID 500(即 core),但它没有 UID/GID 33 的名称。在容器中它有相反的。这是因为我提到的文件在主机和容器之间没有共享。

我想我解决了。谢谢@dnephin 的解释!

当您将整个 Wordpress 从主机共享到带有卷的容器时,我遇到了此类 Wordpress 安装的权限问题。

在我的堆栈中,我有一个基本映像,它是一个经过我基本修改的 debian,所有其他映像都将从该映像构建。

在基本图像中,我有这部分:

### Start of Nginx WEBSERVER setup
RUN mkdir -p /var/www
# Modify www-data user and set UID, GID to 500
# https://muffinresearch.co.uk/linux-changing-uids-and-gids-for-user/
RUN groupmod -g 500 www-data \
    && usermod -u 500 www-data \
    #&& `find / -user 33 -exec chown -h 500 {} \;` \
    #&& `find / -group 33 -exec chgrp -h 500 {} \;` \
    && usermod -g 500 www-data \
    && chown -R www-data:www-data /var/www \
    && chmod g+s /var/www
### End of Nginx WEBSERVER setup

www-data 不是由 php 或 nginx 安装创建的。它是 Debian 和其他发行版中默认定义的 user/group。一些 PHP、Nginx 安装建议在其配置文件中使用此用户。

如果您的主机用户的 UID/GID 是 500,那么此脚本会将 www-data 用户的 UID/GID 从 33 更改为 500。这样,如果您从主机共享任何内容,Docker 认为那些文件和文件夹属于 www-data 用户。

在您的 PHP-FPM 设置文件中,也将用户和组设置为 www-data

在您的 nginx Docker文件中,您还必须设置:

# Allow Nginx to access /var/run/php-fpm/php-fpm.sock
RUN usermod -aG www-data nginx

这样 nginx 用户可以访问 www-data 拥有的文件(您可以在 nginx 配置文件中定义 nginx 的用户名)。

这次破解后,我的 Wordpress 安装没有任何权限问题。所有文件都驻留在主机上 + 更新 Wordpress 工作完美。

我的 LEMP 堆栈:https://github.com/DJviolin/LEMP