Docker Copy and change owner

后端 未结 2 1975
离开以前
离开以前 2021-02-02 04:47

Given the following Dockerfile

FROM ubuntu
RUN groupadd mygroup
RUN useradd -ms /bin/bash -G mygroup john
MKDIR /data
COPY test/ /data/test data
RUN chown -R joh         


        
相关标签:
2条回答
  • 2021-02-02 05:33

    I think I found a solution, which works. Using a data volume container will do the trick. First I create the Data Volume Container, which contains the copy of my external directory:

    FROM busybox
    RUN mkdir /data
    VOLUME /data
    COPY /test /data/test
    CMD /bin/sh
    

    In my application container, where I have my users, which could look something like this

    FROM ubuntu
    RUN groupadd mygroup
    RUN useradd -ms /bin/bash -G mygroup john
    COPY setpermissions.sh /root/setpermissions.sh
    CMD /root/setpermissions.sh && /bin/bash
    

    The setpermissions script does the job of setting the user permissions:

    #!/bin/bash
    
    if [ ! -e /data/.bootstrapped ] ; then
      chown -R john:mygroup /data
      touch /data/.bootstrapped
    fi
    

    Now I just have to use the --volumes-from <myDataContainerId> when running the application container.

    0 讨论(0)
  • 2021-02-02 05:34

    A --chown flag has finally been added to COPY:

    COPY --chown=patrick hostPath containerPath
    

    This new syntax seems to work on Docker 17.09.

    See the PR for more information.

    0 讨论(0)
提交回复
热议问题