Introduction
The ENTRYPOINT
instruction specifies which command a Docker container should run when it starts. While ENTRYPOINT
cannot be permanently overridden, the docker run command allows users to replace it temporarily.
In this tutorial, you will learn how to override ENTRYPOINT
using the docker run
command.
Prerequisites
- Command-line access.
- Docker installed.
- A Docker image containing an
ENTRYPOINT
instruction.
ENTRYPOINT in Docker Explained
ENTRYPOINT
is a Dockerfile instruction that tells Docker which command should run after the container initiates. It is an immutable instruction, i.e., arguments at runtime cannot override it. This property makes the instruction helpful in creating dedicated containers for apps, web servers, and scripts.
Dockerfiles often feature ENTRYPOINT
in conjunction with the CMD
directive. The main difference between CMD and ENTRYPOINT is that the Docker daemon ignores CMD
if the user passes a different argument at runtime. For this reason, ENTRYPOINT
is used to specify the command and CMD
to provide the default argument.
For example, the Dockerfile below defines a Docker image called test-override. The ENTRYPOINT
directive tells Docker to execute the echo command while CMD
defines "Hello, World!" as the default argument. For example:
FROM ubuntu
MAINTAINER test-user
RUN apt update
ENTRYPOINT ["echo"]
CMD ["Hello, World!"]
Docker initiates the container when the user executes the following docker run
command:
docker run test-override
The system runs the ENTRYPOINT
directive by echoing the default argument to the command output.
However, if the user provides a custom argument as part of the docker run
command, Docker ignores the CMD
instruction:
docker run test-override new-argument
The output prints the new argument.
Note: For a more detailed overview of the differences between the CMD
and ENTRYPOINT
instructions, refer to our article Docker CMD vs. ENTRYPOINT Commands: What's the Difference?
Override ENTRYPOINT with docker run
The example above shows that supplying a new argument overrides the CMD
directive, but the ENTRYPOINT
remains the same. To override the ENTRYPOINT
directive at runtime, add the --entrypoint
option to the docker run
command:
docker run [options] --entrypoint [new_command] [docker_image]
The following command overrides the default echo
command from the previous example and runs the container interactively:
sudo docker run -it --entrypoint /bin/bash test-override
The container's shell prompt appears in the output.
Note: ENTRYPOINT
override is temporary. Once the user exits the container and runs it again using the standard docker run
command, Docker executes the default ENTRYPOINT
instruction.
Conclusion
This tutorial explained the ENTRYPOINT
directive and how to override it at runtime. It discussed the relationship between the docker run
command and ENTRYPOINT
and explained how to use the --entrypoint
option.
Next, learn about another two important Dockerfile directives by reading ADD vs. COPY: What are the Differences.