Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start kubernetes container with specific command

Tags:

kubernetes

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?

like image 992
static416 Avatar asked Mar 11 '15 00:03

static416


People also ask

How do I run a command in a container?

Running Commands in an Alternate Directory in a Docker Container. To run a command in a certain directory of your container, use the --workdir flag to specify the directory: docker exec --workdir /tmp container-name pwd.

What is the difference between command and args in Kubernetes?

If you supply a command but no args for a Container, only the supplied command is used. The default EntryPoint and the default Cmd defined in the Docker image are ignored. If you supply only args for a Container, the default Entrypoint defined in the Docker image is run with the args that you supplied.


2 Answers

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

like image 184
static416 Avatar answered Oct 02 '22 21:10

static416


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" 
like image 45
MrE Avatar answered Oct 02 '22 21:10

MrE