How to run multiple commands with gosu in Kubernetes job

7/24/2019

I am defining a Kubernetes job to run a rake task but stuck in how to write the command...

I am new to K8s and trying to run a Rails application in K8s. In my Rails app Dockerfile, I created a user , copied code to /home/abc and installed rvm and rails in this user, and also specified an entrypoint and command:

ENTRYPOINT ["/home/abc/docker-entrypoint.sh"]
CMD bash -l -c "cd /home/abc && rvm use 2.2.10 --default && rake db:migrate && exec /usr/bin/supervisord -c config/supervisord.conf"

In docker-entrypoint.sh, the last command is

exec gosu abc "$@"

The goal is to at the end, gosu to user abc, and then run db migration and start the server through supervisord. It works, although I dont know whether it is a good practice or not...

Now I would like to run another rake task for some purpose. Firstly, I tried to run it using kubectl exec command:

kubectl exec my-app-deployment-xxxx -- gosu abc bash -l -c 'cd /home/abc && rvm use 2.2.10 --default && rake app:init:test_task'

It worked, but it requires to know the pod id, which is dynamic. so I tried to create a K8s job and specify in the command:

containers:
- name: my-app
  image: my-app:v0.2
  command: 
  - "gosu"
  - "abc"
  - "bash -l -c 'cd /home/abc && rvm use 2.2.10 --default && rake app:init:test_task'"

I expect the job can be completed successfully, but it failed, and the error info when kubectl logs job_pod is like:

error: exec: "bash -l -c 'cd /home/abc && rvm use 2.2.10 --default && rake app:init:test_task'": stat bash -l -c cd /home/abc && rvm use 2.2.10 --default && rake app:init:test_task': no such file or directory

I think it should be because of how to write the 'command' part to run multiple commands with gosu...

Thanks for your help!

-- JohnZhang
command
jobs
kubernetes
libgosu

1 Answer

7/24/2019

Since gosu takes the user name and the Bash shell as arguments, I'd say that this is one rather than three separate commands.

Given that, there can be only one single entrypoint in each container, you can try running it as follows:

containers:
- name: my-app
 image: my-app:v0.2
command: ["/bin/sh", "-c", "gosu username bash -l -c 'cd /home/abc && rvm use 2.2.10 --default && rake app:init:test_task'"]

Notice that you have to spawn a new TTY in order to run the command as the image's entrypoint is replaced when running commands in the container spec in Kubernetes.

-- yyyyahir
Source: StackOverflow