Multiple RUN instructions vs. single CMD to execute setup script in Dockerfile to initialize container

What the is best practice for using many RUN commands in a Dockerfile to setup the image vs. a single CMD instruction to execute a script when the container starts?

For example:

  • File locks support in Docker volumes of NFS4 shares
  • Docker: Multiple MySQL instances/containers - second slave time out
  • invalid header field value "oci runtime error while running docker image
  • Kitchen doesn't recognise docker
  • How does one remove an image in Docker?
  • Run gitlab runner in debug mode doesnt work
  • 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"]
    

  • How to kill networking to a docker container?
  • PhpStorm 2017.1 remote php-cli in docker: Configuration php.ini file does not exist
  • How to create a copy of exisiting docker image
  • In Influxdb, I cannot retrieve correct values in time range
  • How to set up a simple docker-contained reverse-proxying (nginx) server?
  • Prevent Docker-Engine restart during apt-get upgrade
  • One Solution collect form web for “Multiple RUN instructions vs. single CMD to execute setup script in Dockerfile to initialize container”

    The first example will be better for running app multiple times. If you were to use ENTRYPOINT instead of CMD then you could make the container behave as if it was app (because it would run app with anything you passed in on the commandline as arguments). If you later wanted to use this image as a base for another image, you could still access app.

    The second example will run a bit slower since it must extract the zip file each time you docker run and you cannot use app easily if you make this a parent image, since app hasn’t been installed.

    In general I would suggest using the first format, but the second one is fine for trivial zips and throwaway images and containers.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.