Docker runs its processes in isolated containers. In our previous articles, we learned about various docker topics and how to run the docker containers (https://www.golinuxcloud.com/docker-run-i/)
In this article let us understand how to override the entrypoint with the docker run command.
Let us get started!
Understanding Docker Entrypoint and CMD Instructions
Entrypoint and CMD are two instructions in Dockerfile that define the process for a Docker image. You can use these instructions depending on how you want to run your container.
The command parameter is an optional vehicle running the docker container as the user knows which command needs to be executed and has already added it in the dockerfile with CMD instruction.
The entrypoint of an image is very similar to the command as it indicates what needs to be executed when the container starts.
The main difference between CMD and Entrypoint is that you can override the CMD instruction from Docker CLI when the container is running. However, you can't override the entrypoint command with just command line parameters. Instead, you need to use the docker run command with a particular syntax.
Create Docker Image with custom ENTRYPOINT and CMD
In this section let us understand how to override the entrypoint instruction with the docker run command with an example.
Firstly, let us create a dockerfile that has both an ENTRYPOINT and CMD instruction as shown below.
FROM ubuntu RUN apt-get update ENTRYPOINT [“echo”, “Hello user”] CMD [“World”]
Now, let us build the docker image and run the docker container as follows -
$ docker build -t test:1.0 .
Output
Sending build context to Docker daemon 1.978MB Step 1/4 : FROM ubuntu ---> a8780b506fa4 Step 2/4 : RUN apt-get update ---> Running in 710278698a6b Get:1 http://archive.ubuntu.com/ubuntu jammy InRelease [270 kB] Get:2 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB] Get:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [114 kB] Get:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [99.8 kB] Get:5 http://security.ubuntu.com/ubuntu jammy-security/multiverse amd64 Packages [4644 B] Get:6 http://archive.ubuntu.com/ubuntu jammy/main amd64 Packages [1792 kB] Get:7 http://security.ubuntu.com/ubuntu jammy-security/restricted amd64 Packages [480 kB] Get:8 http://archive.ubuntu.com/ubuntu jammy/restricted amd64 Packages [164 kB] Get:9 http://archive.ubuntu.com/ubuntu jammy/multiverse amd64 Packages [266 kB] Get:10 http://archive.ubuntu.com/ubuntu jammy/universe amd64 Packages [17.5 MB] Get:11 http://security.ubuntu.com/ubuntu jammy-security/universe amd64 Packages [757 kB] Get:12 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages [579 kB] Get:13 http://archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages [940 kB] Get:14 http://archive.ubuntu.com/ubuntu jammy-updates/multiverse amd64 Packages [16.9 kB] Get:15 http://archive.ubuntu.com/ubuntu jammy-updates/restricted amd64 Packages [528 kB] Get:16 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [881 kB] Get:17 http://archive.ubuntu.com/ubuntu jammy-backports/universe amd64 Packages [7290 B] Get:18 http://archive.ubuntu.com/ubuntu jammy-backports/main amd64 Packages [3175 B] Fetched 24.5 MB in 8s (2946 kB/s) Reading package lists... Removing intermediate container 710278698a6b ---> 088f78d3384c Step 3/4 : ENTRYPOINT [“echo”, “Hello user”] ---> Running in fb315664092c Removing intermediate container fb315664092c ---> 8dad4eba3c5c Step 4/4 : CMD [“World”] ---> Running in dd1de103397c Removing intermediate container dd1de103397c ---> ae5f8b619196 Successfully built ae5f8b619196 Successfully tagged test:1.0
Now let us run the docker without passing any additional parameters
$ docker run test:1.0
Output
Hello user World
Override CMD value from Dockerfile
Now, let us see how we can override the entrypoint instruction by specifying parameters to the docker run
command. What we are doing here, is that we are overriding the default CMD by adding the parameter to the docker run command.
Syntax
docker run <image> <parameter>
Example
$ docker run test:1.0 how are you
Output
Hello user how are you
Override ENTRYPOINT value from Dockerfile
Now, if you want to override the default executable, you can use the --entrypoint
flag and run the docker container as shown below.
Syntax
docker run --entrypoint [new_command] [docker_image] [optional:value]
Example
$ sudo docker run -it --entrypoint /bin/bash test:1.0
Output
You can see that you have entered the docker container in an interactive mode.
root@4ad3fed5e1fa:/# ls bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var root@4ad3fed5e1fa:/#
NOTE: Passing --entrypoint
will clear out any default command set on the image i.e. any CMD instruction in the Dockerfile used to build it.
Conclusion
In this article, we understood CMD and entrypoint instructions and how to use the --entrypoint flag with the docker run command to override the default executable. Hope this article helped you and in case of any queries please feel to post your comments or queries and we will be able to resolve them at the earliest.
Happy learning!!
References
https://docs.docker.com/engine/reference/run/#/entrypoint-default-command-to-execute-at-runtime