Define a Command and Arguments for a Container

This page shows how to define commands and arguments when you run a container in a Pod.

Before you begin

You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run this tutorial on a cluster with at least two nodes that are not acting as control plane hosts. 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 kubectl version.

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 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 with your new arguments.

Note: The command field corresponds to entrypoint in some container runtimes.

In this exercise, you create a Pod that runs one container. The configuration file for the Pod defines a command and two arguments:

pods/commands.yaml

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: command-demo
  5. labels:
  6. purpose: demonstrate-command
  7. spec:
  8. containers:
  9. - name: command-demo-container
  10. image: debian
  11. command: ["printenv"]
  12. args: ["HOSTNAME", "KUBERNETES_PORT"]
  13. restartPolicy: OnFailure
  1. Create a Pod based on the YAML configuration file:

    1. kubectl apply -f https://k8s.io/examples/pods/commands.yaml
  2. List the running Pods:

    1. kubectl get pods

    The output shows that the container that ran in the command-demo Pod has completed.

  3. To see the output of the command that ran in the container, view the logs from the Pod:

    1. kubectl logs command-demo

    The output shows the values of the HOSTNAME and KUBERNETES_PORT environment variables:

    1. command-demo
    2. tcp://10.3.240.1:443

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:

  1. env:
  2. - name: MESSAGE
  3. value: "hello world"
  4. command: ["/bin/echo"]
  5. args: ["$(MESSAGE)"]

This means you can define an argument for a Pod using any of the techniques available for defining environment variables, including ConfigMaps and Secrets.

Note: The environment variable appears in parentheses, "$(VAR)". This is required for the variable to be expanded in the command or args field.

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:

  1. command: ["/bin/sh"]
  2. args: ["-c", "while true; do echo hello; sleep 10;done"]

What’s next