linux - Docker 容器中的文件权限

标签 linux docker dockerfile file-permissions

我有一个简单的 tomcat docker 文件,如下所示。

FROM tomcat:7
MAINTAINER ***
COPY sample.war /opt/tomcat/webapps
USER tomcat
RUN chown -R tomcat:tomcat /tmp/
CMD ["catalina.sh", "run"]

当我创建 docker 镜像时,它会给我文件权限

tomcat tomcat  Dec 11 08:04 tmp

我的示例应用程序在 tmp 文件夹中创建了几个目录,我希望所有者是 tomcat,但它看起来像 root。由于我以用户 tomcat 身份运行容器,如何让它使用 tomcat 用户来创建这些容器。

最佳答案

我尝试构建并运行 Dockerfile您提供并遇到了多个错误。您正在询问您的“应用程序”创建的文件权限。所以这是我的出发点:

我假设“应用程序”是 catalina.sh .在 /tmp/ 中创建文件的过程.由于我们以用户身份运行容器 tomcat它会自动创建具有相应文件权限的文件。请查看下面的代码注释,以获取有关此处发生的事情的更多信息。

Dockerfile :

FROM httpd 
# I switched the image since you would need to configure tomcat to make it run
# httpd works out of the box without any application specific config. That's why

COPY ./catalina.sh /
RUN chmod +x /catalina.sh  # copy your 'app' / entrypoint into the image

RUN groupadd -r tomcat \
    && useradd -r -g tomcat tomcat
# create a new user and group under which the container runs
RUN chown -R tomcat:tomcat /tmp/  # change initial file permisisons. 
# So that our new user tomcat is allowed to access and fill the directory with files

USER tomcat  # switch the user under which the container runs

ENTRYPOINT ["/catalina.sh"]

catalina.sh :

#!/bin/bash

mkdir /tmp/test  # create a dir
touch /tmp/yolo.txt  # and some files
touch /tmp/test/yolo2.txt  # to check the file permissions later on

while true; do echo "sleep"; sleep 2; done
# endless loop so that the container doesn't exit

检查文件权限exec进入正在运行的容器。

docker exec -ti <container_name> bash

enter image description here

关于linux - Docker 容器中的文件权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47768553/

相关文章:

asp.net-core - 使用带有 DockerFile 的 Azure DevOps 设置网络核心应用程序的程序集版本

docker - 官方 Docker 镜像显示 docker 未运行?

c - 为什么这个程序不产生任何警告?

linux - 如何使用 Shell 脚本读取包含具有句点字符的键的 .properties 文件

docker - 是否可以使用Http通过Http连接Docker容器

spring-boot - 使用 CCDT + Spring Boot 在 docker 中使用 IBM MQ 的 MQRC_CLIENT_CONN_ERROR

docker - 有没有办法在 docker-compose.yml 文件中设置 Docker 容器的 mac 地址?

java - 如何在众多项目中指向我的项目中的一个 logback.xml 文件

linux - Android Studio 消耗了所有可用的内存

docker - 如何将 Docker 容器日志大小限制为预定义值