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

<p>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:</p>

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

<p>In docker-entrypoint.sh, the last command is</p>

<pre><code>exec gosu abc "$@"
</code></pre>

<p>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...</p>

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

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

<p>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:</p>

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

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

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

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

<p>Thanks for your help!</p>

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

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

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

<p>Notice that you have to spawn a new TTY in order to run the command as <a href="https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#notes" rel="nofollow noreferrer">the image's <code>entrypoint</code> is replaced when running commands in the container spec in Kubernetes</a>.</p>
