// Tutorial //

Using Commands and Arguments in a Kubernetes Pod

Published on August 3, 2022
Default avatar
By Safa Mulani
Developer and author at DigitalOcean.
Using Commands and Arguments in a Kubernetes Pod

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Hello, readers! This article talks about Using Commands and Arguments in a Kubernetes Pod with different scenarios for a better understanding.

So, let us begin!! :)

Use of Commands and Arguments - Process execution

When we say that an application runs within a Kubernetes Pod, we actually mean that the container is wrapped and presented as a Pod.

A container wraps up all the necessary dependencies and commands to execute processes together and sits within a Pod. During the creation of a Pod, we can define commands and arguments that will run within the container altogether.

Usually, the command and arguments that we define in a custom form override the default command and arguments of the base container image.

In the context of this topic, we will be dealing with ways to create and define commands and arguments for a container running as an application Pod.

Define Commands and Arguments for a Kubernetes Pod

In order to define an argument within a container, we can make use of the command field. The moment we define commands, we would be needing arguments to be passed to it. We can pass these arguments to the command using the args field.

In the below example, we have passed the command printenv to the container for it to print the values for the environment variable KUBECONFIG as an argument to it.

Example: pod.YAML

apiVersion: v1
kind: Pod
  name: demo-cmd
  - name: cmd-arg-demo
    image: debian
    command: ["printenv"]
    args: ["KUBECONFIG"]
  restartPolicy: OnFailure

Let us now apply the above file and create a Pod.

kubectl apply -f pod.yaml

Once we create a Pod, we can get the logs of the pod and the specific container to look for the result of the command execution.

kubectl logs -f demo-cmd


The output returns the value for the command execution. That is, it displays the path of the KUBECONFIG file as the value.


1. Using env variables to define arguments

As a variant, we can make use of environment variables to pass the value of the arguments to the commands. Let us have a look at the below section of code-

Example: Sample Code

- name: data
  value: "002234-welcome-message"
command: ["/bin/data"]
args: ["$(data)"]

Using the above block of code, we can pass the value of the arguments using an environment variable. Here, we pass the value of the argument to the command in the form of a variable named data whose value is specified as an environment variable.

Apart from environment variables, we can also parse the value in the form of a ConfigMap and a Secret in a similar manner.

2. Running commands within a shell

At times, when we wish to execute multiple commands altogether, we would need a shell to be running within the container for the execution.

This can be achieved through running a virtual shell at the run-time.

For the same, we define a command to run all the specified commands within the Pod in the shell as shown below-

command: ["/bin/sh"]
args: ["-c", "while true; do echo Welcome to JournalDev; sleep 100;done"]

Here, in this example, we have instructed the Pod to use a shell to run a BASH script executing multiple commands altogether such as the while loop execution.


By this, we have approached the end of this topic. Feel free to comment below, in case you come across any questions.

For more questions related to Docker and Kubernetes, Stay tuned with us.

Till then, Happy Learning! :)

If you’ve enjoyed this tutorial and our broader community, consider checking out our DigitalOcean products which can also help you achieve your development goals.

Learn more here

About the authors
Default avatar
Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?