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.
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:
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
.
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.
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.
原创文章,作者:506227337,如若转载,请注明出处:https://blog.ytso.com/223244.html