How to Override ENTRYPOINT Using docker run

Docker containers allow applications to run with no interference from other applications. An ENTRYPOINT command in a Dockerfile prescribes the container’s behavior upon starting it. However, overriding this default command might be necessary from time to time. For instance, if you want your container to be debugged or another command to be executed, the command would make use of the ‘entrypoint’ during the docker run command. This allows the user to specify a different entry point for that single run of the container without editing the Dockerfile.

This article demonstrates how to use the docker run command to override the ENTRYPOINT settings on a container.

What is ENTRYPOINT in Docker?

The term “ENTRYPOINT” refers to a specialized command in a Dockerfile that indicates a primary program to be executed when a particular Docker container is initiated. Unlike CMD which provides ‘arguments automation’, ENTRYPOINT lets the container function as an independent application.

For example, /bin/bash will always be executed as a command in the Linux container by default:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
FROM ubuntu
ENTRYPOINT ["/bin/bash"]
FROM ubuntu ENTRYPOINT ["/bin/bash"]
FROM ubuntu
ENTRYPOINT ["/bin/bash"]

Before proceeding with overriding the ENTRYPOINT, make sure to install Docker on your Ubuntu system or any other operating system you are using.

Overriding the ENTRYPOINT Using docker run

To override Dockerfile’s ENTRYPOINT, you may pass in a different command during container execution with the command ‘docker run –entrypoint’. This allows you to set a different command on the container at runtime and makes overriding an ENTRYPOINT quite simple.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
docker run --entrypoint <new-entrypoint> [OPTIONS] IMAGE [ARGUMENTS]
docker run --entrypoint <new-entrypoint> [OPTIONS] IMAGE [ARGUMENTS]
docker run --entrypoint <new-entrypoint> [OPTIONS] IMAGE [ARGUMENTS]

Here, <new-entrypoint> is the command you want to run instead of the default ENTRYPOINT. You can also include other options, the image name, and arguments for the new command.

Let’s explore the following steps to learn how to override an ENTRYPOINT using the docker run command:

Step 1: Create a Dockerfile

Let’s create a Dockerfile to define a custom ENTRYPOINT and CMD by adding the following content to it:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
FROM ubuntu
LABEL maintainer="anees"
RUN apt update
ENTRYPOINT ["echo"]
CMD ["Greetings from Anees Asghar!"]
FROM ubuntu LABEL maintainer="anees" RUN apt update ENTRYPOINT ["echo"] CMD ["Greetings from Anees Asghar!"]
FROM ubuntu
LABEL maintainer="anees"
RUN apt update
ENTRYPOINT ["echo"]
CMD ["Greetings from Anees Asghar!"]

Step 2: Build the Docker Image

Now build the image using the docker build command and name it ultahost-image:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo docker build -t ultahost-image .
sudo docker build -t ultahost-image .
sudo docker build -t ultahost-image .
build the docker image

Step 3: Verify Default ENTRYPOINT

Let’s use the following command to run the container to verify the default ENTRYPOINT:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo docker run ultahost-image
sudo docker run ultahost-image
sudo docker run ultahost-image
verify default entrypoint

The output confirms that the image uses the default ENTRYPOINT (echo) with the CMD (Greetings from Anees Asghar!).

Step 4: Docker Run Override ENTRYPOINT 

To override the default ENTRYPOINT in the image, use the –entrypoint flag with a new command, as shown below:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo docker run --entrypoint /bin/bash ultahost-image -c "echo 'Overridden entrypoint'"
sudo docker run --entrypoint /bin/bash ultahost-image -c "echo 'Overridden entrypoint'"
sudo docker run --entrypoint /bin/bash ultahost-image -c "echo 'Overridden entrypoint'"

Here, “–entrypoint /bin/bash” Overrides the default ENTRYPOINT with /bin/bash and -c “echo ‘Overridden entrypoint'” passes a command to the new ENTRYPOINT:

override entrypoint

Step 5: Verify the Behavior

For a profound understanding, first, run the image without –entrypoint to confirm the default behavior:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo docker run ultahost-image
sudo docker run ultahost-image
sudo docker run ultahost-image

After this, run the image with –entrypoint to observe the overridden behavior:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo docker run --entrypoint /bin/bash ultahost-image -c "echo 'Overridden entrypoint'"
sudo docker run --entrypoint /bin/bash ultahost-image -c "echo 'Overridden entrypoint'"
sudo docker run --entrypoint /bin/bash ultahost-image -c "echo 'Overridden entrypoint'"
verify behavior

Best Practices for Docker Override ENTRYPOINT

You can follow the below-listed practices to ensure efficient and maintainable overriding of Docker ENTRYPOINT:

  1. Use –entrypoint only when necessary, and update your Dockerfile if frequent overrides are needed.
  2. Combine ENTRYPOINT and CMD effectively to make your container more flexible.
  3. Keep ENTRYPOINT scripts simple to avoid unnecessary complexity.
  4. Document any overrides clearly in your project’s README or documentation for better understanding.

Conclusion

Overriding the ENTRYPOINT in Docker containers can be useful when you need to change the default behavior temporarily without altering the Dockerfile itself. To do this, you can use the docker run command with the –entrypoint option. This practice allows you to easily run a different command or debug your container. However, it’s important to use this method carefully and write down any changes to keep things clear and efficient. In this article, we explained how you can override ENTRYPOINT using the “docker run entrypoint” command.

Integrate Docker with UltaHost’s DDoS Protected VPS Hosting to easily override ENTRYPOINT and streamline your workflows. Enjoy ultra-fast SSD NVMe speeds, 3500+ Gbps DDoS protection, and lightning-fast server deployment at an unbeatable cost. Protect your business or online service with our top-tier firewalls!

FAQ

What is ENTRYPOINT in Docker?
How do I override the ENTRYPOINT in Docker?
How to Use the docker run command with entrypoint option?
Why would I want to override the ENTRYPOINT?
Can I use the –entrypoint option with any Docker image?
Do I need to update the Dockerfile when overriding the ENTRYPOINT?
Can I revert back to the default ENTRYPOINT after overriding it?

Related Post

How to Use Docker for Visual Studio Code

An open platform for creating, distributing, and execut...

How to Install Docker on Debian

Docker is a powerful containerization platform that all...

How to Set up Nginx as Reverse Proxy for Dock

Nginx is one of the most popular webserver and revers...

How to Install PHP Dependencies on Docker

As a web developer, you're likely no stranger to the im...

How to Set Up and Use Private Docker Registry

Setting up a private Docker registry can significantly ...

How to Set Up Laravel in Docker

Laravel is a PHP based web framework known for its clea...

Leave a Comment