Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

How to Override ENTRYPOINT with docker run

Use the --entrypoint option to run a different program than the one set in the Dockerfile.

VPS and Cloud Servers2 min read4 screenshots

What is ENTRYPOINT

ENTRYPOINT in a Dockerfile sets the program that runs when the container starts. Docker adds the CMD value, or the arguments you give to docker run, after it.

Example:

FROM ubuntu
ENTRYPOINT ["/bin/bash"]

Every container from this image starts with Bash.

Override it with docker run

docker run --entrypoint <new-entrypoint> [OPTIONS] IMAGE [ARGUMENTS]

This is useful to debug an image, open a shell, or run a different tool inside it.

Example

Step 1: Create a Dockerfile

FROM ubuntu
LABEL maintainer="admin"
RUN apt update
ENTRYPOINT ["echo"]
CMD ["Greetings from Docker!"]

Step 2: Build the image

sudo docker build -t my-image .
Building the Docker image

Step 3: Run it with the default ENTRYPOINT

sudo docker run my-image
Running the image with the default ENTRYPOINT

The output is Greetings from Docker!, because the container runs echo "Greetings from Docker!".

Step 4: Override the ENTRYPOINT

sudo docker run --entrypoint /bin/bash my-image -c "echo 'Overridden entrypoint'"
Overriding the ENTRYPOINT

Now Bash is the entrypoint. The arguments after the image name (-c "echo ...") go to Bash, and the CMD value is dropped.

Step 5: Compare

sudo docker run my-image
sudo docker run --entrypoint /bin/bash my-image -c "echo 'Overridden entrypoint'"
Comparing the default and the overridden behavior

More examples

Open a shell in an image:

docker run -it --entrypoint /bin/sh my-image

Run a command once:

docker run --rm --entrypoint ls my-image -la /etc

Good to know

  • --entrypoint takes only the program. Put the arguments after the image name.
  • To remove the entrypoint use --entrypoint "".
  • In Docker Compose use the entrypoint: key.

Quick recap

  • docker run --entrypoint PROGRAM IMAGE ARGS replaces the Dockerfile ENTRYPOINT.