2016-10-20 18:05:23 +00:00
|
|
|
---
|
2017-06-08 19:12:52 +00:00
|
|
|
title: Define a Command and Arguments for a Container
|
2018-05-05 16:00:51 +00:00
|
|
|
content_template: templates/task
|
2018-05-17 23:46:49 +00:00
|
|
|
weight: 10
|
2016-10-20 18:05:23 +00:00
|
|
|
---
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture overview %}}
|
2016-10-20 18:05:23 +00:00
|
|
|
|
|
|
|
This page shows how to define commands and arguments when you run a container
|
2018-05-05 16:00:51 +00:00
|
|
|
in a {{< glossary_tooltip term_id="pod" >}}.
|
2016-10-20 18:05:23 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2016-10-20 18:05:23 +00:00
|
|
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture prerequisites %}}
|
2016-10-20 18:05:23 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
|
2016-10-20 18:05:23 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2016-10-20 18:05:23 +00:00
|
|
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture steps %}}
|
2016-10-20 18:05:23 +00:00
|
|
|
|
2017-06-08 19:12:52 +00:00
|
|
|
## Define a command and arguments when you create a Pod
|
2016-10-20 18:05:23 +00:00
|
|
|
|
2016-10-20 23:36:00 +00:00
|
|
|
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 `command`
|
|
|
|
field in the configuration file. To define arguments for the command, include
|
|
|
|
the `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
|
2017-05-04 18:38:34 +00:00
|
|
|
with your new arguments.
|
2016-10-20 18:05:23 +00:00
|
|
|
|
|
|
|
In this exercise, you create a Pod that runs one container. The configuration
|
|
|
|
file for the Pod defines a command and two arguments:
|
|
|
|
|
2018-07-03 18:50:19 +00:00
|
|
|
{{< codenew file="pods/commands.yaml" >}}
|
2016-10-20 18:05:23 +00:00
|
|
|
|
|
|
|
1. Create a Pod based on the YAML configuration file:
|
|
|
|
|
2018-07-03 18:50:19 +00:00
|
|
|
kubectl create -f https://k8s.io/examples/pods/commands.yaml
|
2016-10-20 18:05:23 +00:00
|
|
|
|
|
|
|
1. List the running Pods:
|
|
|
|
|
2018-05-25 06:58:34 +00:00
|
|
|
kubectl get pods
|
2016-10-20 18:05:23 +00:00
|
|
|
|
|
|
|
The output shows that the container that ran in the command-demo Pod has
|
|
|
|
completed.
|
|
|
|
|
|
|
|
1. To see the output of the command that ran in the container, view the logs
|
|
|
|
from the Pod:
|
|
|
|
|
2018-05-25 06:58:34 +00:00
|
|
|
kubectl logs command-demo
|
2016-10-20 18:05:23 +00:00
|
|
|
|
|
|
|
The output shows the values of the HOSTNAME and KUBERNETES_PORT environment
|
|
|
|
variables:
|
|
|
|
|
|
|
|
command-demo
|
|
|
|
tcp://10.3.240.1:443
|
|
|
|
|
2017-06-08 19:12:52 +00:00
|
|
|
## Use environment variables to define arguments
|
2016-10-20 23:36:00 +00:00
|
|
|
|
|
|
|
In the preceding example, you defined the arguments directly by
|
|
|
|
providing strings. As an alternative to providing strings directly,
|
2016-10-21 22:52:30 +00:00
|
|
|
you can define arguments by using environment variables:
|
2016-10-20 23:36:00 +00:00
|
|
|
|
|
|
|
env:
|
|
|
|
- name: MESSAGE
|
|
|
|
value: "hello world"
|
|
|
|
command: ["/bin/echo"]
|
|
|
|
args: ["$(MESSAGE)"]
|
|
|
|
|
|
|
|
This means you can define an argument for a Pod using any of
|
|
|
|
the techniques available for defining environment variables, including
|
2018-01-05 02:05:27 +00:00
|
|
|
[ConfigMaps](/docs/tasks/configure-pod-container/configure-pod-configmap/)
|
2016-10-20 23:36:00 +00:00
|
|
|
and
|
2017-04-19 17:56:47 +00:00
|
|
|
[Secrets](/docs/concepts/configuration/secret/).
|
2016-10-20 23:36:00 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< note >}}
|
2017-08-17 23:17:32 +00:00
|
|
|
**Note:** The environment variable appears in parentheses, `"$(VAR)"`. This is
|
2016-10-21 22:52:30 +00:00
|
|
|
required for the variable to be expanded in the `command` or `args` field.
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< /note >}}
|
2016-10-21 22:52:30 +00:00
|
|
|
|
2017-06-08 19:12:52 +00:00
|
|
|
## Run a command in a shell
|
2016-10-21 22:52:30 +00:00
|
|
|
|
|
|
|
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"]
|
|
|
|
|
2017-05-04 18:38:34 +00:00
|
|
|
## Notes
|
|
|
|
|
|
|
|
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 `command` or `args` for a Container, the defaults defined
|
|
|
|
in the Docker image are used.
|
|
|
|
|
|
|
|
* If you supply a `command` but no `args` for a Container, only the supplied
|
|
|
|
`command` is used. The default EntryPoint and the default Cmd defined in the Docker
|
|
|
|
image are ignored.
|
|
|
|
|
|
|
|
* If you supply only `args` for a Container, the default Entrypoint defined in
|
|
|
|
the Docker image is run with the `args` that you supplied.
|
|
|
|
|
|
|
|
* If you supply a `command` and `args`, the default Entrypoint and the default
|
|
|
|
Cmd defined in the Docker image are ignored. Your `command` is run with your
|
|
|
|
`args`.
|
|
|
|
|
|
|
|
Here are some examples:
|
|
|
|
|
|
|
|
| Image Entrypoint | Image Cmd | Container command | Container args | Command run |
|
|
|
|
|--------------------|------------------|---------------------|--------------------|------------------|
|
|
|
|
| `[/ep-1]` | `[foo bar]` | <not set> | <not set> | `[ep-1 foo bar]` |
|
|
|
|
| `[/ep-1]` | `[foo bar]` | `[/ep-2]` | <not set> | `[ep-2]` |
|
|
|
|
| `[/ep-1]` | `[foo bar]` | <not set> | `[zoo boo]` | `[ep-1 zoo boo]` |
|
|
|
|
| `[/ep-1]` | `[foo bar]` | `[/ep-2]` | `[zoo boo]` | `[ep-2 zoo boo]` |
|
|
|
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2016-10-20 18:05:23 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture whatsnext %}}
|
2016-10-20 18:05:23 +00:00
|
|
|
|
|
|
|
* Learn more about [containers and commands](/docs/user-guide/containers/).
|
2017-08-17 00:57:43 +00:00
|
|
|
* Learn more about [configuring pods and containers](/docs/tasks/).
|
|
|
|
* Learn more about [running commands in a container](/docs/tasks/debug-application-cluster/get-shell-running-container/).
|
2018-05-05 16:00:51 +00:00
|
|
|
* See [Container](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#container-v1-core).
|
|
|
|
|
|
|
|
{{% /capture %}}
|
2016-10-20 18:05:23 +00:00
|
|
|
|
|
|
|
|