Search code examples
dockerpermissionsvolumes

Creating/generating files in a mounted volume on docker container own to regular user and not to root


The Problem: can't generate/create files on mounted volumes in docker that is not own by root. Throwing permission denied errors when I create and push my user to docker-compose as well.

Scenario:

version: '3.4'
services:
  app:
    image: ubuntu:latest
    command: /home/john/clone_repository_and_generate_reports.sh 
    volumes:
      - "/home/john/:/home/john/"
    environment:
      - NODE_ENV=production
    env_file:
      - ./.config.env
  • My docker-compose.yml file calls to simple bash script that clones a github repository and generate different reports in python, In addition I want this data to be accessible and persistent to user john, unfortunately the the saved files are on root only.

  • If Im adding on Dockerfile additional user RUN addgroup -S developer && adduser -S john -G developer It gets permissions issue accessing to this mounted volume with user: john attribute on docker-compose.yml file

Expected behaviour: cloned github repository and generated files owned by john and not by root.

What is the right approach dealing with permission issue on docker? How can I save these files under john user on this mounted volume?


Solution

  • Solution: We need to run docker script on user scope and push the user attribute with user id and group id convention ("uid:gid" ).

    example: run command in specific user sudo -iu john and run docker-compose function:USER_INFO=$(id -u):$(id -g) docker-compose up --build --force-recreate -d

    compose file structure:

    version: '3.4'
    services:
      app:
        image: ubuntu:latest
        command: /home/john/clone_repository_and_generate_reports.sh 
        volumes:
          - "/home/john/:/home/john/"
        environment:
          - NODE_ENV=production
        env_file:
          - ./.config.env
        user: "${USER_INFO}"