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?

  • Docker project structure with submodules
  • Running docker and 64 bit linux VM on my host windows10
  • Unable to access MongoDB within a container within a Docker Machine instance from Windows
  • Files created in docker build not present in container created from image
  • docker-compose: difference between network and link
  • Can't resolve hostnames between docker containers
  • Docker copy file from container to host with file permissions & ownership intact
  • How to fix server error while pushing an image to the Docker hub?
  • How to connect to docker container in local machina [duplicate]
  • Can't access Docker images when using user namespace
  • docker-compose oci runtime error starting container process caused connection reset by peer
  • “docker cp” all files from a folder to existing container folder
  • 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.