Commands and Arguments

Commands and Arguments

In this tutorial, we will discuss about commands and arguments in a Kubernetes POD.

Command and arguments

In the previous tutorial, we crated a simple docker image that sleeps for a given number of seconds. We named it ubuntu-sleeper and we ran it using the following docker command

$ docker run --name ubuntu-sleeper ubuntu-sleeper

By default it sleeps for 10 seconds. But we can override it by passing a command line argument.

$ docker run --name ubuntu-sleeper ubuntu-sleeper 20

We will now create a POD using this docker image. We start with a following POD definition file

apiVersion: v1
kind: Pod
metadata:
  name: ubuntu-sleeper-pod

spec:
  containers:
    - name: ubuntu-sleeper
      image: ubuntu-sleeper

Now if you need the container to sleep for 30 seconds as in the second command. How do you specify the additional argument in the POD definition file?

Anything that is appended to the docker run command will go into the “args” property of the POD definition file in the form of an array like as follows

apiVersion: v1
kind: Pod
metadata:
  name: ubuntu-sleeper-pod

spec:
  containers:
    - name: ubuntu-sleeper
      image: ubuntu-sleeper
      args: ["30"]

Let us try to relate that to the docker file we created earlier. The Dockerfile has an Entrypoint as well as a CMD instruction specified.

The Entrypoint is the command that is run at startup, and the CMD is the default parameter passed to the command.

FROM Ubuntu

ENTRYPOINT ["sleep"]

CMD ["10"]

With the args option in the POD definition file, we override the CMD instruction in the Dockerfile.

But what if you need to override the entrypoint? Say from sleep to sleep2.0 command.

In the docker world we would run the docker run command with the entry point option set to the new command.

$ docker run --name ubuntu-sleeper --entrypoint sleep2.0 ubuntu-sleeper 30

The corresponding entry in the POD definition file would be using a command field corresponds to entry point instruction in the Dockerfile.

apiVersion: v1
kind: Pod
metadata:
  name: ubuntu-sleeper-pod

spec:
  containers:
    - name: ubuntu-sleeper
      image: ubuntu-sleeper
      command: ["sleep2.0"]
      args: ["30"]

So to summarize there are 2 fields that correspond to two instructions in the Dockerfile. The command field overrides the entrypoint instruction and the args field overrides the command instruction in the Dockerfile.

Remember it is not the command field that overrides the CMD instruction in the Dockerfile.

Commands and Arguments
Scroll to top