Open In App

What Is Entrypoint in Dockerfile?

A Dockerfile is a blueprint for building Docker images. It consists of instructions that tell Docker how to build an image. One such instruction is ‘ENTRYPOINT’. It specifies the command that will always run when a new container is started. In this article, we will look at the ENTRYPOINT instruction in detail.

Key Terminologies in Docker

Understanding ENTRYPOINT On Dockerfile

The ENTRPOINT specifies the command that will always be executed when a container starts. It has two possible forms of execution:



ENTRYPOINT ["executable", "parameter1", "parameter2"]
ENTRYPOINT command param1 param2

ENTRYPOINT can never be overridden, unlike CMD. The command in the CMD instruction is overridden when you add another command when running the docker container from the CLI. In the case of ENTRYPOINT, the commands in the docker CLI are appended as parameters to the ENTRYPOINT command. Let’s see this in action:

FROM ubuntu
ENTRYPOINT ["echo", "Docker"]
docker build -t my-container .

Output:



Output of docker build

docker run --rm --name c1 my-container

–rm removes the container as soon as it executes the command. –name is used to name the container. When you run the above command, Docker runs a container from the my-container image named c1 and removes it as soon as the command mentioned in ENTRYPOINT (echo in our case) exits.

Output:

docker run output

docker run --rm --name c1 my-container is awesome

Output:

docker run output with an extra argument

The extra arguments are appended to the ENTRYPOINT command. If it was CMD instead of ENTRYPOINT the command in CMD would be overridden by the command in docker run.

FROM ubuntu
ENTRYPOINT ["echo", "Docker"]
CMD ["is", "cool"]

Build the container again:

docker build -t my-container .

And run it without any additional arguments

docker run --rm --name c1 my-container

Output:

docker run output

So far we have worked with the exec form of ENTRYPOINT that executes the command binary directly. In exec form there is no environment variable processing as it is not executed through a shell. So when you try to use environment variables in commands ($PWD for example). Change the command in the Docker file:

FROM ubuntu
ENTRYPOINT ["echo", "$PWD"]

Now build and run the image again.

docker build -t my-container .
docker run --rm --name c1 my-container

Output:

docker run output

See, the environment variable username does not gets replaced as the echo command was not executed from the shell.

FROM ubuntu
ENTRYPOINT echo $PWD

Now build and run the image again.

docker build -t my-container .
docker run --rm --name c1 my-container

Output:

docker run output

The environment variable gets processed and the value of PWD is logged to the terminal instead of “$PWD”.

Now, if you are confused as to what values get appended to ENTRYPOINT and when, you can just refer to this table.

No ENTRYPOINT

ENTRYPOINT entry p1_entry

ENTRYPOINT [“entry”, “p1_entry”]

No CMD

error, not allowed

/bin/sh -c entry p1_entry

entry p1_entry

CMD [“cmd”, “p1_cmd”]

cmd p1_cmd

/bin/sh -c entry p1_entry

entry p1_entry p1_cmd

CMD cmd p1_cmd

cmd p1_cmd

/bin/sh -c entry p1_entry

entry p1_entry /bin/sh -c cmd p1_cmd

Diffrenece Between ENTRPOINT and CMD

For information refer this link.

Conclusion

ENTRYPOINT are essential for building Dockerfiles. Use ENTRYPOINT when you want to build executable Docker images using commands that always need to be executed. Thus, ENTRYPOINT acts as an important element in your Dockerfile and can be primarily used when you want to specify command that should be run whenever a container starts.

Entrypoint in Dockerfile – FAQ’s

Can I Include Multiple ENTRPOINT In A Dockerfile?

Each Dockerfile should contain at most only one ENTRYPOINT.

Can ENTRYPOINT Run Shell Scripts In exec Mode?

Yes ENTRYPOINTs can run shell scripts in exec mode like this: ENTRYPOINT [“sh”, “-c”, “script.sh”]

What Are Some Common Use Cases For ENTRYPOINT?

Launching custom applications that consists of custom binaries, web servers, database servers.

Why Does My Environment Variable (like $PWD) Not Get Replaced In The exec Form Of ENTRYPOINT?

The exec form bypasses the shell, so environment variable processing doesn’t occur. Switch to the shell form to enable environment variable expansion.

How To Override ENTRYPOINT Instruction ?

You can use the –entrypoint flag with the docker run command to completely override ENTRYPOINT with your own command.


Article Tags :