루트가 파일을 소유하지 않도록 docker에 볼륨을 어떻게 마운트합니까?

루트가 파일을 소유하지 않도록 docker에 볼륨을 어떻게 마운트합니까?

내 Linux 컴퓨터(Ubuntu 23.10)에 다음과 같은 디렉터리가 있습니다.

/home/steve/foobar/foo

디렉터리와 그 안의 모든 파일은 사용자 steve(uid 1000)와 그룹 steve(gid 1000)가 소유합니다.

docker 내부에 /host/foo로 마운트하고 싶습니다.

내가 무엇을 시도하든 docker 내부의 모든 파일은 결국 그룹 루트가 있는 루트의 소유가 됩니다. 나는 그것들을 사용자 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

관련 정보