DOCKER: The ENTRYPOINT and CMD

A Tech Lead by profession, a Data Enthusiast and a forever learner. Experienced in DevOps, Cloud, FullStack Development, Data Science, Machine Learning and AI.
Search for a command to run...

A Tech Lead by profession, a Data Enthusiast and a forever learner. Experienced in DevOps, Cloud, FullStack Development, Data Science, Machine Learning and AI.
No comments yet. Be the first to comment.
Hashnode introduced 'headless' and while having a look at it, It really seemed interesting. It's nice to have a way to play with our publications and posts from Hashnode, outside of Hashnode without tedious data scraping. While reading another blog, ...

While trying to get Red Hat (RHEL9) installed in my WSL (Windows Subsystem for Linux) within my Windows 11 laptop, I stumbled upon the same hurdle I did for installing the older RHEL8. The list of available distros published by Microsoft still does n...

When it comes to collaborative software development, efficiency in communication and planning are critical. GitHub offers a feature called "Code owners" to improve project management and facilitate cooperation. Let's see how this can improve your dev...

PowerShell is a powerful tool, allowing to perform a myriad of tasks with just a few commands. However, there are times when elevated privileges are required to execute certain commands. This is as easy as right-clicking PowerShell and "Run As Admini...

While trying to setup RHEL with WSL 2, I stumbled upon this very helpful blog https://wsl.dev/mobyrhel8/ While doing the setup, I found this zsh theme very nice, and I have been using it since then. The above mentioned blog already has the steps, but...

In a Dockerfile, the use of ENTRYPOINT and CMD is crucial in defining the way a container behaves. However, it does confuse a lot of us regarding the usage, best practices, and key considerations when using these instructions.
The ENTRYPOINT instruction sets the primary command that will executed by the container when it starts. It can be specified with or without an executable and also allows for arguments.
ENTRYPOINT ["nginx", "-g", "daemon off;"]
ENTRYPOINT command cannot be overridden by appending a command while executing 'docker run' at runtime. However, the arguments can be appended.
Example Dockerfile:
FROM ubuntu
ENTRYPOINT ["echo", "Hello"]
Run the container with a command:
docker run my-image Goodbye
Output:
# output
Hello Goodbye
If we really want to overwrite the command in ENTRYPOINT, we need to use the '--entrypoint' with docker run:
# Example
docker run -it --entrypoint /bin/sh my_custom_image
The CMD instruction sets the default command for the container. If used without an ENTRYPOINT, it becomes the default command and arguments that will be used when the container starts. Whereas, when used with an ENTRYPOINT, it provides default arguments for the entry point.
This can be overridden though at runtime by passing a command when running the container using 'docker run'.
Example:
CMD ["nginx", "-g", "daemon off;"]
CMD command can be overridden by appending a command while executing 'docker run' at runtime.
Example Dockerfile:
FROM ubuntu
CMD ["echo", "Hello"]
Run the container with a command:
docker run my-image Goodbye
Output:
# output
Goodbye
We should prefer the exec form of the ENTRYPOINT and CMD to avoid shell processing.
Example:
ENTRYPOINT ["echo", "Hello, $USER"]
If we use the insecure way of using shell form instead, it might introduce a security vulnerability. The shell form can be abused to perform shell injection. Additionally, the shell form becomes dependent on the particular shell in use and if a different shell is being used in a distribution, it might not be consistent.
# An example where $USER can be used for shell injection vulnerability.
# Malicious code can be introduced if the $USER variable is not sanitized.
FROM ubuntu
CMD echo "Hello, $USER"
Use CMD when you want to provide default values for the command and allow them to be easily overridden at runtime.
Use ENTRYPOINT when you want to set a fixed primary command and allow users to append additional arguments.
In many cases, combining both instructions offers a balance of flexibility and definition. Understanding these use cases will empower you to create more versatile and user-friendly Docker images.
If we have a scenario where we want to run a script instead of any particular command, and maybe pass a default argument, we can do it seamlessly with ENTRYPOINT and using CMD along with it.
FROM node
# Set the working directory
WORKDIR /app
# Copy the application files
COPY . .
# Set the script as the ENTRYPOINT
ENTRYPOINT ["./entrypoint.sh"]
# Default argument 'start' to pass to the script if nothing mentioned in run.
CMD ["start"]
Mastering Dockerfile entry points empowers you to create efficient, modular, and customizable Docker images. Whether you're building a simple web server or a complex microservices architecture, understanding how to leverage ENTRYPOINT and CMD is essential for Dockerfile success.
Happy Dockerizing!