Skip to main content

Docker ENTRYPOINT Explained: Usage, Syntax, and Best Practices

Master Docker ENTRYPOINT with exec vs. shell syntax, CMD usage, runtime overrides, and real-world examples. Build clearer, more reliable containers today.
May 20, 2025  · 8 min read

Docker ENTRYPOINT is a Dockerfile instruction that defines and controls the default command executed whenever your container starts. Mastering ENTRYPOINT simplifies developing predictable, reproducible, and maintainable containers that behave as expected. 

This guide aims to help intermediate DevOps engineers, backend developers, and software engineers confidently use ENTRYPOINT and avoid common pitfalls.

If you are new to Docker, check our introduction to Docker tutorial, as this tutorial assumes some level of understanding of Docker. To get hands-on with Docker, check out our Introduction to Docker course

What is Docker ENTRYPOINT?

ENTRYPOINT defines the command executed by default whenever your Docker container launches. Unlike using CMD, the ENTRYPOINT instruction ensures consistency by providing commands that run every single time the container starts.

How does Docker ENTRYPOINT work?

ENTRYPOINT in a Dockerfile defines the main command that always runs when you start a container from an image. It’s like saying:  “No matter what arguments the user passes when running this container, always start with this command.”

We go into more detail about Dockerfiles in our How to Containerize an Application Using Docker tutorial. 

For example, in a Dockerfile:

ENTRYPOINT ["python", "app.py"]

Now, when you run:

docker run myimage

It will always execute:

python app.py

Here is a flow diagram of how ENTRYPOINT works:

+---------------------------+

|       Dockerfile          |

+---------------------------+

|                           |

|  ENTRYPOINT ["app"]       |

|  CMD ["--default-arg"]    |

+---------------------------+

             |

             v

+---------------------------+

|   Build Docker Image      |

+---------------------------+

             |

             v

+---------------------------+

|    Run Container          |

|   (docker run image)      |

+---------------------------+

             |

             v

+---------------------------+

| Executed Command:         |

|   app --default-arg       |

+---------------------------+

Override CMD at Runtime:

(docker run image --custom-arg)

             |

             v

+---------------------------+

| Executed Command:         |

|   app --custom-arg        |

+---------------------------+

Override ENTRYPOINT at Runtime:

(docker run --entrypoint /bin/bash image)

             |

             v

+---------------------------+

| Executed Command:         |

|   /bin/bash               |

+---------------------------+

Fundamentals of Docker ENTRYPOINT

Understanding the fundamentals will significantly boost your confidence in using ENTRYPOINT effectively.

Conceptual framework

ENTRYPOINT specifies the executable or script that runs automatically when your container starts. It is your container’s main process, driving your container's behavior predictably.

Core syntax

Dockerfile syntax for ENTRYPOINT is straightforward and usually used alongside CMD to provide default arguments or parameters. ENTRYPOINT defines the executable, whereas CMD specifies default arguments that ENTRYPOINT uses.

Understanding Docker concepts such as ENTRYPOINT is vital in acing data science interviews. Explore common interview questions in our Top 26 Docker Interview Questions and Answers for 2025

ENTRYPOINT Syntax Forms

Docker ENTRYPOINT comes in two distinct forms: shell and exec form. Knowing the differences between these forms can help you select wisely.

Shell form

Example syntax: 

ENTRYPOINT /usr/bin/ping -c 3 localhost

The shell form ensures easy integration of shell features (variable substitution, environment manipulation).

However, it can lead to poor signal handling; it runs inside intermediate /bin/sh process rather than directly as PID 1, causing issues with stopping and restarting containers.

PID 1 is the first process that is started during system boot. In Docker, the PID 1 process manages all other processes inside the container. 

Exec form

Example syntax: 

ENTRYPOINT ["/usr/bin/ping", "-c", "3"]

The Exec form ensures direct command execution as PID 1, leading to reliable signal handling and control.

However, it has no direct shell support or automatic environment variable expansion; it requires additional scripting for shell-like behavior.

ENTRYPOINT vs CMD: Key Differences and Use Cases

Distinguishing ENTRYPOINT from CMD prevents confusion and enables predictable container behavior.

Execution precedence

ENTRYPOINT sets the always-run executable, while CMD provides default arguments. If both ENTRYPOINT and CMD are defined, ENTRYPOINT takes precedence and executes using CMD’s values as its arguments unless explicitly overridden at runtime.

Use case scenarios

ENTRYPOINT should be used when you want consistent, enforced execution every launch, such as single-command containers or wrapper scripts around legacy applications. Conversely, CMD is suitable for specifying default arguments, allowing more flexibility with runtime overrides.

Feature

ENTRYPOINT

CMD

Purpose

Always-run executable

Default arguments

Overridable?

Only with docker run --entrypoint

Yes, with command line arguments

Combined use

CMD provides arguments to ENTRYPOINT

Ignored if command is passed at runtime

Typical use

Fixed behavior (e.g. run app)

Optional config (e.g. default flags)

Our Top 18 Docker Commands to Build, Run, and Manage Containers covers essential Docker commands, from container basics to volumes and networking, so you can confidently manage applications across environments.

Practical example

A typical combination of ENTRYPOINT and CMD is illustrated by this Dockerfile:

FROM ubuntu
ENTRYPOINT ["/usr/bin/ping", "-c", "3"]
CMD ["localhost"]

This Docker container always issues three pings by default against localhost. Users can override this default target at runtime with other hosts easily, still retaining ENTRYPOINT’s predictable execution behavior:

docker run ping-container google.com

You can learn more about deploying applications with Docker from our Containerization and Virtualization with Docker and Kubernetes course. 

Combining ENTRYPOINT with CMD

Properly combining ENTRYPOINT with CMD offers flexibility without sacrificing consistency.

CMD as default arguments

Setting ENTRYPOINT defines the main command reliably; CMD supplies default parameters for easy overrides without rebuilding images.

Runtime argument overrides

Arguments specified during docker run overwrite only CMD-defined defaults. ENTRYPOINT remains consistent, thus simplifying users’ experiences.

Overriding ENTRYPOINT at Runtime

Docker allows overriding ENTRYPOINT directly at runtime, enabling debugging without rebuilding your Dockerfile image.

Entrypoint flag usage

Docker provides a simple flag --entrypoint that changes container behavior temporarily.

docker run -it --entrypoint /bin/bash my-container

Temporary overrides enable troubleshooting using the shell, resolving common startup problems without image rebuilds or substantial delays.

Customizing ENTRYPOINT Behavior

Docker offers flexibility when ENTRYPOINT requires more advanced customization.

Overriding ENTRYPOINT at runtime

You can override the ENTRYPOINT command dynamically at runtime using docker run --entrypoint  for debugging or changing contexts quickly without modifying your Dockerfile repeatedly.

ENTRYPOINT with arguments

Arguments passed into containers become ENTRYPOINT parameters at runtime; best practices suggest having minimal ENTRYPOINT complexity alongside CMD for maximum clarity and control.

Wrapper scripts

Bash scripts introduced as ENTRYPOINT commands can execute conditional startup logic, waiting for dependent services (like databases), handling setup tasks, and more, while maintaining clean Dockerfile syntax.

Real-World Examples and Patterns

Practical ENTRYPOINT applications can illustrate typical, realistic usages. Let’s look at some of them.

Single-command containers 

For a Flask application setup:

FROM python:3.11-alpine
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
ENTRYPOINT ["python", "app.py"]
CMD ["--port", "8080"]

In the above Dockerfile:

  • ENTRYPOINT ["python", "app.py"]  sets the default executable. This is what runs when the container starts.
  • CMD ["--port", "8080"]  provides default arguments (--port 8080) to the ENTRYPOINT command.

The full default command executed will be:

python app.py --port 8080

 If you override CMD when running the container (e.g., docker run myimage --port 9090), it will replace --port 8080.

Script as ENTRYPOINT 

This is used for setting up support conditional logic: 

entrypoint.sh:

#!/bin/sh
until nc -z db_host 5432; do
  echo "Waiting for database to become available."
  sleep 2
done
exec python app.py "$@"

Dockerfile:

FROM python:3.11-alpine
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
COPY entrypoint.sh .
RUN chmod +x entrypoint.sh
ENTRYPOINT ["./entrypoint.sh"]
CMD ["--port", "8080"]

In the above files:

  • entrypoint.sh is used for waiting for the database connection.
  • exec python app.py "$@"  replaces the current shell process with python app.py, passing any additional arguments. "$@" comes from CMD or docker run parameters.
  • COPY entrypoint.sh  copies the entrypoint.sh script into the container’s /app directory.
  • ENTRYPOINT ["./entrypoint.sh"] sets entrypoint.sh as the default startup script when the container runs.
  • CMD ["--port", "8080"] provides default arguments that will be passed to the entrypoint.sh script and eventually to python app.py. The final executed command will look like: python app.py --port 8080

Signal Handling and PID 1

ENTRYPOINT’s form significantly impacts Docker's ability to handle signals due to PID 1's importance. Let’s explore that in more detail: 

Exec form and PID 1

Exec form runs commands as Docker’s PID 1 process directly, which improves signal forwarding reliability and proper stopping or restarting behavior.

Shell form limitations

Shell form adds an intermediate shell process. This prevents termination signals from reaching application processes, creating a risk for stuck or unresponsive containers.

Debugging ENTRYPOINT Behavior

Troubleshooting entrypoint configurations is easier using certain techniques and tricks. Let’s discover them below. 

Using --entrypoint for debugging

Temporary overrides quickly enable interactive debugging shells within your containers without image rebuilding. This allows you to test different things in your application without rebuilding the image. 

Viewing ENTRYPOINT logs

Checking output logs with docker logs simplifies tracing execution history and finding configuration issues clearly and quickly.

Avoid modifying Dockerfile for temporary testing

Debug-friendly runtime ENTRYPOINT overrides let you isolate and correct issues without repeatedly modifying and rebuilding container images.

Optimization Techniques and Best Practices

Below are several recommended optimizations to improve ENTRYPOINT reliability and maintainability.

Use exec form for signal handling

Always prefer exec form for proper termination signals and better overall container reliability and management.

Combining ENTRYPOINT and CMD effectively

Separate your executable (ENTRYPOINT) from default runtime arguments (CMD) and utilize runtime overrides for maximum flexibility and ease of management.

Minimize complexity in ENTRYPOINT

Keep ENTRYPOINT simple, delegating complex startup logic or conditions into separate scripts to ensure proper management of complexity and enhance clarity.

Advanced Use Cases and Patterns

Let’s explore some advanced ENTRYPOINT use cases and patterns.

Multi-stage builds with ENTRYPOINT

ENTRYPOINT in final Docker stages enforces reliable execution, providing cleaner builds tailored specifically for production tasks within CI/CD workflows.

Dynamic ENTRYPOINT scripts

ENTRYPOINT scripts can dynamically adjust their startup logic according to provided environment variables or runtime arguments, adapting seamlessly within different application contexts.

ENTRYPOINT for CI/CD pipelines

Leveraging ENTRYPOINT  improves consistency across test and production environments, ensuring predictable runtime behaviors and repeatability. For example, you can set up ENTRYPOINT to a script that runs tests automatically. 

Other applications include: 

  • An ENTRYPOINT that waits for the database, then runs migrations, and finally starts the app.
  • An ENTRYPOINT that compiles assets, lints code, or validates configurations automatically.
  • ENTRYPOINT scripts can detect environments (dev, staging, production) and tweak settings accordingly at container startup. 

Here is an example of a Docker image that runs tests automatically:

FROM python:3.11
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
ENTRYPOINT ["pytest"]

The .gitlab-ci.yml would look like this:

build-test:
  image: docker:latest
  services:
    - docker:dind
  script:
    - docker build -t myapp-test .
    - docker run myapp-test  # <-- No need to specify 'pytest', ENTRYPOINT does it

When docker run executes, it automatically triggers pytest inside the container. This will ensure all tests will be found and run immediately. 

There are several advantages to using ENTRYPOINT in CI/CD:

  • Every container starts and behaves in the same way. 
  • Simplifies CI/CD scripts because you don’t have to pass full commands every time. 
  • Automates pre-start checks, test runs, and migrations, reducing human error. 

Common Pitfalls and Troubleshooting

ENTRYPOINT has a few pitfalls worth avoiding in your workflows.

Misusing shell form

Shell form hinders signals and container managers; always prefer exec form for proper signal handling, container exits, and clean stopping.

Overcomplicating ENTRYPOINT

Complex scripts that lack clarity or reliability deteriorate further during debugging or ongoing maintenance. Simple ENTRYPOINT setups significantly simplify troubleshooting.

Conclusion

Understanding and effectively implementing Docker ENTRYPOINT allows building predictable, reliable, and maintainable Docker containers for your applications. Used correctly, ENTRYPOINT creates consistency without sacrificing flexibility due to Docker’s powerful override system. Experimenting carefully with ENTRYPOINT patterns, adhering to best practices outlined above, simplifies Docker usage significantly and leads to smoother deployment and management cycles in production.

Check out our Docker courses to explore more Docker topics, such as:


Derrick Mwiti's photo
Author
Derrick Mwiti

Docker ENTRYPOINT FAQs

What is the difference between ENTRYPOINT and CMD in a Dockerfile?

ENTRYPOINT defines the main command that always runs when the container starts. CMD provides default arguments for that command. If both are used, CMD values are passed to ENTRYPOINT unless overridden at runtime.

When should I use exec form vs shell form of ENTRYPOINT?

Use exec form (e.g., ["app", "arg1"]) when you want proper signal handling and direct PID 1 execution. Shell form (e.g., "app arg1") is limited because it spawns a shell that may not forward signals correctly, making it less reliable for production use.

Can I override ENTRYPOINT at runtime without changing the Dockerfile?

Yes, you can override ENTRYPOINT at runtime using the --entrypoint flag with docker run. This is useful for debugging or running alternate commands temporarily.

Why do some containers fail to stop properly when using ENTRYPOINT?

This often happens when using the shell form of ENTRYPOINT, which prevents proper signal forwarding to the main process due to the intermediate shell. Use exec form to avoid this issue.

Is it bad practice to include lots of logic in an ENTRYPOINT script?

Yes. Keeping ENTRYPOINT scripts minimal is best practice. Delegate complex logic to separate scripts or initialization utilities. This improves maintainability and reduces debugging friction.

Topics

Top Docker Courses

Track

Containerization and Virtualization with Docker and Kubernetes

0 min
Learn the power of Docker and Kubernetes, this interactive track will allow you to build and deploy applications in modern environments.
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

blog

Top 18 Docker Commands to Build, Run, and Manage Containers

This guide breaks down essential Docker commands—from container basics to volumes and networking—so you can confidently manage applications across environments!
Laiba Siddiqui's photo

Laiba Siddiqui

15 min

blog

How to Learn Docker from Scratch: A Guide for Data Professionals

This guide teaches you how to learn Docker from scratch. Discover practical tips, resources, and a step-by-step plan to accelerate your learning!
Joel Wembo's photo

Joel Wembo

14 min

blog

10 Docker Project Ideas: From Beginner to Advanced

Learn Docker with these hands-on project ideas for all skill levels, from beginner to advanced, focused on building and optimizing data science applications.
Joel Wembo's photo

Joel Wembo

9 min

Tutorial

Docker for Beginners: A Practical Guide to Containers

This beginner-friendly tutorial covers the essentials of containerization, helping you build, run, and manage containers with hands-on examples.
Moez Ali's photo

Moez Ali

14 min

Tutorial

Docker Build Args: The Ultimate Guide for Data Professionals

Learn how to supercharge your containerization workflow with Docker build arguments for flexible, secure, and consistent environments.
Dario Radečić's photo

Dario Radečić

11 min

Docker bot

Tutorial

Docker for Data Science: An Introduction

In this Docker tutorial, discover the setup, common Docker commands, dockerizing machine learning applications, and industry-wide best practices.
Arunn Thevapalan's photo

Arunn Thevapalan

15 min

See MoreSee More