Define a Command and Arguments for a Container
This page shows how to define commands and arguments when you run a container in a PodThe smallest and simplest Kubernetes object. A Pod represents a set of running containers on your cluster. .
- Before you begin
- Define a command and arguments when you create a Pod
- Use environment variables to define arguments
- Run a command in a shell
- What's next
Before you begin
You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. If you do not already have a cluster, you can create one by using Minikube, or you can use one of these Kubernetes playgrounds:
To check the version, enter
Define a command and arguments when you create a Pod
When you create a Pod, you can define a command and arguments for the
containers that run in the Pod. To define a command, include the
field in the configuration file. To define arguments for the command, include
args field in the configuration file. The command and arguments that
you define cannot be changed after the Pod is created.
The command and arguments that you define in the configuration file override the default command and arguments provided by the container image. If you define args, but do not define a command, the default command is used with your new arguments.
commandfield corresponds to
entrypointin some container runtimes. Refer to the Notes below.
In this exercise, you create a Pod that runs one container. The configuration file for the Pod defines a command and two arguments:
Create a Pod based on the YAML configuration file:
kubectl apply -f https://k8s.io/examples/pods/commands.yaml
List the running Pods:
kubectl get pods
The output shows that the container that ran in the command-demo Pod has completed.
To see the output of the command that ran in the container, view the logs from the Pod:
kubectl logs command-demo
The output shows the values of the HOSTNAME and KUBERNETES_PORT environment variables:
Use environment variables to define arguments
In the preceding example, you defined the arguments directly by providing strings. As an alternative to providing strings directly, you can define arguments by using environment variables:
env: - name: MESSAGE value: "hello world" command: ["/bin/echo"] args: ["$(MESSAGE)"]
Note: The environment variable appears in parentheses,
"$(VAR)". This is required for the variable to be expanded in the
Run a command in a shell
In some cases, you need your command to run in a shell. For example, your command might consist of several commands piped together, or it might be a shell script. To run your command in a shell, wrap it like this:
command: ["/bin/sh"] args: ["-c", "while true; do echo hello; sleep 10;done"]
This table summarizes the field names used by Docker and Kubernetes.
|Description||Docker field name||Kubernetes field name|
|The command run by the container||Entrypoint||command|
|The arguments passed to the command||Cmd||args|
When you override the default Entrypoint and Cmd, these rules apply:
If you do not supply
argsfor a Container, the defaults defined in the Docker image are used.
If you supply a
argsfor a Container, only the supplied
commandis used. The default EntryPoint and the default Cmd defined in the Docker image are ignored.
If you supply only
argsfor a Container, the default Entrypoint defined in the Docker image is run with the
argsthat you supplied.
If you supply a
args, the default Entrypoint and the default Cmd defined in the Docker image are ignored. Your
commandis run with your
Here are some examples:
|Image Entrypoint||Image Cmd||Container command||Container args||Command run|
|<not set>||<not set>|
- Learn more about configuring pods and containers.
- Learn more about running commands in a container.
- See Container.
Was this page helpful?
Thanks for the feedback. If you have a specific, answerable question about how to use Kubernetes, ask it on Stack Overflow. Open an issue in the GitHub repo if you want to report a problem or suggest an improvement.