Docker’s ENTRYPOINT
instruction is a powerful tool for defining the main process of your container. Mastering its use, in conjunction with the CMD
instruction, is essential for building robust and reusable Docker images. This article explores ENTRYPOINT
and its practical applications.
Table of Contents
- Understanding the
ENTRYPOINT
Instruction - Passing Arguments to
ENTRYPOINT
ENTRYPOINT
vs.CMD
: A Detailed Comparison- Practical Examples and Best Practices
- Conclusion
Understanding the ENTRYPOINT
Instruction
The ENTRYPOINT
instruction in a Dockerfile designates the primary executable launched when a container starts. It defines the container’s core functionality. Unlike CMD
, arguments provided to ENTRYPOINT
aren’t overridden by docker run
commands; instead, they’re combined with runtime arguments. This ensures a consistent, predictable base process while allowing for runtime customization.
For example, a web server image might use:
ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
This guarantees Apache runs in the foreground, regardless of how the container is started. The -D FOREGROUND
arguments are inherent to the image.
Passing Arguments to ENTRYPOINT
The true power of ENTRYPOINT
lies in its ability to accept arguments from the docker run
command. These are appended to the ENTRYPOINT
command, enabling customization without altering the image.
Consider this Dockerfile
:
ENTRYPOINT ["/bin/sh", "-c"]
CMD ["echo $1 && date"]
Running docker run my-image "Hello from ENTRYPOINT!"
will print “Hello from ENTRYPOINT!” followed by the current date and time. The argument “Hello from ENTRYPOINT!” is passed to the shell and executed by the echo
command. The date
command from CMD
is also executed.
A more sophisticated example is a script that takes a port number:
#!/bin/bash
PORT="${1:-8080}" # Use 8080 as default if no argument is provided.
echo "Starting server on port: $PORT"
# ... server logic using $PORT ...
ENTRYPOINT
vs. CMD
: A Detailed Comparison
ENTRYPOINT
defines the main process; CMD
provides default arguments for that process. If both are present, docker run
arguments are appended to the ENTRYPOINT
command, overriding the CMD
arguments. If only CMD
is specified, it acts as the main process. Use ENTRYPOINT
for the core functionality and CMD
for defaults or easily overridable options.
Practical Examples and Best Practices
Example 1: A simple Python application
COPY . /app
WORKDIR /app
ENTRYPOINT ["python3", "my_app.py"]
This runs your Python application directly. Any arguments to docker run
will be passed to my_app.py
.
Example 2: Using a shell for more complex commands
ENTRYPOINT ["/bin/bash", "-c"]
CMD ["npm start"]
Here, you can start with a shell and then run any command you need, either default (npm start
) or override from docker run
.
Best Practices:
- Use
ENTRYPOINT
for the core command. - Use
CMD
for default arguments or easily changeable settings. - Always handle arguments safely within your
ENTRYPOINT
script, checking for validity and providing informative error messages.
Conclusion
ENTRYPOINT
is a fundamental Docker instruction, enabling the creation of robust, reusable, and adaptable container images. Understanding its interaction with CMD
is crucial for developing effective containerized applications. By carefully crafting your ENTRYPOINT
and CMD
instructions, you can build more reliable and maintainable Docker images.