title: How to modify volume files with host user inside container when using docker-compose
body: <p>I have a docker container managed by docker-compose that on startup will run a build script to add a bunch of files onto a volume mapped to a host folder, since the container is always running as root it keeps creating files which are root in the parent folder, how can I set docker-compose such that files created will be by the same user as the user who ran docker-compose up on the host file system?</p>
  
  <p>docker-compose file</p>
  
  <pre><code>version: "2"
  services:
  
    # Data API and authentication/authorization
    api:
      build:
        context: ../api/
        dockerfile: Dockerfile.dev
      hostname: api
      depends_on:
        - db
        - redis
      environment:
        - CORS_ORIGIN=http://localhost:3000,http://localhost:3001
        - DATABASE_URL=postgres://postgres@db:5432/dev
        - DATABASE_DEBUG=false
        - REDIS_URL=redis://redis:6379/0
        - SESSION_SECRET=wZjwhFtzQsd7r87W6AZw45Sm
        - FACEBOOK_ID=1821424564802638
        - FACEBOOK_SECRET=2339bdf25f236a42fc3a18280bf455e8
        - GOOGLE_ID=xxxxx.apps.googleusercontent.com
        - GOOGLE_SECRET=xxxxx
        - TWITTER_KEY=xxxxx
        - TWITTER_SECRET=xxxxx
      ports:
        - "8080:8080"
        - "127.0.0.1:9229:9229" # V8 inspector for tools/run.js
        - "127.0.0.1:9230:9230" # V8 inspector for src/server.js
      volumes:
        - yarn:/home/node/.cache/yarn
        - ../api/:/usr/src/app
      command: node tools/run.js # or, `node --inspect=0.0.0.0:9229 tools/run.js`
  
    # SQL and document data store
    db:
      image: postgres:9.6.5-alpine
      read_only: true
      tmpfs:
        - /tmp
        - /var/run/postgresql
      volumes:
        - db:/var/lib/postgresql/data
        - ./postgres-initdb.sh:/docker-entrypoint-initdb.d/initdb.sh
      # ports:
      #   - "127.0.0.1:5432:5432" # you can override it via docker-compose.override.yml
  
    # Distributed in-memory cache
    redis:
      image: redis:4.0.2-alpine
      read_only: true
      volumes:
        - redis:/data
  
  volumes:
    db:
    redis:
    yarn:
  </code></pre>
  
  <p>dockerfile.dev on api directory</p>
  
  <pre><code>FROM node:8.6.0-alpine
  
  # Set a working directory
  WORKDIR /usr/src/app
  
  # If you have native dependencies, you'll need extra tools
  RUN apk add --no-cache make g++ python2 libsodium-dev &amp;&amp; \
    npm install -g node-gyp &amp;&amp; \
    mkdir -p /home/node/.cache/yarn &amp;&amp; \
    chown -R node:node /home/node/.cache/yarn &amp;&amp; \
    chmod 777 /home/node/.cache/yarn
  
  VOLUME /home/node/.cache/yarn
  </code></pre>