A closer look at commands and args in Kubernetes Pods
Tags: kubernetes, docker, tutorial
Published: 2021-09-21 14:09:56

---
title: A closer look at commands and args in Kubernetes Pods
published: true
date: 2021-09-21 14:08:00 UTC
tags: kubernetes, docker, tutorial
canonical_url: https://sahansera.dev/closer-look-at-kubernetes-pod-commands-args/
cover_image: https://sahansera.dev/static/a1ec4f32bafe8a2e51764b421b330cb3/1ce69/closer-look-at-commands-and-args-in-kubernetes-pods.jpg
---

In my previous blog post, we looked at how commands and args work in Docker. If you missed that one, the link is down below.

- [A closer look at commands and args in Docker containers](https://sahansera.dev/closer-look-at-docker-commands-args/)

Today we will be looking at the same but in Kubernetes. To get started, we will be building on top of the following Dockerfile.

```docker
FROM debian
ENTRYPOINT ["printf", "Hello from %s\n"]
CMD ["Name Printer"]
```

This would simply print out `Hello from Name Printer` when you run it.

As a time-saver, I have already packaged up the image. You can have look at the image layers in [Docker Hub](https://hub.docker.com/layers/sahan/name-printer/v1/images/sha256-99d5313101544ceff84d499f7c476bf63ee85f895af4d3b26040048acf6763b6?context=repo).

![closer-look-at-commands-and-args-in-kubernetes-pods-1.png](https://sahansera.dev/static/dc0eb3947e71396ac812b0bc6f5e5b26/5a190/closer-look-at-commands-and-args-in-kubernetes-pods-1.png "closer-look-at-commands-and-args-in-kubernetes-pods-1.png")

You could run this with Docker like so.

```sh
docker run sahan/name-printer:latest
```

That should give you `Hello from Name Printer` as the output.

Now let’s do the same thing in Kubernetes. I’m a big fan of imperative commands in k8s. You can simple create resources without having to write definition files for simple things.

```sh
kubectl run test --image=sahan/name-printer:latest
```

![closer-look-at-commands-and-args-in-kubernetes-pods-2.png](https://sahansera.dev/static/749c6e7be6c77e62125a99675f1ecaac/5a190/closer-look-at-commands-and-args-in-kubernetes-pods-2.png "closer-look-at-commands-and-args-in-kubernetes-pods-2.png")

> 💡 You can quickly delete the pod with `kubectl delete pod test --grace-period=0 --force` command.

Let’s export the pod definition to a file.

```sh
kubectl run test --image=sahan/name-printer:latest --dry-run=client -o yaml > pod.yml
```

In the pod definition file, we will add a new field called `args` under `spec.containers.args` like so:

```yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: test
  name: test
spec:
  containers:
  - image: sahan/name-printer:v1
    name: test
    args: ["Kubernetes"]
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}
```

Running and building this gives you `Hello from Kubernetes` on the console

```sh
kubectl create -f pod.yml
```

![closer-look-at-commands-and-args-in-kubernetes-pods-3.png](https://sahansera.dev/static/6b83d53788ba44f569e324043649ccb5/5a190/closer-look-at-commands-and-args-in-kubernetes-pods-3.png "closer-look-at-commands-and-args-in-kubernetes-pods-3.png")

This makes sense because we didn’t change/override the `ENTRYPOINT`. Whatever we define as `args` simply gets appended to that. So how can we change the `ENTRYPOINT`’s behaviour?

We can use the `command` attribute to define whichever command we want to run in it.

```yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: test
  name: test
spec:
  containers:
  - image: sahan/name-printer:latest
    name: test
    command: ["date"]
    args: ["-u"]
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}
```

Now when we run the new pod definition it should print out the current date-time in UTC format. As you can see it’s easy to override the `ENTRYPOINT`’s behaviour in the container.

One thing to take note of is, remember that we had `bash` as `CMD` set in our that why we didn’t have to pass in that. If you have a container that and you want to invoke a command, you can simply prepend `/bin/bash -c` to the command you want to run.

### Conclusion

That’s it for this one - hope it’s useful to understand how commands and args work in pods ✌️