<p>I'm trying to deploy <code>mongo</code> in <code>Kubernetes</code>, but before I run the <code>mongo</code> itself, it should do some prerequisites in init containers.</p>
<p>This is the list of <code>configMaps</code></p>
<pre><code>mongo-auth-env       4      16m
mongo-config         1      16m
mongo-config-env     7      16m
mongo-scripts        10     16m
</code></pre>
<p><code>StatefulSet</code> looks like:</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:
      initContainers:
        - name: mongo-init
          image: curlimages/curl:latest
          volumeMounts:
            - mountPath: /mongodb/mongodb-config.sh
              name: mongo-config
              subPath: mongodb-config.sh
            - mountPath: /mongo/scripts
              name: mongo-scripts
      containers:
        - name: mongo
          image: bitnami/mongodb:latest
          command: [ &quot;/bin/sh&quot;, &quot;-c&quot; ]
          args:
          - /scripts/mongo-run.sh
          livenessProbe:
            exec:
              command:
                - '[ -f /data/health.check ] &amp;&amp; exit 0 || exit 1'
            failureThreshold: 300
            periodSeconds: 2
            timeoutSeconds: 60
          ports:
            - containerPort: 27017
          imagePullPolicy: Always
          volumeMounts:
            - name: mongo-persistent-storage
              mountPath: /data/db
            - name: mongo-scripts
              mountPath: /mongo/scripts
          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_INITDB_ROOT_USERNAME
              valueFrom:
                configMapKeyRef:
                  key: MONGO_USER_ROOT_NAME
                  name: mongo-auth-env
            - name: MONGO_INITDB_ROOT_PASSWORD
              valueFrom:
                configMapKeyRef:
                  key: MONGO_USER_ROOT_PASSWORD
                  name: mongo-auth-env
            - name: MONGO_WIREDTIGER_CACHE_SIZE
              valueFrom:
                configMapKeyRef:
                  key: MONGO_WIREDTIGER_CACHE_SIZE
                  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
  volumeClaimTemplates:
    - metadata:
        name: mongo-persistent-storage
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 1Gi
</code></pre>
<p>Pod description:</p>
<pre><code>Init Containers:
  mongo-init:
    Container ID:   docker://9a4c20c9b67470af03ee4f60a24eabc428ecafd3875b398ac52a54fe3b2b7b96
    Image:          curlimages/curl:latest
    Image ID:       docker-pullable://curlimages/curl@sha256:d588ff348c251f8e4d1b2053125c34d719a98ff3ef20895c49684b3743995073
    Port:           &lt;none&gt;
    Host Port:      &lt;none&gt;
    State:          Waiting
      Reason:       CrashLoopBackOff
    Last State:     Terminated
      Reason:       Error
      Exit Code:    2
      Started:      Fri, 19 Nov 2021 02:04:16 +0100
      Finished:     Fri, 19 Nov 2021 02:04:16 +0100
    Ready:          False
    Restart Count:  8
    Environment:    &lt;none&gt;
    Mounts:
      /mongo/scripts from mongo-scripts (rw)
      /mongodb/mongodb-config.sh from mongo-config (rw,path=&quot;mongodb-config.sh&quot;)
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-9jwkz (ro)
Containers:
  mongo:
    Container ID:  
    Image:         mongo:4.2.12-bionic
    Image ID:      
    Port:          27017/TCP
    Host Port:     0/TCP
    Command:
      /bin/sh
      -c
    Args:
      /scripts/mongo-run.sh
    State:          Waiting
      Reason:       PodInitializing
    Ready:          False
    Restart Count:  0
    Liveness:       exec [[ -f /data/health.check ] &amp;&amp; exit 0 || exit 1] delay=0s timeout=60s period=2s #success=1 #failure=300
    Environment:
      MONGO_USER_APP_NAME:          &lt;set to the key 'MONGO_USER_APP_NAME' of config map 'mongo-auth-env'&gt;            Optional: false
      MONGO_USER_APP_PASSWORD:      &lt;set to the key 'MONGO_USER_APP_PASSWORD' of config map 'mongo-auth-env'&gt;        Optional: false
      MONGO_INITDB_ROOT_USERNAME:   &lt;set to the key 'MONGO_USER_ROOT_NAME' of config map 'mongo-auth-env'&gt;           Optional: false
      MONGO_INITDB_ROOT_PASSWORD:   &lt;set to the key 'MONGO_USER_ROOT_PASSWORD' of config map 'mongo-auth-env'&gt;       Optional: false
      MONGO_WIREDTIGER_CACHE_SIZE:  &lt;set to the key 'MONGO_WIREDTIGER_CACHE_SIZE' of config map 'mongo-config-env'&gt;  Optional: false
    Mounts:
      /data/db from mongo-persistent-storage (rw)
      /mongo/scripts from mongo-scripts (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-9jwkz (ro)
Conditions:
  Type              Status
  Initialized       False 
  Ready             False 
  ContainersReady   False 
  PodScheduled      True 
Volumes:
  mongo-persistent-storage:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  mongo-persistent-storage-mongo-0
    ReadOnly:   false
  mongo-scripts:
    Type:      ConfigMap (a volume populated by a ConfigMap)
    Name:      mongo-scripts
    Optional:  false
  mongo-config:
    Type:      ConfigMap (a volume populated by a ConfigMap)
    Name:      mongo-config
    Optional:  false
</code></pre>
<p>After all files/folders are mounted, there is a <code>run.sh</code> script in the <code>scripts</code> directory which should spin things up, but I can't get to that part because the init container is crashing. Is there any other way of doing this, I tried with <code>Jobs</code> but without success. Am I missing something obvious again, because I can see <code>configMaps</code> in the description, but unfortunately can't get logs because Pod is always initializing, and can't exec into the container because of the same reason? I followed some ideas from <a href="https://github.com/markpeterfejes/mongo-repl-init-container" rel="nofollow noreferrer">this repo</a>. Any input would be appreciated.</p>

dejanmarich
<p>To clarify I am posting Community Wiki answer.</p>
<p>The problem here was small mistake - incorrect path (<code>/scripts/mongo-run.sh</code>) - the correct one: <code>/mongodb/scripts/mongo-run.sh</code></p>
<blockquote>
<p>@dejanmarich: correct path s /mongodb/scripts/mongo-run.sh, i just missed that, fixed now</p>
</blockquote>
