- Update container images with Copa - Mon, Nov 27 2023
- Deploying stateful applications with Kubernetes StatefulSets - Wed, Nov 1 2023
- Install and enable IIS Manager for Remote Administration - Thu, Oct 26 2023
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"]
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.
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.
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
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!
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
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.
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
Since the container command has finished, you will notice that the Pod status is shown as completed.
Subscribe to 4sysops newsletter!
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.
Read the latest IT news and community updates!
Join our IT community and read articles without ads!
Do you want to write for 4sysops? We are looking for new authors.