How to Override Entrypoint Using Docker Run

April 10, 2020

Introduction

Entrypoint and CMD are instructions in the Dockerfile that define the process in a Docker image. You can use one or combine both depending on how you want to run your container.

One difference is that unlike CMD, you cannot override the ENTRYPOINT command just by adding new command line parameters. To override ENTRYPOINT you need to modify the docker run command following a specific syntax.

In this tutorial, you will learn how to override ENTRYPOINT using the docker run command.

Tutorial on how to override entrypoint using docker run command.

Note: If you still have uncertainties about how to use CMD and ENTRYPOINT instructions, refer to our article Docker CMD Vs Entrypoint Commands: What’s The Difference?

Prerequisites

  • Access to a command line
  • A user with sudo privileges
  • A running Docker instance
  • An existing Docker image

Override ENTRYPOINT with docker run

To illustrate how to override this command, we are going to run a container that echoes the message Hello World by combining ENTRYPOINT and CMD in the Dockerfile.

In the Dockerfile, the ENTRYPOINT command defines the executable, while CMD sets the default parameter.

FROM ubuntu
MAINTAINER sofija
RUN apt-get update
ENTRYPOINT [“echo”, “Hello”]
CMD [“World”]

If you build an image from this file and use it to run a Docker container, the output displays:

Docker ENTRYPOINT vs CMD instructions combined.

You can easily override the default CMD by adding the desired parameter to the docker run command:

sudo docker run [container_name] [new_parameter]

In the example below, we changed the CMD parameter World, by adding Sofija to the command. As a result, the output displays Hello Sofija.

Adding parameters to a docker run command to run a container with ENTRYPOINT and CMD instructions.

However, you may want to override the default executable and, for instance, run a shell inside a container. In that case, you need to use the --entrypoint flag and run the container using the following syntax:

sudo docker run --entrypoint [new_command] [docker_image] [optional:value]

To override the default echo message in our example and run the container interactively, we use the command:

sudo docker run -it --entrypoint /bin/bash [docker_image]

The output shows us we are now inside the container.

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

Note: You can use different attributes to set up a container exactly how you need it. Follow the link to learn how to use the docker run command and master the art of running containers.

Conclusion

Now you know how to use the --entrypoint flag in the docker run command and override the default executable.

Don’t forget that this is only temporary. Once you exit out of the container and run it again using the standard docker run command, it executes the default ENTRYPOINT instruction.

Was this article helpful?
YesNo
Sofija Simic
Sofija Simic is an experienced Technical Writer. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.
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
May 27, 2019

A Docker container uses an image of a preconfigured operating system environment.
Read more
Docker ADD vs COPY: What are the Differences?
December 16, 2019

If you are creating a Dockerfile, you may be puzzled by how to copy files and directories into it.
Read more
Docker Image Size: How to Keep It Small?
February 19, 2020

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