In my previous post in this Kubernetes guide, you learned about the HorizontalPodAutoscaler for automatic Pod scaling based on real-time usage. In this post, you will learn how to use commands and arguments in a Kubernetes Pod. Command and arguments help you specify the default command (or executable) and the parameters that are passed to it when a container runs in a Kubernetes Pod.
Avatar

Command and arguments in Docker containers

Let's first discuss how a command and arguments work in a Dockerfile. A Dockerfile is a text file containing a series of commands and instructions that are used to create a container image. In a Dockerfile, you specify an ENTRYPOINT instruction to define the executable that will run as the container's main process. For example:

ENTRYPOINT ["printenv"]

This is a command that prints the environment variables of the container. In this case, when a container starts from an image built with this Dockerfile, it will simply print out the environment variables and then exit.

A CMD instruction can be used to define the arguments that are passed to the default command. Multiple arguments can be specified by comma-separated strings enclosed in quotes. For example:

CMD ["HOSTNAME", "PATH"]

This defines the default arguments "HOSTNAME" and "PATH" that are passed to the printenv command specified with an ENTRYPOINT instruction. Let's see a sample Dockerfile below.

FROM ubuntu
ENTRYPOINT ["printenv"]
CMD ["HOSTNAME", "PATH"]
Viewing a sample Dockerfile

Viewing a sample Dockerfile

This Dockerfile uses the Ubuntu container image as a base image, which uses bash as a default command. However, we changed the main executable and passed additional arguments with the help of the ENTRYPOINT and CMD instructions in the Dockerfile. When containerizing your app, you need to follow the same approach.

Now, you can build a custom image with this Dockerfile by running this command:

docker build -t ubuntu-custom . 

The -t flag allows you to tag the image with a name (and, optionally, a tag). In this case, the name of the image will be ubuntu-custom. The dot . indicates the current directory. Docker uses the build context to find the files that are copied into the image or otherwise used during the build process. The Dockerfile should be located at the root of this context. In this case, Docker expects to find the Dockerfile in the current directory.

Building a container image using Dockerfile

Building a container image using Dockerfile

When you run this image, the ENTRYPOINT and CMD instructions specified in the Dockerfile are combined to form a final command that is executed in the container. For example, see this command:

docker run --rm --hostname demo-container ubuntu-custom

The --rm flag automatically removes the container when it exits. The --hostname demo-container flag sets the hostname of the container to "demo-container." Inside the container, if you run the hostname command, it will return demo-container. "ubuntu-custom" is the name of the image from which the container will be created.

Running a container image with the default command and arguments

Running a container image with the default command and arguments

Here, we created a container using a custom image. In this case, the final command executed in the container is printenv HOSTNAME PATH, which prints the values of the hostname and path variables to the standard output.

Sometimes, you might need to override the default command and arguments without modifying the container image itself. For instance, what if you want to pass different arguments than the hard-coded ones (i.e., HOSTNAME and PATH)? In this case, you can append the argument to the command like this:

docker run --rm --hostname demo-container ubuntu-custom HOME
Running a container image with custom arguments

Running a container image with custom arguments

This command uses the hard-coded default command printenv, but a custom argument is passed to it (i.e., HOME). As a result, you get to see the value of the HOME variable on the host as an output (i.e., /root). To override the default command itself, use the --entrypoint flag in the docker run command as shown below:

docker run --rm --entrypoint echo ubuntu-custom hello world!
Running a container image with the custom command and arguments

Running a container image with the custom command and arguments

Here, we changed the default command from printenv to echo and passed the hello world! string as an argument. As a result, you see hello world! as the container output.

Command and arguments in Kubernetes Pods

To learn how to use a command and arguments in Kubernetes Pods, let's create a Pod definition file.

apiVersion: v1
kind: Pod
metadata:
  name: python-pod
spec:
  containers:
  - name: python-container
    image: python
    command: ["python"]
    args: ["-c", "print('hello world!')"]
  restartPolicy: Never 
Using the command and arguments in a Kubernetes Pod

Using the command and arguments in a Kubernetes Pod

As you can see in the screenshot, there are two new fields under the spec.containers section: command and args.

  • The command field corresponds to the ENTRYPOINT instruction of a Dockerfile that specifies the default command or program for the container. This field is an array, so you can specify multiple comma-separated strings enclosed in quotes. For instance, to run your Python app in a container, you can set the command field as follows:
    command: ["python", "app.py"]
    		

    Remember, the first array item must be the command or executable itself, whereas the subsequent items could act as arguments.

  • The args field corresponds to the CMD instruction of a Dockerfile to override the default arguments provided in the container image. It is also an array, so you can specify multiple comma-separated arguments enclosed in quotes.

In the above example, we specified python as the default command and passed "-c" and "print('hello world!')" strings as arguments.

Now, apply this Pod manifest, and see what happens.

kubectl apply -f python-pod.yaml

This command applies the configuration defined in the python-pod.yaml file to create or update resources in a Kubernetes cluster.

Create a Kubernetes Pod and view its logs

Create a Kubernetes Pod and view its logs

The final command that is executed in the container is python -c print('hello world!'). As a result, you see hello world! output from the container. You can use the kubectl describe pods command to inspect the command and arguments of a Kubernetes Pod.

kubectl describe pods python-pod
Viewing the command and arguments of a Kubernetes Pod

Viewing the command and arguments of a Kubernetes Pod

Since the container command has finished, you will notice that the Pod status is shown as completed.

Subscribe to 4sysops newsletter!

Viewing the status of a Kubernetes Pod

Viewing the status of a Kubernetes Pod

Conclusion

You just learned how to use a command and arguments with Docker containers and Kubernetes Pods. This is quite useful for beginners to help customize the default container process and parameters. In the next post, we will discuss Kubernetes init containers and utilize the concepts learned in this post.

0 Comments

Leave a reply

Please enclose code in pre tags

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account