<p>I'm trying to run Stateful set with my own scripts, and I'm able to run first script that will spin up <code>mongodb</code> and setup some users etc, but the second script in <code>postStart</code> block, named <code>configure.sh</code> is never executed for some reason.</p>
<p>Here's the <code>StatefulSet</code> manifest <code>yaml</code>:</p>
<pre><code>apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongo
  labels:
    component: mongo
spec:
  selector:
    matchLabels:
      component: mongo
  serviceName: mongo
  replicas: 1
  template:
    metadata:
      labels:
        component: mongo
    spec:
      containers:
        - name: mongo
          image: mongo:latest
          command: [ &quot;/bin/bash&quot;, &quot;-c&quot; , &quot;+m&quot;]
          workingDir: /mongo/scripts
          args:
          - /mongo/scripts/mongo-start.sh
          livenessProbe:
            exec:
              command:
                - &quot;bin/bash&quot;
                - &quot;-c&quot;
                - mongo -u $MONGO_USER -p $MONGO_PASSWORD --eval db.adminCommand\(\&quot;ping\&quot;\)
            failureThreshold: 3
            successThreshold: 1
            periodSeconds: 10
            timeoutSeconds: 5
          ports:
            - containerPort: 27017
          imagePullPolicy: Always
          lifecycle:
             postStart:
               exec:
                 command: [&quot;/bin/bash&quot;, &quot;-c&quot;, &quot;/mongodb/configure.sh&quot;]
          volumeMounts:
            - name: mongo-persistent-storage
              mountPath: /data/db
            - name: mongo-scripts
              mountPath: /mongo/scripts
            - name: mongo-config
              mountPath: /mongodb/configure.sh
              subPath: configure.sh
          env:
            - name: MONGO_USER_APP_NAME
              valueFrom:
                configMapKeyRef:
                  key: MONGO_USER_APP_NAME
                  name: mongo-auth-env
            - name: MONGO_USER_APP_PASSWORD
              valueFrom:
                configMapKeyRef:
                  key: MONGO_USER_APP_PASSWORD
                  name: mongo-auth-env
            - name: MONGO_USER
              valueFrom:
                configMapKeyRef:
                  key: MONGO_USER
                  name: mongo-auth-env
            - name: MONGO_PASSWORD
              valueFrom:
                configMapKeyRef:
                  key: MONGO_PASSWORD
                  name: mongo-auth-env
            - name: MONGO_BIND_IP
              valueFrom:
                configMapKeyRef:
                  key: MONGO_BIND_IP
                  name: mongo-config-env              
      restartPolicy: Always
      volumes:
        - name: mongo-scripts
          configMap:
            name: mongo-scripts
            defaultMode: 0777
        - name: mongo-config
          configMap:
            name: mongo-config
            defaultMode: 0777
        - name: mongo-config-env
          configMap:
            name: mongo-config-env
            defaultMode: 0777
        - name: mongo-auth-env
          configMap:
            name: mongo-auth-env
            defaultMode: 0777
  volumeClaimTemplates:
    - metadata:
        name: mongo-persistent-storage
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 1Gi
</code></pre>
<p><code>mongo-start.sh</code> which is in <code>/scripts</code> folder with another scripts, is being executed, but after <code>Pod</code> is up and running, <code>configure.sh</code> is never executed, logs are not helpful, <code>kubectl describe pod</code> returns it as recognized, but it never runs. <code>ConfigMaps</code> are all deployed and their content and paths are also ok. Is there any other way to run the script after another, or I'm doing something wrong, been searching on <code>SO</code> and official docs, that's the only examples I found. Tnx</p>
<p><strong>EDIT</strong>
it started somehow, but with:</p>
<pre><code>Exec lifecycle hook ([/bin/bash -c /mongodb/mongodb-config.sh]) for Container &quot;mongo&quot; in Pod &quot;mongo-0_test(e9db216d-c1c2-4f19-b85e-19b210a22bbb)&quot; failed - error: command '/bin/bash -c /mongodb/mongodb-config.sh' exited with 1: , message: &quot;MongoDB shell version v4.2.12\nconnecting to: mongodb://mongo:27017/?authSource=admin&amp;compressors=disabled&amp;gssapiServiceName=mongodb\n2021-11-24T22:16:50.520+0000 E  QUERY    [js] Error: couldn't connect to server mongo:27017, connection attempt failed: SocketException: Error connecting to mongo:27017 (172.20.3.3:27017) :: caused by :: Connection refused :\nconnect@src/mongo/shell/mongo.js:353:17\n@(connect):2:6\n2021-11-24T22:16:50.522+0000 
</code></pre>
<p>Content of the configure.sh:</p>
<pre><code>#!/bin/bash
mongo --username $MONGO_USER_ROOT_NAME --password &quot;$MONGO_USER_ROOT_PASSWORD&quot; --authenticationDatabase &quot;$MONGO_AUTH_SOURCE&quot; --host mongo --port &quot;$MONGO_PORT&quot; &lt; create.js
</code></pre>
<p>If I remove postStart part, and init into container, I can successfully run the script..</p>

dejanmarich
<p>There is no guarantee that <code>postStart</code> hook will be call after container entry point. Also, <code>postStart</code> hook can be called more than once. The error was because by the time <code>configure.sh</code> was executed; the mongodb was not up and running yet. If your <code>configure.sh</code> script is idempotent, you can do a wait before proceed to next step:</p>
<pre><code>#!/bin/bash

until mongo --nodb --disableImplicitSessions --host mongo --username $MONGO_USER_ROOT_NAME --password $MONGO_USER_ROOT_PASSWORD --eval 'db.adminCommand(&quot;ping&quot;)'; do sleep 1; done

mongo --username $MONGO_USER_ROOT_NAME --password &quot;$MONGO_USER_ROOT_PASSWORD&quot; --authenticationDatabase &quot;$MONGO_AUTH_SOURCE&quot; --host mongo --port &quot;$MONGO_PORT&quot; &lt; create.js
</code></pre>
