Reputation: 26139
I somehow don't like the RUN x && y && z ...
syntax we currently use in DockerFile. As far as I understand I could just run a shell script instead like RUN xyz.sh
and do the same tasks on my favorite language. Does the latter have any disadvantage?
Upvotes: 3
Views: 357
Reputation: 12089
Update:
In additional to the point made by David about the complexity, I believe writing everything to the Dockerfile makes it easier for share (thus creating a survivorship bias for you). Namely on the DockerHub, most of the time, you have a "Dockerfile" tab to quickly get the idea on how the image is built. If the author uses COPY
and RUN xyz.sh
, he/she would have to host the script elsewhere or the Dockerfile alone becomes meaningless.
CMD
is executed at runtime, that is when the container is created from the image. RUN
is a build time instruction. So the question is actually why people run things with RUN
instead of CMD
at runtime. (You can of course COPY script.sh /script.sh
then RUN bash /script.sh
)
If you do things like installing dependencies, it could take a lot of time, in case of scaling up your service, this would make auto-scaling useless because it can't be fast enough to absorb the peak.
At build time, RUN
can be cached, so next time the build will be a lot faster.
Because the way docker file system works, creating 10 containers from the same image takes only a few more space than creating 1 container. So you can save disk space by installing packages in the image, while if you install them at runtime, they will all occupy a part of disk space.
Upvotes: 8
Reputation: 6147
RUN executes commands in a new layer and creates a new image. This happens when you build the image using docker build.
CMD specifies a default command an parameters to be run when a container is launched from the image.
In summary. Run and cmd is not interchangeable, RUN runs when an image is created, CMD when a container is launched.
Upvotes: 1