Dockerfile Reference – ENTRYPOINT

Hello Everyone,
Hope you’re doing good and utilizing your time in learning new things, in this article we’ll understand ENTRYPOINT instruction being used in Dockerfile. For other related articles, please refer index page.

ENTRYPOINT Instruction :
ENTRYPOINT Instruction allows you to configure a starting point which will be triggered when your container will be sponned from the image. Available forms,

# exec form - this is preffered one.
ENTRYPOINT ["executable", "param1", "param2"]

# shell form
ENTRYPOINT command param1 param2
  • Only the last ENTRYPOINT instruction in Dockerfile will be considered as an ENTRYPOINT, if base image and Dockerfile both contains ENTRYPOINT, the base image’s instruction will be ignored.
  • You can override ENTRYPOINT runtime by using –entrypoint instruction, like docker run –entrypoint.
  • Any runtime argument passed to docker run command after image will be passed to ENTRYPOINT and will override arguments mentioned in CMD instruction, for example docker run <image> -H, here -H will be passed as a argument to ENTRYPOINT instruction.
  • When you’re using shell form, any runtime arguments will be ignored.
  • If you use shell form, it will be executed as a subcommand of /bin/sh -c, which will not start your command as PID 1. Because of which docker stop <image> command will not run. To overcome this, make sure you use exec as command (we’ll understand with example).

EXEC form :
We can use exec form to set the base command as ENTRYPOINT to the image. For additional parameters we can use CMD instruction which can be overrided runtime. let’s take an example for the same.

FROM ubuntu
ENTRYPOINT ["top", "-b"]
CMD ["-c"]

When we execute above image, top is the only process,

Run image using command docker run -it –rm –name test1 exec_entrypoint -H
Double check running processes of the image using docker exec -it test1 ps aux
Running docker stop command and checking time taken by container to stop.

Once we’re done with the experiments, we can stop container using docker stop test1. please find below some more examples for entrypoint.

# ENTRYPOINT to start apache2 in foreground
ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

# Writing custom script to do preprocess and then start server
ENTRYPOINT ["/usr/src/start_server.sh"]

Shell form :
We can also specify plain string for the ENTRYPOINT and it will get executed in /bin/sh -c. While working with shell form, you need to make sure to use exec as command, otherwise your process will not start with pid 1 and docker stop command will not work. Moreover any runtime parameters will be ignored, as shell form doesn’t take any runtime arguments. Let’s check example for same.

# Shell form where we have not used exec as command.
FROM ubuntu
ENTRYPOINT top -b
Created container from above file and executed with name test2

Trying to stop container

Let’s observe outcome closely,
1. top was not started with PID 1, as we had in previous scenario.
2. Continer failed to stop cleanly, it has to send SIGKILL after timeout to stop the container.
let’s check example when we use exec.

FROM ubuntu
ENTRYPOINT exec top -b
Executed above example and observe top is running as PID 1.
Process was able to stop gracefully.

That’s it for this article, hope you got to know something new by this article. Please let me know your suggestion / doubt / feedback in comments. For other related articles, please refer index page. Stay Safe and Happy Learning !!

Reference : https://docs.docker.com

Sanket Modi

Working in Information Technology since 2012. Started my career as Java Developer and now working in multiple different technologies like nodejs, Python, Docker, Kubernetes, Azure etc. I like to explore new technologies and read books.

You may also like...

1 Response

  1. March 10, 2021

    […] Dockerfile Reference – ENTRYPOINT Instruction […]

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: