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.
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 thedocker run command. For example, running the Ubuntu container with the sleep command:
sleep program for 5 seconds before exiting.
Making the Change Permanent
If you prefer that your image always executes thesleep command when started, you can create a new image based on Ubuntu with an updated CMD:
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:
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:sleep 5. If you run:
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 fromsleep to a different executable like sleep2.0—you can override it at runtime using the --entrypoint option:
sleep2.0 10.
Summary of Docker Commands and Dockerfile Configurations
Below is a summary table of key commands and their effects:| Command/Configuration | Description | Example |
|---|---|---|
| Default CMD in Ubuntu container | Executes bash but exits when no terminal is attached | docker run ubuntu |
| Overridden CMD at runtime | Replaces default command with a specified one (e.g., sleep 5) | docker run ubuntu sleep 5 |
| Permanent CMD update in Dockerfile | Builds an image with a permanent command change | CMD ["sleep", "5"] |
| Using ENTRYPOINT with CMD | Fixes the executable while allowing dynamic command-line argument replacement | ENTRYPOINT ["sleep"] & CMD ["5"] |
| Override ENTRYPOINT at runtime | Replaces the image’s fixed executable with an alternative one | docker run --entrypoint sleep2.0 ubuntu-sleeper 10 |
Consolidated Dockerfile Example
Below is the consolidated Dockerfile example using both ENTRYPOINT and CMD:-
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