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 - Exposing container directories to host directories without obscuring original contents
  • Raspberry-pi docker error: standard_init_linux.go:178: exec user process caused “exec format error”
  • Dockerfile with multiple base image
  • Docker installation issues on Windows 10 Home Edition
  • Creation of Dockerfile for Android GitLab CI
  • Docker: communication between web container and php container
  • Docker swarm mode: scale down a node and remove services
  • Docker CE on Mac OSX hanging on startup
  • Java Spring Hibernate application runs on local, but not in aws's ecs docker
  • How can I write Dockerfile for Yesod? “RUN yesod init -n myApp -d postgresql” didn't work as expected
  • docker start a container after stopping
  • How can I add a volume to an existing Docker container?
  • 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.