Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker doesn't RUN command as USER

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.

like image 364
MadJlzz Avatar asked Mar 18 '23 03:03

MadJlzz


1 Answers

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.

like image 72
John Avatar answered Mar 31 '23 08:03

John