簡體   English   中英

Docker容器中的文件權限

[英]File permissions in Docker Container

我有一個簡單的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 ,並遇到多個錯誤。 您正在詢問“應用”創建的文件許可權。 所以這是我的出發點:

我假設“ app”是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

在此處輸入圖片說明

Docker通常以root特權運行,因此我相信您必須創建一個docker組(如果不存在),並將用戶(在您的情況下為tomcat)添加到docker組。

請參閱以下如何將用戶添加到Docker組:

創建泊塢窗組。

$ sudo groupadd docker

將您的用戶添加到docker組。

$ sudo usermod -aG docker tomcat

參考: https : //docs.docker.com/engine/installation/linux/linux-postinstall/#manage-docker-as-a-non-root-user

這樣,我相信您的問題可以解決。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM