【发布时间】:2020-03-31 12:03:03
【问题描述】:
背景
我们正在对具有应用程序用户(我们称之为 appuser)的产品进行 docker 化,应用程序使用的所有文件和目录都归 appuser 所有。
在 dockerized 环境中,docker 以 appuser(非 root)身份运行。一些文件存在于 linux 主机上,它们绑定安装到 docker 容器。所有绑定安装的位置都归主机上的 appuser 所有。
问题
现在,问题是主机上的 appuser 和容器上的 appuser 不会有相同的 UID,所以当我们尝试访问绑定挂载的文件时会遇到权限错误.
解决方案?
我看到了解决这个问题的两种方法。
1. 使容器上appuser的UID与宿主机上的UID一致。即更新 docker entryscript 上的 UID。
2. 以root用户运行容器。
处理这种情况的理想方法是什么?我们应该更喜欢以 root 身份运行容器吗?
编辑 1
我们使用 docker-compose 并在容器之间共享 docker 卷,这些卷归 appuser 所有。更新 appuser 的 UID 会影响访问 docker 卷。
目前我们正在使用上面的方法 1 并将 777 设置为 docker 卷。
编辑 2
显示所描述问题的示例。
码头工人撰写
---
version: "3.4"
services:
datacontainer:
image: datacontainer
container_name: datacontainer
volumes:
- type: volume
source: commonJars
target: /opt/company/product/java/lib
product-server:
image: product-server
hostname: product-server
container_name: product-server
depends_on:
- datacontainer
environment:
APPUSER_UID:
APPUSER_GID:
volumes:
- type: volume
source: commonJars
target: /opt/company/product/java/lib
- type: volume
source: install
target: /opt/company/product/install
- type: volume
source: product_temp
target: /var/opt/company/product/tmp
- type: bind
source: /etc/opt/company/product
target: /etc/opt/company/product
- type: bind
source: /var/opt/company/product/content-repo
target: /var/opt/company/product/content-repo
ports:
- "80:80"
- "443:443"
- "8000:8000"
product-loader:
image: product-loader
hostname: product-loader
container_name: product-loader
depends_on:
- datacontainer
environment:
APPUSER_UID:
APPUSER_GID:
volumes:
- type: volume
source: commonJars
target: /opt/company/product/java/lib
- type: volume
source: install
target: /opt/company/product/install
- type: volume
source: product_temp
target: /var/opt/company/product/tmp
- type: bind
source: /etc/opt/company/product
target: /etc/opt/company/product
- type: bind
source: /var/opt/company/product/content-repo
target: /var/opt/company/product/content-repo
volumes:
commonJars:
name: commonJars
install:
name: install
product_temp:
name: product_temp
服务器入口脚本
#!/bin/bash
if [ -n "$APPUSER_GID" ]; then
groupmod -g ${APPUSER_UID} appuser
fi
if [ -n "$APPUSER_UID" ]; then
usermod -u ${APPUSER_UID} appuser
fi
chmod 755 /opt/company/product/bin/server-startup.sh
#script to start tomcat using jsvc as appuser
/opt/company/product/bin/server-startup.sh debug
【问题讨论】:
-
一种解决方法是使两个 UID 相同(不知道是不是唯一一个)。您永远不应该在生产环境中以 root 身份运行容器(出于安全原因)。
-
这是我们目前采用的解决方法,只是感觉不对。
-
在 Linux 中,在权限方面,一切都与数字有关。唯一重要的是 UID 和 GID。也许感觉不对,但我还没有遇到其他解决方案。
-
不仅仅是这样。更新 UID 会影响对 docker 卷上文件的访问,因为它们归容器用户所有。
标签: docker