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:

  • Meteor build locally or on aws host
  • Cannot run ruby commands while running Docker
  • Gitlab, docker and sendmail ports
  • Rails can't find a temporary directory when run inside Docker container
  • Using Docker Environment Variable inside of Java
  • Docker for Mac: Using Persistent Storage
  • FROM  centos:latest
    RUN useradd myuser
    RUN mkdir -p /usr/local/myapp
    ADD ./resources/ /usr/local/myapp
    RUN unzip /usr/local/myapp/
    RUN chown -R myuser:myuser /usr/local/myapp
    CMD ["/usr/local/myapp/bin/app"]


    FROM  centos:latest
    ADD ./resources/ /
    ADD ./resources/ / 
    RUN chmod +x /
    # will create the user, extract the zip, execute the binary
    CMD ["/"]

  • Path is not writable within container
  • Docker Compose stuck downloading or pulling fs layer
  • an established connection was aborted by the software in your host machine Docker
  • Accessing a server via script, both running inside the same Docker container
  • Why is MariaDB data persistent in my Docker container? I don't have any volumes
  • Docker shows no containers or images after system reboot
  • 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.