Everything you need to know about Dockerfile ENTRYPOINT
In shell form, your application never receives the signal Docker sends to stop it.
ENTRYPOINT is one of the most important instructions in a Dockerfile. It sets the command that runs when a container starts, so it defines what your container does: run a web server, a worker, a command-line tool, or a script that prepares the container before your app starts.
This article explores how ENTRYPOINT works, how its two forms differ, how it works with CMD, how to override it, and the best practices that keep your containers starting and stopping cleanly. Every example was tested with Docker.
Docker ENTRYPOINT explained#
ENTRYPOINT sets the executable that runs when a container starts from your image. When you run docker run <image>, Docker starts the ENTRYPOINT command as the container's main process. When that process exits, the container stops.
You can also pass arguments to the ENTRYPOINT command, either as defaults with the CMD instruction or when you start the container. This lets you build an image that behaves like a command-line tool, with a fixed program and arguments you can change.
Syntax and usage of ENTRYPOINT#
ENTRYPOINT has two forms: exec form and shell form. You can put the instruction anywhere in your Dockerfile, but it's usually near the end, after the steps that build your app. If a Dockerfile has more than one ENTRYPOINT, only the last one takes effect.
Exec form syntax#
Exec form is a JSON array of the executable and its arguments:
ENTRYPOINT ["executable", "param1", "param2"]Docker runs the executable directly, without a shell. Arguments from CMD, or from the docker run command, are appended to the end. Exec form is the recommended form for almost every image.
Shell form syntax#
Shell form is a plain command:
ENTRYPOINT command param1 param2Docker runs it with /bin/sh -c, so shell features such as environment variable substitution ($HOME) work. But shell form has two big downsides:
- It ignores
CMDand any arguments you pass todocker run. - Your command runs as a child of the shell, so the shell, not your app, receives the signal Docker sends to stop the container. We'll look at why that matters below.
ENTRYPOINT in action#
Here's a simple Dockerfile for a Python app, using exec form:
FROM python:3.14-slim
WORKDIR /app
COPY app.py .
ENTRYPOINT ["python", "app.py"]When a container starts from this image, it runs python app.py. You can't see ENTRYPOINT work during docker build, because it only runs when a container starts.
The same instruction in shell form looks like this:
ENTRYPOINT python app.pyIt also runs python app.py, but through a shell, as /bin/sh -c python app.py.
ENTRYPOINT with CMD#
CMD sets the default command for a container. When an image has both ENTRYPOINT and CMD in exec form, CMD provides the default arguments for the ENTRYPOINT command:
ENTRYPOINT ["python", "app.py"]
CMD ["--port", "8000"]Running the container with no arguments runs python app.py --port 8000:
docker run myappAny arguments you add after the image name replace CMD. For example, this runs python app.py --port 9000:
docker run myapp --port 9000This is the most flexible pattern: ENTRYPOINT fixes the program, and CMD provides defaults that users can override.
If you use shell form for ENTRYPOINT, this doesn't work. With ENTRYPOINT python app.py, the container runs python app.py with no arguments, and both CMD and the arguments you pass to docker run are ignored.
Why exec form matters: stopping containers cleanly#
When you run docker stop, Docker sends a SIGTERM signal to the container's main process (process ID 1), so your app can finish its work and shut down. If the process is still running after 10 seconds, Docker kills it.
With exec form, your app is process 1 and receives the signal. With shell form, the shell is process 1, and it doesn't pass the signal on to your app.
To see the difference, we ran the same Python app, which prints a message when it receives SIGTERM, with each form:
| Exec form | Shell form | |
|---|---|---|
| Process 1 | python app.py | /bin/sh -c python app.py |
Time for docker stop | Immediate | 10 seconds |
App received SIGTERM | Yes | No |
| Exit code | 0 | 137 (killed) |
With shell form, the app never got the chance to shut down cleanly. In shell form, your application never receives the signal Docker sends to stop it. In production, that can mean dropped requests and unfinished work every time you deploy. Use exec form.
How to override ENTRYPOINT#
To replace an image's ENTRYPOINT when you start a container, use the --entrypoint option. It takes the new executable, and it must come before the image name. Any arguments after the image name are passed to the new entrypoint.
For example, to open a shell in an image instead of starting its app, run:
docker run -it --entrypoint sh myappTo run a different command with arguments, put the arguments after the image name:
docker run --entrypoint python myapp --versionThis runs python --version in the container. Note that --entrypoint also clears the image's CMD, so the default arguments don't apply.
The order matters. If you put the image name right after --entrypoint, as in docker run --entrypoint myapp /bin/bash, Docker treats myapp as the entrypoint and /bin/bash as the image name, and fails with docker: invalid reference format.
To run a command in a container that's already running, use docker exec instead, as in docker exec -it <container> sh.
Use cases for ENTRYPOINT in Docker#
The most common use for ENTRYPOINT is to make an image run a specific application. For example, an image for a command-line tool can set ENTRYPOINT to the tool, so users run it like a regular command, as in docker run mytool --help.
ENTRYPOINT is also useful in Continuous Integration and Continuous Deployment (CI/CD) pipelines. For example, you can build an image with ENTRYPOINT set to your test runner, so every run executes the tests in the same, repeatable environment.
Another common use is an entrypoint script that prepares the container before the app starts, for example, by waiting for a database, running migrations, or generating a configuration file from environment variables. The next section shows how to write one correctly.
Best practices for using ENTRYPOINT in Docker#
Use exec form#
Use the JSON array syntax for ENTRYPOINT and CMD, so your app runs as process 1, receives stop signals, and gets the arguments from CMD and docker run. If you need shell features like variable substitution, put them in an entrypoint script instead of using shell form.
End entrypoint scripts with exec "$@"#
An entrypoint script runs setup steps, then starts your app. The script should start the app with exec, which replaces the shell with your app, so the app becomes process 1 and receives stop signals:
#!/bin/sh
set -e
echo "Preparing the container..."
exec "$@""$@" is the list of arguments the script receives, which is the CMD. So with this Dockerfile, the script runs its setup and then replaces itself with python app.py:
FROM python:3.14-slim
WORKDIR /app
COPY app.py .
COPY --chmod=755 entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
CMD ["python", "app.py"]In our test, this container stopped immediately and the app shut down cleanly, just like exec form. Without exec, the script would stay as process 1, and you'd have the same problem as shell form.
Make entrypoint scripts executable and check them#
The script must be executable, or the container fails to start with a "permission denied" error. The --chmod option of COPY, shown above, sets the permissions as the file is copied. You can also run chmod +x in a separate RUN step.
Start the script with a shebang that the image supports. Small images, such as those based on Alpine Linux, don't include Bash, so use #!/bin/sh unless you install Bash. A linter like ShellCheck catches common mistakes in shell scripts before you build.
Avoid hard-coding values in entrypoint scripts#
Use environment variables or arguments instead of hard-coding values, so you can change them without rebuilding the image. For example, instead of hard-coding a file path:
#!/bin/sh
echo "Starting my application..."
exec ./my-app -f /path/to/my/file.txtYou can read it from a variable, with a default:
#!/bin/sh
echo "Starting my application..."
exec ./my-app -f "${MY_FILE:-/path/to/my/file.txt}"Now you can set MY_FILE when you start the container, as in docker run -e MY_FILE=/data/other.txt myapp.
Keep containers focused on a single responsibility#
Each container should run one main process, such as a web server or a worker, much like microservices. ENTRYPOINT defines that process. If you need several processes, run them in separate containers rather than starting them all from one entrypoint script.
Summary#
ENTRYPOINT sets the command that runs when a container starts, so it defines your container's main purpose. Use exec form, so your app receives stop signals and the arguments from CMD, combine it with CMD for flexible defaults, and override it with --entrypoint placed before the image name. If you use an entrypoint script, end it with exec "$@".
If you deploy with a Dockerfile on Sevalla's Application Hosting, your image's ENTRYPOINT and CMD start the web process by default. If you set a custom start command on the process, it replaces both. See the docs on building from a Dockerfile for details.