我有一个简单的 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
关于linux - Docker 容器中的文件权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47768553/