Using Commands and Arguments in a Kubernetes Pod

Published on August 3, 2022

Safa Mulani

Using Commands and Arguments in a Kubernetes Pod

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

If you’re looking for a managed Kubernetes hosting service, check out our simple, managed Kubernetes service built for growth.

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! :)

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Safa Mulani


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.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel