<p>I need to execute multiple line of code in a container, which getting passed as args, like in the following example:
deployment.yaml excerpt</p>
<pre><code>initContainers:
 - name: wait-for-migrations
   args:
    - python
    - -c
    - |
        print(&quot;python&quot;)
        ...more lines
    - bash
    - -c
    - |
        echo bash
        ...more lines


</code></pre>
<p>Right now, no matter what I have tried, I cannot get both python/bash to be executed.. only the first one always is.
Chaining both with in-between:</p>
<pre><code>- ;
</code></pre>
<p>Did not work either.</p>
<p>Any ideas how to implement it?</p>

Bennimi
<p>I think it would be better to create a dedicated script for this. Doing it the way I am showing below is somewhat dirty.</p>
<p>It's a limitation of containers in general. You cannot run multiple commands. This has nothing to do with Kubernetes in that sense.
The usual workaround for complex commands is to wrap everything in a single <code>bash -c</code>, i.e. <code>bash -c &quot;first command; second command&quot;</code>.
Often times we also see them join with <code>&amp;&amp;</code> which would not execute any further commands if the current command failed, i.e. <code>bash -c &quot;first command &amp;&amp; second command&quot;.</code></p>
<p>You don't have to separate those in <code>command</code> and <code>args</code> but I think it's a bit cleaner.</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Pod
metadata:
  name: script
spec:
  containers:
    - name: script
      image: python
      command:
        - bash
        - -c
      args:
        - |
          echo &quot;hello world&quot;;
          python -c '
          print(&quot;foo&quot;)
          print(&quot;bar&quot;)
          print(&quot;baz&quot;)
          ';
          sleep 180;
</code></pre>
<p>That said, since you want to wait for a migration, it may be cleaner to move the migration to a Kubernetes job and check the Kubernetes API in the init container if the job is finished. For example with kubectl.
The migration job could run the same image as the main application but performing its migration command, if it has one.</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: batch/v1
kind: Job
metadata:
  name: migration
spec:
  ttlSecondsAfterFinished: 100
  template:
    spec:
      containers:
        - name: migration
          image: busybox
          command: [&quot;/bin/sh&quot;, &quot;-c&quot;, &quot;sleep 20&quot;]
      restartPolicy: Never
---
apiVersion: v1
kind: Pod
metadata:
  name: app
spec:
  containers:
    - name: app
      image: busybox
      command: [&quot;/bin/sh&quot;, &quot;-c&quot;, &quot;sleep 100&quot;]
  initContainers:
    - name: wait-for-migration
      image: bitnami/kubectl
      args:
        - wait
        - job/migration
        - --for
        - condition=complete
        - --timeout
        - 60s
</code></pre>
