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

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}"