How do I Docker COPY as non root?

While building a Docker image, how do I COPY a file into the image so that the resulting file is owned by a user other than root?

  • How to deploy many instances of the same Docker image with unique database connection string
  • How to install pdo driver in php docker image?
  • Requests hanging on ports forwarded from docker container inside vagrant
  • What does the task hierarchy mean in 'docker node ps' output?
  • Load balancing express app instances
  • Location of tensorflow/models.. in Windows
  • Unable to start mongodb using supervisor file
  • Identical dockerfiles giving different behaviours
  • docker + apt-add-repository: 'ascii' codec can't decode byte
  • How to create a Docker container of an AngularJS app?
  • Docker CMD to start Haproxy in Dockerfile
  • why different output from zabbix_get and zabbix_agentd command?
  • One Solution collect form web for “How do I Docker COPY as non root?”

    Docker currently doesn’t support COPY as a user other than root. You need to chown / chmod the file after the COPY command.

    Example Dockerfile:

    from centos:6
    RUN groupadd -r slcs && adduser -r -g myuser myuser
    USER myuser
    #Install code, configure application, etc...
    USER root
    COPY run-my-app.sh /usr/local/bin/run-my-app.sh
    RUN chown myuser:myuser /usr/local/bin/run-my-app.sh && \
        chmod 744 /usr/local/bin/run-my-app.sh
    USER myuser
    ENTRYPOINT ["/usr/local/bin/run-my-app.sh"]
    

    I’ve been trying to follow the discussion through multiple GitHub issues: 6119, 9943, 13600, 27303, 28499

    At the time of this post, Issue 30110 seems to be the most current which has no resolution. Therefore, it seems it is still not possible to do this with regular docker commands.

    Also, the Dockerfile Reference for the COPY command still says that

    All new files and directories are created with a UID and GID of 0.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.