<p>I want to run two containers on a single pod.</p>
<p><code>container1</code> is a test that tries to connect to a SQL Server Database that runs on <code>container2</code>.</p>
<p>How can I make sure that the sql container (<code>container2</code>) will run and be ready before <code>container1</code> starts?</p>
<p><code>initContainer</code> won't work here, as it will run before both containers.</p>
<p>This is my compose.yaml:</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: sql-test-pod
  labels:
    name: sql-test
spec:
  restartPolicy: Never
  containers:
    - name: my-sqldb
      image: docker-registry.com/database
      imagePullPolicy: Always
      resources:
        limits:
          memory: &quot;4096Mi&quot;
          cpu: &quot;750m&quot;
        requests:
          memory: &quot;4096Mi&quot;
          cpu: &quot;750m&quot;
    - name: tests
      tty: true
      stdin: true
      image: docker-registry.com/test
      imagePullPolicy: Always
      resources:
        limits:
          memory: &quot;4096Mi&quot;
          cpu: &quot;750m&quot;
        requests:
          memory: &quot;4096Mi&quot;
          cpu: &quot;750m&quot;
      env:
      - name: sqlhostname
        value: &quot;SqlHostnamePlaceholder&quot;
  nodeSelector:
    kubernetes.io/os: windows
  tolerations:
  - key: &quot;windows&quot;
    operator: &quot;Equal&quot;
    value: &quot;2019&quot;
    effect: &quot;NoSchedule&quot;
</code></pre>

Gal I.
<p>In order to make sure that <code>container1</code> will start only after <code>container2</code>'s SQL Server is up the only way I found is to use <code>postStart</code> container's lifecycle event.
<code>postStart</code> triggered after after the container is create, it is true that  there is no guarantee, that the postStart handler is called before the Container's entrypoint is called, <strong>but it turns out that the Kubelet code that starts the container blocks the start of the next container until the post-start handler terminates.</strong></p>
<p>And this is how my new compose file will look like:</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: sql-test-pod
  labels:
    name: sql-test
spec:
  restartPolicy: Never
  containers:
    - name: my-sqldb
      image: docker-registry.com/database
      imagePullPolicy: Always
      lifecycle:
        postStart:
          exec:
            command: ['powershell.exe', '-command', &quot;$connectionString = 'Server=sql-test-pod;User Id=user;Password=password'; $sqlConnection = New-Object System.Data.SqlClient.SqlConnection $connectionString; $i=0; while($i -lt 6) {Try { $i++;$sqlConnection.Open();$sqlConnection.Close(); return}Catch {Write-Error $_; start-sleep 30}}&quot;]
      resources:
        limits:
          memory: &quot;4096Mi&quot;
          cpu: &quot;750m&quot;
        requests:
          memory: &quot;4096Mi&quot;
          cpu: &quot;750m&quot;
    - name: tests
      tty: true
      stdin: true
      image: docker-registry.com/test
      imagePullPolicy: Always
      resources:
        limits:
          memory: &quot;4096Mi&quot;
          cpu: &quot;750m&quot;
        requests:
          memory: &quot;4096Mi&quot;
          cpu: &quot;750m&quot;
      env:
      - name: sqlhostname
        value: &quot;sql-test-pod&quot;
  nodeSelector:
    kubernetes.io/os: windows
  tolerations:
  - key: &quot;windows&quot;
    operator: &quot;Equal&quot;
    value: &quot;2019&quot;
    effect: &quot;NoSchedule&quot;
</code></pre>
<p>Similar case you can find <a href="https://medium.com/@marko.luksa/delaying-application-start-until-sidecar-is-ready-2ec2d21a7b74" rel="nofollow noreferrer">here</a></p>
