|
| 1 | +--- |
| 2 | +--- |
| 3 | + |
| 4 | +{% capture overview %} |
| 5 | + |
| 6 | +This page shows how to define commands and arguments when you run a container |
| 7 | +in a Kubernetes Pod. |
| 8 | + |
| 9 | +{% endcapture %} |
| 10 | + |
| 11 | + |
| 12 | +{% capture prerequisites %} |
| 13 | + |
| 14 | +{% include task-tutorial-prereqs.md %} |
| 15 | + |
| 16 | +{% endcapture %} |
| 17 | + |
| 18 | + |
| 19 | +{% capture steps %} |
| 20 | + |
| 21 | +### Defining a command and arguments when you create a Pod |
| 22 | + |
| 23 | +When you create a Pod, you can define a command and arguments for the |
| 24 | +containers that run in the Pod. To define a command, include the `command` |
| 25 | +field in the configuration file. To define arguments for the command, include |
| 26 | +the `args` field in the configuration file. The command and arguments that |
| 27 | +you define cannot be changed after the Pod is created. |
| 28 | + |
| 29 | +The command and arguments that you define in the configuration file |
| 30 | +override the default command and arguments provided by the container image. |
| 31 | +If you define args, but do not define a command, the default command is used |
| 32 | +with your new arguments. For more information, see |
| 33 | +[Commands and Capabilities](/docs/user-guide/containers/). |
| 34 | + |
| 35 | +In this exercise, you create a Pod that runs one container. The configuration |
| 36 | +file for the Pod defines a command and two arguments: |
| 37 | + |
| 38 | +{% include code.html language="yaml" file="commands.yaml" ghlink="/docs/tasks/configure-pod-container/commands.yaml" %} |
| 39 | + |
| 40 | +1. Create a Pod based on the YAML configuration file: |
| 41 | + |
| 42 | + export REPO=https://raw.githubusercontent.com/kubernetes/kubernetes.github.io/master |
| 43 | + kubectl create -f $REPO/docs/tasks/configure-pod-container/commands.yaml |
| 44 | + |
| 45 | +1. List the running Pods: |
| 46 | + |
| 47 | + kubectl get pods |
| 48 | + |
| 49 | + The output shows that the container that ran in the command-demo Pod has |
| 50 | + completed. |
| 51 | + |
| 52 | +1. To see the output of the command that ran in the container, view the logs |
| 53 | +from the Pod: |
| 54 | + |
| 55 | + kubectl logs command-demo |
| 56 | + |
| 57 | + The output shows the values of the HOSTNAME and KUBERNETES_PORT environment |
| 58 | + variables: |
| 59 | + |
| 60 | + command-demo |
| 61 | + tcp://10.3.240.1:443 |
| 62 | + |
| 63 | +### Using environment variables to define arguments |
| 64 | + |
| 65 | +In the preceding example, you defined the arguments directly by |
| 66 | +providing strings. As an alternative to providing strings directly, |
| 67 | +you can define arguments by using environment variables: |
| 68 | + |
| 69 | + env: |
| 70 | + - name: MESSAGE |
| 71 | + value: "hello world" |
| 72 | + command: ["/bin/echo"] |
| 73 | + args: ["$(MESSAGE)"] |
| 74 | + |
| 75 | +This means you can define an argument for a Pod using any of |
| 76 | +the techniques available for defining environment variables, including |
| 77 | +[ConfigMaps](/docs/user-guide/configmap/) |
| 78 | +and |
| 79 | +[Secrets](/docs/user-guide/secrets/). |
| 80 | + |
| 81 | +NOTE: The environment variable appears in parentheses, `"$(VAR)"`. This is |
| 82 | +required for the variable to be expanded in the `command` or `args` field. |
| 83 | + |
| 84 | +### Running a command in a shell |
| 85 | + |
| 86 | +In some cases, you need your command to run in a shell. For example, your |
| 87 | +command might consist of several commands piped together, or it might be a shell |
| 88 | +script. To run your command in a shell, wrap it like this: |
| 89 | + |
| 90 | + command: ["/bin/sh"] |
| 91 | + args: ["-c", "while true; do echo hello; sleep 10;done"] |
| 92 | + |
| 93 | +{% endcapture %} |
| 94 | + |
| 95 | +{% capture whatsnext %} |
| 96 | + |
| 97 | +* Learn more about [containers and commands](/docs/user-guide/containers/). |
| 98 | +* Learn more about [configuring containers](/docs/user-guide/configuring-containers/). |
| 99 | +* Learn more about [running commands in a container](/docs/user-guide/getting-into-containers/). |
| 100 | +* See [Container](/docs/api-reference/v1/definitions/#_v1_container). |
| 101 | + |
| 102 | +{% endcapture %} |
| 103 | + |
| 104 | + |
| 105 | +{% include templates/task.md %} |
0 commit comments