Dockerfile Reference – RUN & CMD Instructions
Hope you’re doing well and utilizing time to learn new things. In this series we’re learning different instructions for writing Dockerfile. In this article we’ll understand RUN and CMD instructions. To read other aspects of Docker and Containerisation please check index page.
RUN Instruction :
Run is one of the basic instruction which you’ll require to setup your environment in image. RUN has two different forms,
# Shell form, the command gets executed in shell, which by default is /bin/sh -c for linux and cmd /S /C for windows RUN <command> # exec form, where you can provide executable RUN ["executable", "param1", "param2"]
- The RUN instruction will create a new layer on top of current image and will commit the final changes. Committed image will be used by next step.
- exec form is passed as a JSON array, so we should make sure to use double quotes (“) around the word and not single quote (‘).
- Backslash (\) can be used to continue current RUN instruction to new line in shell form.
- We can change default shell in shell form by using SHELL command.
- Layering RUN instruction and generating commits is the core concept of docker where we can pick any version of image, just like source control.
- The cache for RUN command doesn’t get invalidated automatically. The cache for instruction like RUN apt-get dist-upgrade -y will be reused in next build. In order to skip cache use –no-cache flag while building image like, docker build –no-cache.
# Multi line RUN instruction RUN apt-get update \ && apt-get install apache2 # Similar command with single line RUN apt-get update && apt-get install apache2 RUN /bin/bash -c 'echo $VARIABLE' # exec form - The variable replacement is not done by docker, it's done by shell. RUN ["apt-get", "install", "python3"] RUN ["/bin/bash", "-c", "echo $VARIABLE"] # When using windows with exec, we need to use escape character for backslash RUN ["c:\\windows\\system32\\tasklist.exe"]
CMD Instruction :
The main purpose of CMD instruction is to provide defaults to the image. These defaults can include executable or skip executable in case if it contains ENTRYPOINT instruction. CMD instruction has 3 forms,
# exec form, this is preferred form CMD ["executable", "param1", "param2"] # as a Default parameter to ENTRYPOINT instruction CMD ["param1", "param2"] # shell form CMD command param1 param2
- There can only be one CMD instruction in given docker file, if there are more than one instruction, only last one will be considered.
- If CMD is used to pass arguments to ENTRYPOINT instruction then both instructions should be in JSON array format.
- The exec form is parsed as a JSON which means we should be using double quotes (“), not single quote (‘).
- CMD instruction is executed while running the image irrespective of what form is used (shell or exec).
- When using shell form, the command will be executed in /bin/bash -c. If you want to run your command without shell, you must use exec form and specify executable.
- If user provides the arguments while executing docker run, it will override the values provided in CMD. (when using ENTRYPOINT instruction.)
- Basic difference between RUN and CMD is, RUN executes the command and commits the same where as CMD doesn’t execute command at the build time but specifies the intended command for the image.
- If you want your container to execute the same commad everytime, consider using ENTRYPOINT in combination with CMD.
# shell form FROM ubuntu CMD echo "Hello World!" | wc - # exec form FROM ubuntu CMD ["/usr/app/startserver.sh"]
We’ll understand ENTRYPOINT in upcoming article, for this article we sticked to understand RUN and CMD instructions.
That’s it for this article, please let me know your feedback / suggestion / queries in comments. If you want to check other related articles, please visit index page. Stay Safe and keep growing.
Reference : https://docs.docker.com