64

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?

5 Answers 5

63

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).

6
55

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"
4
  • 1
    one line is one parameter. if you have spaces in your parameters, that's fine, but it won't be parsed as multiple parameters
    – MrE
    Commented Sep 16, 2016 at 22:28
  • 3
    What is the purpose of args if one could specify all arguments using command? Commented Sep 17, 2016 at 0:53
  • 2
    you may have an entrypoint with a command and only want to pass arguments to it
    – MrE
    Commented Sep 17, 2016 at 1:30
  • 1
    I've tried to do command: ["npm", "run", "serve"], and it did not worked well Commented Apr 25, 2017 at 1:56
19

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.

0
6

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"]
0

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>

Not the answer you're looking for? Browse other questions tagged or ask your own question.