Overriding Default Behavior with Arguments
Suppose we want to create a pod using the “ubuntu-sleeper” image. We begin with a basic pod definition where the pod’s name and image are specified. When the pod is created, it starts a container that runs the default sleep command (sleeping for five seconds) and then exits. To modify the sleep duration to 10 seconds, simply append an additional argument in the pod specification. Any argument provided in the Docker run command correlates with theargs property in the pod definition file (formatted as an array).
When you append an argument to the Docker run command, it overrides the default parameters defined by the CMD instruction in the Dockerfile.
Dockerfile Instructions and Their Mappings
The Dockerfile for the “ubuntu-sleeper” image is defined with both an ENTRYPOINT and a CMD:- The ENTRYPOINT instruction specifies the command to run when the container starts.
- The CMD instruction provides default parameters for that command.
args field in the pod definition file, the CMD instruction is overridden, which effectively changes the sleep duration from 5 to 10 seconds.
Overriding the ENTRYPOINT
Now, consider a scenario where you want to override the ENTRYPOINT itself (for example, switching from “sleep” to an alternative command like “sleep2.0”). In Docker, you would use the--entrypoint option in the docker run command. In Kubernetes, this is achieved by providing the command field in the pod definition. Here, the command field corresponds to the Dockerfile’s ENTRYPOINT, while the args field continues to override the CMD instruction.
Docker command example with overridden ENTRYPOINT:
Deployment of the Pod
Once your pod definition is ready, deploy the pod using the following command:Key Fields Summary
Below is a table summarizing the Kubernetes pod definition fields that map to Dockerfile instructions:| Pod Definition Field | Dockerfile Instruction | Functionality Description |
|---|---|---|
| command | ENTRYPOINT | Specifies the command to run when the container starts (completely replacing Dockerfile ENTRYPOINT). |
| args | CMD | Provides default parameters passed to the command (overriding the Dockerfile CMD). |
Remember that specifying the
command in a pod definition replaces the Dockerfile’s ENTRYPOINT entirely, while the args field only overrides the default parameters defined by CMD.