Understanding Container Commands
When you run a Docker container using the Ubuntu image, as shown below:Default Commands in Docker Images
Each Docker image contains instructions that define the process to run when a container starts. Many popular images, such as nginx or MySQL, include a CMD instruction in their Dockerfile that sets the default command. For instance, the nginx image typically has the commandnginx, and the MySQL image uses mysqld.
Consider this Dockerfile snippet for installing and configuring nginx:
Remember: Bash is a shell, not a persistent server process. When the Ubuntu container is launched without an attached terminal, the shell exits immediately.
Overriding the Default Command
To override the default command for a container, you can append a command to the end of thedocker run command. For example, this command instructs the container to run sleep 5:
sleep 5, waits for five seconds, and then exits.
If you want to permanently change the behavior of the image so that it always runs sleep 5, you must create a new image based on Ubuntu and specify the new default command in its Dockerfile. You can specify the command in either of two formats:
- Shell form:
- JSON array format:
Configuring ENTRYPOINT for Runtime Arguments
Sometimes, you may want to specify only runtime arguments without changing the default command. In such cases, the ENTRYPOINT instruction is useful. This instruction sets the executable to run when the container starts, and any command-line arguments provided at runtime are appended to it. Consider the following Dockerfile:sleep 5 by default. You can override the sleep duration at runtime by specifying a new parameter:
sleep 10.
- With CMD alone, runtime arguments replace the default command.
- With ENTRYPOINT, runtime arguments are appended to the specified executable, allowing you to override just the parameters.
Overriding ENTRYPOINT at Runtime
At times, you might want to completely override the ENTRYPOINT. For example, if you wish to use a different command (like switching fromsleep to sleep2.0), you can do so using the --entrypoint flag in the docker run command.
Given the Dockerfile:
sleep expects an operand:
sleep 10.
To override the ENTRYPOINT with a different executable, run:
sleep2.0 10 (provided that sleep2.0 is a valid command).
