Open In App

What Is Entrypoint in Dockerfile?

Last Updated : 26 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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

  • Dockerfile: A text file containing step-by-step instructions for building a Docker image. Consider it as the code of a program.
  • Docker image: The package that is created from a Docker file. A docker image is analogous to an executable binary.
  • Docker container: A runnable instance created from a Docker image. Think of it as a process spawned from an executable file. Like multiple processes can be spawned from a program binary, various containers can be created from a Docker image.

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:

  • The exec form specifies what executable will be run along with its parameters. The executable is executed directly. This is the recommended form for the ENTRYPOINT command.
ENTRYPOINT ["executable", "parameter1", "parameter2"]
  • The shell form specifies the command to be run from a shell. Yes, these commands are executed through a shell, just like you execute a command in your terminal.
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:

  • Create a file named ‘Dockerfile’ and add these instructions.
FROM ubuntu
ENTRYPOINT ["echo", "Docker"]
  • Now, open a terminal in the same directory and enter this command to build the image.
docker build -t my-container .

Output:

build-output_resize

Output of docker build

  • Run the container.
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 output

  • Now run the container with some additional argument in the docker run command.
docker run --rm --name c1 my-container is awesome

Output:

docker run output with an extra argument

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.

  • If you add the CMD instruction to the Dockerfile, it gets appended to the ENTRYPOINT command (if no additional arguments are passed to the docker CLI). Update the Dockerfile:
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

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

docker run output

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

  • Update the ENTRYPOINT to run it in shell form. When you use the shell form, ENTRYPOINT executes the command in /bin/sh -c. So the full command would be /bin/sh -c echo $PWD.
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

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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads