如何在 docker 中掛載磁碟區以使檔案不歸 root 所有?

如何在 docker 中掛載磁碟區以使檔案不歸 root 所有?

我的 Linux 電腦(Ubuntu 23.10)上有一個目錄,名稱為:

/home/steve/foobar/foo

此目錄及其中的所有檔案均由使用者 steve (uid 1000) 和群組 steve (gid 1000) 擁有。

我想將其安裝在 docker 內作為 /host/foo

無論我如何嘗試,docker 內的所有檔案最終都由 root 和 root 群組擁有。我希望它們由使用者 steve 和群組 steve 擁有。

這是 compose.yaml:

services:
  pyapp:
    hostname: pytest
    container_name: pytest1
    command: 'sleep infinity'
    volumes:
        - /home/steve/foobar/foo:/host/foo
    build:
      dockerfile: testapp.dockerfile

 

這是我的 testapp.dockerfile

FROM ubuntu:jammy

ARG UID=1000
RUN adduser \
    --disabled-password \
    --gecos "" \
    --home "/home/steve" \
    --shell "/bin/bash" \
    --uid "${UID}" \
    steve

RUN mkdir -p /host/foo
RUN chown steve: /host
RUN chown steve: /host/foo

USER steve
VOLUME /host/foo

為了建構我做:

docker compose build

要運行我這樣做:

docker compose run

為了在 docker 中取得命令列,我這樣做:

docker exec -it pytest1 /bin/bash    

我需要做哪些具體更改才能使 /host/foo 下的所有檔案都歸 steve 和一組 steve 所有?

謝謝。

(如果重要的話我也在運行 docker 桌面)。

答案1

您需要將以下內容新增至您的 docker-compose 中

user: "1000:1000"

所以在你的情況下

services:
  pyapp:
    hostname: pytest
    container_name: pytest1
    command: 'sleep infinity'
    user: "1000:1000"
    volumes:
        - /home/steve/foobar/foo:/host/foo
    build:
      dockerfile: testapp.dockerfile

相關內容