Ben Whitmore
Ben Whitmore

Reputation: 967

Docker volume change owner to non-root

I want to create an uploads volume and set its owner to the node user. But upon running the container I find that the volume's owner is root. This is my Docker file:

FROM node:12.21
RUN apt-get update && apt-get -y install curl vim bash nano

WORKDIR /home/node/app

COPY package.json .
COPY yarn.lock .

RUN mkdir ./uploads

RUN chown -R node:node .
USER node

RUN yarn install

COPY --chown=node:node . .

VOLUME /home/node/app/uploads

I use docker-compose build then docker-compose up to build and run; my docker-compose.yml also contains a volume instruction:

services:
  ...
  server:
    ...
    volumes:
      - ./uploads:/home/node/app/uploads

My tests show that this instruction in docker-compose.yml is what's causing the problem -- without it the uploads directory owner is correctly set to node -- but I don't understand why. Is the instruction in docker-compose.yml redundant in this case? How about if I wanted to map the volume to a local directory (for which I believe this instruction would be necessary)?

Upvotes: 6

Views: 18624

Answers (1)

gohm'c
gohm'c

Reputation: 15500

Bind volume will retain the origin ownership on the host. You can either change mode the directory on the host to 77x, or you can try this way.

Update: Base on your feedback, you can add the chmod in your docker startup script.

Upvotes: 0

Related Questions