Multiple RUN commands against a single CMD to complete a script installation in a Docker file to initialize a container

What is the best practice for using many RUN commands in a Docker file to configure an image compared to a single CMD instruction to run a script when the container starts?

For instance:

FROM centos:latest RUN useradd myuser RUN mkdir -p /usr/local/myapp ADD ./resources/myapp.zip /usr/local/myapp RUN unzip /usr/local/myapp/myapp.zip RUN chown -R myuser:myuser /usr/local/myapp CMD ["/usr/local/myapp/bin/app"] 

vs.

 FROM centos:latest ADD ./resources/myapp.zip / ADD ./resources/setup.sh / RUN chmod +x /setup.sh # setup.sh will create the user, extract the zip, execute the binary CMD ["/setup.sh"] 
+6
source share
1 answer

The first example would be better for launching the app several times. If you used ENTRYPOINT instead of CMD , you could make the container behave as if it were an app (because it would launch the app with something that you passed as arguments to the command line). If you later want to use this image as the basis for another image, you can access the app .

The second example will work a little slower, since it has to extract the zip file every time you docker run , and you cannot easily use the app if you make it the parent image since the app not been installed.

In general, I would suggest using the first format, but the second option is suitable for trivial zippers and images and dump containers.

+5
source

All Articles