Start kubernetes container with specific command

Kubernetes

Kubernetes Problem Overview


Using fleet I can specify a command to be run inside the container when it is started. It seems like this should be easily possible with Kubernetes as well, but I can't seem to find anything that says how. It seems like you have to create the container specifically to launch with a certain command.

Having a general purpose container and launching it with different arguments is far simpler than creating many different containers for specific cases, or setting and getting environment variables.

Is it possible to specify the command a kubernetes pod runs within the Docker image at startup?

Kubernetes Solutions


Solution 1 - Kubernetes

I spend 45 minutes looking for this. Then I post a question about it and find the solution 9 minutes later.

There is an hint at what I wanted inside the Cassandra example. The command line below the image:

id: cassandra
kind: Pod
apiVersion: v1beta1
desiredState:
  manifest:
    version: v1beta1
    id: cassandra
    containers:
      - name: cassandra
        image: kubernetes/cassandra
        command:
          - /run.sh
        cpu: 1000
        ports:
          - name: cql
            containerPort: 9042
          - name: thrift
            containerPort: 9160
        env:
          - key: MAX_HEAP_SIZE
            value: 512M
          - key: HEAP_NEWSIZE
            value: 100M
labels:
  name: cassandra

Despite finding the solution, it would be nice if there was somewhere obvious in the Kubernetes project where I could see all of the possible options for the various configuration files (pod, service, replication controller).

Solution 2 - Kubernetes

for those looking to use a command with parameters, you need to provide an array

for example

command: [ "bin/bash", "-c", "mycommand" ]

or also

command:
  - "bin/bash"
  - "-c"
  - "mycommand"

Solution 3 - Kubernetes

To answer Derek Mahar's question in the comments above:

What is the purpose of args if one could specify all arguments using command?

Dockerfiles can have an Entrypoint only or a CMD only or both of them together.

If used together then whatever is in CMD is passed to the command in ENTRYPOINT as arguments i.e.

ENTRYPOINT ["print"]
CMD ["hello", "world"]

So in Kubernetes when you specify a command i.e.

    command: ["print"]

It will override the value of Entrypoint in the container's Dockerfile.

If you only specify arguments then those arguments will be passed to whatever command is in the container's Entrypoint.

Solution 4 - Kubernetes

In order to specify the command a kubernetes pod runs within the Docker image at startup we need to include the command and args fields inside the yaml file for command and arguments to be passed. For example,

apiVersion: v1
kind: Pod
metadata:
    name: command-demo
    labels: 
        purpose: demo-command
spec:
    containers:
    - name: command-demo-container
      image: ubuntu
      command: ["/bin/sh"]
      args: ["-c", "while true; do echo hello; sleep 10;done"]

Solution 5 - Kubernetes

Additionally to the accepted answer, you can use variables with values from secrets in the commands as follows:

command: ["/some_command","-instances=$(<VARIABLE_NAME>)"]
env: 
- name: <VARIABLE_NAME>
  valueFrom:
    secretKeyRef:
      name: <secret_name>
      key: <secret_key>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionstatic416View Question on Stackoverflow
Solution 1 - Kubernetesstatic416View Answer on Stackoverflow
Solution 2 - KubernetesMrEView Answer on Stackoverflow
Solution 3 - KubernetesJonathanView Answer on Stackoverflow
Solution 4 - KubernetesManya TripathiView Answer on Stackoverflow
Solution 5 - KubernetesLeYAUableView Answer on Stackoverflow