Common DockerFile directives:
Estimated reading: 5 minutes 5 views

🚀 DockerFile ENTRYPOINT Command – A Complete Guide with FAQs

When working with Docker, one of the key instructions you’ll often encounter in a DockerFile is the ENTRYPOINT command. It plays a crucial role in defining how your container behaves when it starts. Unlike CMD, the ENTRYPOINT command provides a fixed command that always executes, no matter what arguments are passed during docker run.


📘 What is the DockerFile ENTRYPOINT Command?

The ENTRYPOINT instruction in a DockerFile specifies the main command to run when the container starts. It is commonly used to define containers that behave like executables.

🧠 Think of ENTRYPOINT as the container’s primary purpose.


🛠️ Syntax of ENTRYPOINT

Docker supports two forms of ENTRYPOINT:

Syntax TypeFormat ExampleDescription
Exec formENTRYPOINT ["executable", "param1", ...]Recommended! Avoids shell form pitfalls.
Shell formENTRYPOINT command param1 param2Runs as /bin/sh -c '...', similar to shell.

✅ Recommended: Exec Form

Exec form is preferred because it directly runs the executable without invoking a shell. This allows better signal handling and works well with CMD for arguments.


🧩 ENTRYPOINT vs CMD

Both CMD and ENTRYPOINT define the command to run when the container starts, but there’s a difference:

FeatureCMDENTRYPOINT
PurposeDefault argumentsDefines the main executable
Can be overridden✅ Yes (with docker run)❌ No (unless --entrypoint is used)
Works with CMDNot mandatoryYes, often used to provide default arguments

📦 Practical Examples of ENTRYPOINT

🔹 Example 1: Basic ENTRYPOINT

FROM ubuntu
ENTRYPOINT ["echo", "Hello from container!"]

When you run:

docker build -t hello-entrypoint .
docker run hello-entrypoint

Output:

Hello from container!

🔹 Example 2: ENTRYPOINT with CMD

FROM alpine
ENTRYPOINT ["ping"]
CMD ["google.com"]

When you run:

docker build -t ping-google .
docker run ping-google

Output:

PING google.com (142.250.182.14): 56 data bytes
...

You can override CMD at runtime:

docker run ping-google github.com

Output:

PING github.com (20.207.73.82): 56 data bytes
...

💡 ENTRYPOINT stays as ping, but CMD becomes the argument.


⚠️ Common Mistakes and Tips

🚫 Using Shell Form Incorrectly

ENTRYPOINT echo "This won’t behave as expected"

This runs under /bin/sh -c, which might cause unexpected behavior and makes signal handling harder.

✅ Always Prefer Exec Form for ENTRYPOINT

ENTRYPOINT ["echo", "This will work"]

🧪 How to Override ENTRYPOINT

You can override the DockerFile’s ENTRYPOINT using the --entrypoint flag:

docker run --entrypoint ls my-image

This bypasses the defined ENTRYPOINT.


🧠 When Should You Use ENTRYPOINT?

  • When you want the container to always run a specific command
  • When building CLI tools in containers
  • For startup scripts or processes that should not be overridden easily

📑 DockerFile ENTRYPOINT Command – Function Table

🧱 Functionality📄 DescriptionExample
1. Define container startup commandSets the command that always runs when a container is started from the image.ENTRYPOINT ["python3"]
2. Supports Exec and Shell formsProvides flexibility to write the command using either a JSON array (exec) or a plain shell string.["nginx", "-g", "daemon off;"] or nginx -g 'daemon off;'
3. Works with CMD for argumentsAllows CMD to specify default arguments that get appended to ENTRYPOINT.ENTRYPOINT ["ping"], CMD ["google.com"]
4. Not overridden by runtime argumentsUnlike CMD, it won’t be overridden by docker run command-line args.docker run myimg github.com → still runs ping github.com
5. Can be overridden with --entrypointYou can override it at runtime using the --entrypoint flag.docker run --entrypoint ls myimg
6. Used for fixed behaviors or CLI containersBest used when the container needs to behave like a single executable (e.g., CLI tools, daemons).ENTRYPOINT ["myapp"]
7. Enables signal forwarding (Exec form only)Proper signal handling (e.g., SIGTERM, SIGINT) works only with the exec form.ENTRYPOINT ["node", "server.js"]
8. Doesn’t require CMD to workIt can function independently without a CMD, though often used together.ENTRYPOINT ["echo", "Hello"]
9. Becomes the PID 1 processThe process defined in ENTRYPOINT runs as PID 1 inside the container.Useful for long-running services
10. Helps create reusable base imagesAllows creating base images with fixed behavior, and flexible CMDs in downstream images.Base: ENTRYPOINT ["java", "-jar", "/app.jar"]

🎯 Summary

The ENTRYPOINT command is a powerful tool in DockerFile that lets you define containers that behave like executables. When used correctly—especially in exec form—it enables reliable and predictable behavior of containers.

Use ENTRYPOINT when you want full control over the container’s start behavior, and combine it with CMD to allow flexibility with default arguments.


❓ FAQs – DockerFile ENTRYPOINT Command

🔸 Q1: Can I use both CMD and ENTRYPOINT together?

Yes!
Use ENTRYPOINT to define the base command and CMD to provide default arguments.


🔸 Q2: What happens if both CMD and ENTRYPOINT are used?

Docker merges them. For example:

ENTRYPOINT ["python3"]
CMD ["app.py"]

Will execute:

python3 app.py

🔸 Q3: Can I override both CMD and ENTRYPOINT?

  • CMD: Yes, via arguments in docker run.
  • ENTRYPOINT: Only by using --entrypoint.

🔸 Q4: What is the difference between shell and exec form of ENTRYPOINT?

Shell FormExec Form
ENTRYPOINT echo HelloENTRYPOINT ["echo", "Hello"]
Uses a shell (/bin/sh -c)Directly calls executable
Limited signal handlingProper signal forwarding

🔸 Q5: Is it mandatory to use ENTRYPOINT?

No. It’s optional. Use it when you need a fixed command to always run when the container starts.


Leave a Reply

Your email address will not be published. Required fields are marked *

Share this Doc

ENTRYPOINT

Or copy link

CONTENTS
Scroll to Top