Skip to main content
In this article, we explore how Docker handles commands, arguments, and entrypoints, and how these affect container behavior. Understanding these differences is crucial for building images that run as expected. Let’s start with a simple example using the Ubuntu image. Consider the following commands:
When you run these commands, Docker creates a container from the Ubuntu image and launches it. However, because the container’s default process (typically bash) expects a terminal, it exits immediately when no terminal is attached. Unlike virtual machines, containers are designed to run a specific task or process (e.g., hosting a web server, application server, or database). Once that task completes or the process crashes, the container stops running.
Containers are meant to run specific tasks rather than continuously running processes. The default behavior of many images reflects this design philosophy.

Defining the Default Process in a Dockerfile

Most Docker images use the CMD instruction in their Dockerfile to specify the process that should run inside the container. For example:
  • Nginx image: Uses CMD ["nginx"] to start the Nginx server.
  • MySQL image: Uses CMD ["mysqld"] to launch the MySQL daemon.
Consider this excerpt from a Dockerfile that installs both Nginx and MySQL:
The Ubuntu image Dockerfile is structured similarly:
In this Ubuntu example, Docker launches bash as the default command. However, since Docker does not attach a terminal by default, the shell exits immediately, and the container stops.

Overriding the Default Command

You can override the default CMD by appending a new command to the docker run command. For example, running the Ubuntu container with the sleep command:
Here, the container runs the sleep program for 5 seconds before exiting.

Making the Change Permanent

If you prefer that your image always executes the sleep command when started, you can create a new image based on Ubuntu with an updated CMD:
After building the image:
The container runs sleep 5 each time it starts. However, if you want the flexibility to change the sleep duration without rebranding the image, you can override the CMD at runtime:
While this works, the image name (ubuntu-sleeper) might misleadingly imply that the container always runs sleep.

Using ENTRYPOINT with CMD for Flexibility

The ENTRYPOINT instruction allows you to fix the executable while still providing flexibility for command-line arguments to override or extend the CMD settings. With ENTRYPOINT, any command-line arguments are appended to the entrypoint command. For example:
When you run the container without additional arguments:
The container executes sleep 5. If you run:
Docker executes sleep 10, replacing the default operand defined in CMD with the provided one.
If you define only ENTRYPOINT without a default CMD, the container may fail to execute properly if no command-line arguments are provided. For instance, removing CMD can result in errors like “sleep: missing operand”.

Overriding Entrypoint at Runtime

If you need to change the entrypoint entirely—say, switching from sleep to a different executable like sleep2.0—you can override it at runtime using the --entrypoint option:
In this case, the container executes sleep2.0 10.

Summary of Docker Commands and Dockerfile Configurations

Below is a summary table of key commands and their effects:

Consolidated Dockerfile Example

Below is the consolidated Dockerfile example using both ENTRYPOINT and CMD:
After building the image:
  • Running without additional parameters:
    Executes the command: sleep 5
  • Overriding the sleep duration:
    Executes the command: sleep 10
  • Overriding the entrypoint and the sleep duration:
    Executes the command: sleep2.0 10
That concludes our exploration of Docker’s CMD versus ENTRYPOINT. Happy containerizing!

Watch Video

Practice Lab