What is the docker ENTRYPOINT and how is it used ?

What is the docker ENTRYPOINT and how is it used ?
Written by Nilima PaulFebruary 18, 2022
10 min read
Nilima Paul

Technology Security Analyst

The purpose of this artical is to show you how to manage docker ENTRYPOINT and its benefits.

Introduction of docker ENTRYPOINT :

Docker ENTRYPOINT is a Docker file order or guidance that is utilized to indicate the executable which should run when a compartment is begun from a Docker picture. It has two structures, the first is the 'executive' structure and the subsequent one is the 'shell' structure. Assuming there is no ENTRYPOINT or CMD indicated in the Docker picture, it starts and exits while implies holder stops naturally in this way, we should need to determine entry point or CMD with the goal that when we will begin the compartment it ought to execute some different option from going to stop.

How to ENTRYPOINT works in Docker :

As we probably are aware, docker ENTRYPOINT permits us to make a holder runnable, nonetheless, we can in any case determine the order while beginning the compartment. The two types of Docker entry points act distinctively when we determine any order at run time.

  • The 'executive' structure permits us to indicate the order line contentions to the 'docker run' order and it is annexed to the furthest limit of all components of the 'executive' structure which implies the predetermined order will pursue the executable determined in the ENTRYPOINT. For instance, if we pass the '- d' contention to the 'docker run' order, the Docker daemon will pass that contention to the entry point and the predetermined executable will run behind the scenes.
  • The 'shell' structure doesn't permit to indicate any order or the 'docker run' order line contentions while beginning the compartment as the ENTRYPOINT order runs as a subcommand of '/container/sh - ca. The executable has an unexpected PID in comparison to the compartment's 'PID 1' so on the off chance that we pass any unix signals like SIGTERM from the 'docker stop' order, the executable doesn't get it.

We can override the ENTRYPOINT instruction while starting the container using the ‘–entrypoint' flag. Also if we have multiple ENTRYPOINT instructions mentioned in Dockerfile then the last ENTRYPOINT will have an effect.

Examples of Docker ENTRYPOINT:

Let’s understand the working on Docker entry point by some examples.

Example #1 – ‘exec’ form ENTRYPOINT

We want to create an nginx Docker image using ubuntu as a base image and also want to start the engine when we run the container automatically.

We have to create a Dockerfile first with the ‘exec’ form ENTRYPOINT as below: –

FROM ubuntu
RUN apt-get update && apt-get install -y nginx
ENTRYPOINT ["nginx", "-g", "daemon off;"]

Build the Docker image using the above Dockerfile as shown below: –

docker build -t my-app.

Run a container using this new Docker image and connect to the newly created container using the ‘docker exec’ command as shown below: –

docker run -d --name my-web my-app
docker exec -it my-web sh

Now, if we run the ‘top’ command inside the container we can see that the ‘Nginx’ is already running with ‘PID 1’.

Example #2 – ‘shell’ form ENTRYPOINT

We are going to use the same scenario as above, however, we will replace the ENTRYPOINT from ‘exec’ to ‘shell’ in the Dockerfile as below:

Let’s build the Docker image using the build command as below:

docker build -t  my-app:v2.

Now, run a container using the above Docker image and connect to the newly created container and check the PID of the nNginxexecutable: –

docker run -d --name my-web2 my-app:v2
docker exec -it my-web2 sh

Explanation: In the above snapshot, we can see that the ‘sh’ command is running under ‘PID 1’. It is because Nginx is now running as a subcommand of the ‘/bin/sh -ca command.

In this way, we cannot stop a long-running ENTRYPOINT executable correctly using the ‘docker stop' command. If we want to stop the executable correctly we must use the ‘exec’ before the executable in the Dockerfile as shown below:

FROM ubuntu
RUN apt-get update && apt-get install -y Nginx
ENTRYPOINT exec nginx -g “daemon off;”

To verify, it is working as expected, create a new Docker image name ‘my-app:v3’ and run a container using it once the container is started, connect to it and check the PID of the nginx executable again:

docker run -d --name my-web3 my-app:v3
docker exec -it my-web3 sh

Explanation: In the above snapshot, we can see that the Nginx executable has PID 1 now.

Example #3

We want to see the entry point of a running container. We can do so by using the ‘inspect’ command or ‘docker ps’ command with the ‘format’ flag to get a cleaner output as below : –

docker inspect my-web2

docker ps --format "table  {{.ID}} \t {{.Names}} \t {{.Command}}" --no-trunc

Example #4

We want to override the ENTRYPOINT while starting the container. We have the ‘–entry point' flag for that We can use the ‘–entry point'flag to pass the command as below: –

docker run -d --entrypoint sh --name my-web4 my-app
docker run -d --entrypoint sh --name my-web5 my-app:v2
docker run -d --entrypoint sh --name my-web6 my-app:v3

docker ps -a --format "table {{.ID}} \t {{.Names}} \t {{.Command}}" --no-trunc

Explanation: In the above snapshot, we can see that the container ‘my-web4’, ‘my-web5’, and ‘my-web6’ is showing command ‘sh’ because the ENTRYPOINT has been overridden.

Benefits of docker ENTRYPOINT:
  1. ENTRYPOINT allows us to create container executable.
  2. ENTRYPOINT provides flexibility to specify additional command-line arguments if we want.

Presently, we realize that there are two types of Docker ENTRYPOINT, nonetheless, the 'executive' structure is best. We have Docker CMD that additionally permits us to make holder runnable yet we get a few benefits in Docker ENTRYPOINT over the CMD. The greater part of the Docker base picture has a shell, for example,/container/sh or/receptacle/slam as the CMD executable, for instance, ubuntu, busy box, Debian, and so on.

Docker secrets
Docker swarm
Was this blog helpful?
You must be Logged in to comment
Code Block

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance