My Understanding about RUN, CMD and ENTRYPOINT in Dockerfile
By Fahad Ahammed
- 3 minutes read - 540 wordsContainerization gives us many benefits from packaging to distribution and others. Thus, Docker is widely used as a containerization tool which is designed for running specific tasks and processes, not for hosting operating systems or like other virtualization tools like VMWare, VirtualBox etc. You create a container to serve a single unit task. Once it completes the given task, it stops. The container life-cycle depends on the ongoing process inside of it. Once the process stops,
the container stops as well.
A Dockerfile defines this process. It is a script made up of instructions on how to build a Docker image. In this script, there are two types of instructions that can define the process running in the container:
- ENTRYPOINT
- CMD
And for the RUN, it is mainly built for only build time instruction.
Dilemma on ENTRYPOINT and CMD
- CMD to define default command or executable or parameters of a container.
- CMD to set command which is possible to override.
- ENTRYPOINT to set command or executable.
- ENTRYPOINT is not overridable. (Except –entrypoint flag)
- Both ENTRYPOINT and CMD works as “last one counts”.
CMD and ENTRYPOINT can have two forms as instructions.
- Shell form
- Exec form
For better clarity –
By the way, it is suggested to avoid shell form for side affects or performance issue.
Example of CMD and ENTRYPOINT
To build this Dockerfile –
To run the recently created image –
The output of the above run command will not give anything.
What if I pass command on the docker run command?
It should give me the directory and file list from the root of the container which is supposed to be running.
How do I know about the “RUN” instructions of the Dockerfile? When I use the docker build command, we will get these –
“RUN” instruction is not changing anything on the image to check if it executed or not as that is just an echo. To test it, let me modify the Dockerfile –
If I again build the image using same command, It will create the image fine. But what changed?
The output will be nothing. But What If I check the folder with “ls”?
Output:
Ok, we have a file “log.txt” inside the image/container.
If I several times run the same command?
I am running same image several times. Should be different container.
Different image but giving it same date and time from that log.txt.
So, RUN statement is just once ran when image was built.
Actual Implementations
Now, I will use a basic Python flask application called app.py .
To make the image –
Build the image as usual –
Run –
What if I run the image with passed parameters?
So, I can override the CMD.
What If I use ENTRYPOINT?
What if I run this now?
So, we can not override the ENTRYPOINT.
Let me try some variations?
So, the above Dockerfile is setting ENTRYPOINT with python3 only and CMD passed to app.py but overriding that can be doable.
Another variation?
It will act as same. Whatever the order of CMD is, it will always pass after ENTRYPOINT if ENTRYPOINT is used.
I am hoping that I will get these always working after digging these with variations. Thanks all for reading.