How to Override ENTRYPOINT Using docker run

September 18, 2024

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.

Tutorial on 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.

Running the ENTRYPOINT command with the default parameter defined in the CMD directive.

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.

A new argument provided on runtime overrides the value supplied by the CMD directive.

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.

Run Docker container by overriding entrypoint command and opening container shell.

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.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
How to Manage Docker Containers? Best Practices
January 29, 2019

With Docker Container Management you can manage complex tasks with few resources. Learn best practices...
Read more
How to List/Start/Stop Docker Containers
September 18, 2024

A Docker container uses an image of a preconfigured operating system environment.
Read more
10 Docker Security Best Practices
September 14, 2020

This article includes ten container security best practices that can help you prevent attacks and security breaches.
Read more
Docker Image Size: How to Keep It Small?
April 10, 2024

Docker images can easily become too large to handle, which is why it is important to keep their size under control.
Read more