Dockerfile: $HOME is not working with ADD/COPY instructions

后端 未结 3 811
星月不相逢
星月不相逢 2020-12-13 13:05

Before filing a bug I would like to ask someone to confirm the weird docker build behavior I have recently faced with.

Consider we have a simple Docker

3条回答
  •  借酒劲吻你
    2020-12-13 13:30

    Here's your problem:

    When you use the USER directive, it affects the userid used to start new commands inside the container. So, for example, if you do this:

    FROM ubuntu:utopic
    RUN useradd -m aptly
    USER aptly
    RUN echo $HOME
    

    You get this:

    Step 4 : RUN echo $HOME
     ---> Running in a5111bedf057
    /home/aptly
    

    Because the RUN commands starts a new shell inside a container, which is modified by the preceding USER directive.

    When you use the COPY directive, you are not starting a process inside the container, and Docker has no way of knowing what (if any) environment variables would be exposed by a shell.

    Your best bet is to either set ENV HOME /home/aptly in your Dockerfile, which will work, or stage your files into a temporary location and then:

    RUN cp /skeleton/myfile $HOME/myfile
    

    Also, remember that when you COPY files in they will be owned by root; you will need to explicitly chown them to the appropriate user.

提交回复
热议问题