Docker doesn't RUN command as USER

女生的网名这么多〃 提交于 2019-12-12 18:21:57

问题


I am new with Docker and I am having trouble writing my Dockerfile. The thing is that I created a user named jonas523 which I want to execute a shell command. So I put it in this Dockerfile and when I go inside my fresh container, I observe that the command was not used with the new user I created.

I think I didn't understand the USER instruction provided by Docker:

USER jonas523

RUN \
    echo "JAVA_HOME=/usr/java/jdk1.7.0_45/bin" >> /home/jonas523/.bash_profile \
    && echo "ANT_HOME=/opt/apache-ant-1.8.2/bin" >> /home/jonas523/.bash_profile \
    && echo "export JAVA_HOME" >> /home/jonas523/.bash_profile \
    && echo "export ANT_HOME" >> /home/jonas523/.bash_profile \
    && echo "PATH=$PATH:$HOME/bin:$JAVA_HOME:$ANT_HOME" >> /home/jonas523/.bash_profile \
    && echo "export PATH" >> /home/jonas523/.bash_profile

RUN source ~/.bash_profile

Could anyone tell me about using of the USER instruction. What is the difference between the RUN and CMD instructions?

Thanks.


回答1:


From the docs:

The USER instruction sets the user name or UID to use when running the image and for any following RUN directives.

It doesn't create the user, though. You need to have done that already (e.g. with an earlier RUN instruction.)

What is the difference between the "RUN" and "CMD" instructions ?

RUN is for progressively "layering up" a docker image to, say, install necessary dependencies (e.g. RUN apt-get install ....). You can have a number of RUN instructions in a Dockerfile, and they all get executed when you build your image (ignoring the build cache for simplicity).

CMD instructions do NOT get executed during docker build ..., but rather specifies a command you want the container to execute at docker run ... You can only have 1 CMD instruction in a Dockerfile.



来源:https://stackoverflow.com/questions/26504591/docker-doesnt-run-command-as-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!