Lune Logo

© 2025 Lune Inc.
All rights reserved.

support@lune.dev

Want to use over 200+ MCP servers inside your coding tools like Cursor?

Asked 1 month ago by QuantumWatcher168

Troubleshooting MongoDB Seeding on Minikube with Skaffold: ContainerCannotRun Error

The post content has been automatically edited by the Moderator Agent for consistency and clarity.

I'm deploying MongoDB on my local Minikube cluster using Skaffold and everything works fine for the basic deployment. However, I also need to seed the database with JSON data and ran into a problem where the mongo-seed pod won’t start, reporting a ContainerCannotRun error.

I initially set up my deployment with the following files:

skaffold.yaml

YAML
apiVersion: skaffold/v2beta26 kind: Config metadata: name: mongodb deploy: kubectl: manifests: - "config/namespace.yaml" - "config/mongodb.yaml" defaultNamespace: "mongodb"

config/namespace.yaml

YAML
kind: Namespace apiVersion: v1 metadata: name: mongodb labels: name: mongodb

config/mongodb.yaml

YAML
apiVersion: apps/v1 kind: Deployment metadata: name: mongodb-mongo-depl spec: replicas: 1 selector: matchLabels: app: mongodb-mongo template: metadata: labels: app: mongodb-mongo spec: containers: - name: mongodb-mongo image: mongo --- apiVersion: v1 kind: Service metadata: name: mongodb-mongo-srv spec: selector: app: mongodb-mongo ports: - name: db protocol: TCP port: 27017 targetPort: 27017

This configuration successfully launches a MongoDB instance. I then attempted to seed the database by creating a ConfigMap and a Job. Below is my updated setup:

skaffold.yaml

YAML
apiVersion: skaffold/v2beta26 kind: Config metadata: name: mongodb deploy: kubectl: manifests: - "config/namespace.yaml" - "config/configmap.yaml" - "config/mongodb.yaml" - "config/mongo-seed-job.yaml" defaultNamespace: "mongodb"

congigmap.yaml

YAML
apiVersion: v1 kind: ConfigMap metadata: name: seed-data namespace: mongodb data: init.json: | [{"name":"Joe Smith","email":"jsmith@gmail.com","age":40,"admin":false},{"name":"Jen Ford","email":"jford@gmail.com","age":45,"admin":true}]

mongo-seed-job.yaml

YAML
apiVersion: batch/v1 kind: Job metadata: name: mongo-seed spec: template: spec: containers: - name: seed image: mongo:latest command: ["sh", "-c", "mongoimport --uri mongodb://mongodb:27017/mydb --collection accounts --type json --file '/init.json' --jsonArray"] volumeMounts: - name: seed-data mountPath: /data volumes: - name: seed-data configMap: name: seed-data items: - key: init.json path: init.json restartPolicy: Never

The issue seems to be related to a mismatch between the file path specified in the mongoimport command and the actual mount path of the ConfigMap. Is there a way to seed the database correctly using Skaffold in this setup? Any insights or corrections would be greatly appreciated.

0

Comments 0

Answers 2

Views 87

2 Answers

Answer by StackOverflowTychoAI Agent 1 month ago

0

Below is one common cause for ContainerCannotRun in this scenario: the mongoimport command is looking for the JSON file at /init.json, but the ConfigMap is mounted at /data (making the file available at /data/init.json). In other words, the file path in your command and your volume mount don’t match.

To resolve this issue you can either:

  1. Update the command to reference the correct file location, for example:
YAML
command: ["sh", "-c", "mongoimport --uri mongodb://mongodb:27017/mydb --collection accounts --type json --file '/data/init.json' --jsonArray"]
  1. Or change the volumeMount’s mountPath so that it makes the file available at /init.json. For example:
YAML
volumeMounts: - name: seed-data mountPath: /

Make sure the file is available at the path specified in the mongoimport command.

Adjusting one of these should allow the job’s container to start successfully.

Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?

No comments yet.

Answer by QuasarPioneer492 1 month ago

0

I managed to get it working. Here is the working code if anyone else needs it:

skaffold.yaml

YAML
apiVersion: skaffold/v2beta26 kind: Config metadata: name: mongodb deploy: kubectl: manifests: - "config/namespace.yaml" - "config/mongodb-credentials.yaml" - "config/configmap.yaml" - "config/mongodb.yaml" - "config/mongo-seed-job.yaml" defaultNamespace: "mongodb"

config/namespace.yaml

YAML
kind: Namespace apiVersion: v1 metadata: name: mongodb labels: name: mongodb

config/mongodb-credentials.yaml

Note: username: admin password: password

Please change this to whatever you want

YAML
apiVersion: v1 kind: Secret metadata: name: mongodb-credentials type: Opaque data: username: YWRtaW4= password: cGFzc3dvcmQ=

config/configmap.yaml

YAML
apiVersion: v1 kind: ConfigMap metadata: name: seed-data data: init.json: | [{"name":"Joe Smith","email":"jsmith@gmail.com","age":40,"admin":false},{"name":"Jen Ford","email":"jford@gmail.com","age":45,"admin":true}]

config/mongodb.yaml

YAML
apiVersion: apps/v1 kind: Deployment metadata: name: mongodb spec: replicas: 1 selector: matchLabels: app: mongodb template: metadata: labels: app: mongodb spec: containers: - name: mongodb image: mongo:latest ports: - containerPort: 27017 volumeMounts: - name: mongo-data mountPath: /data/db env: - name: MONGO_INITDB_ROOT_USERNAME valueFrom: secretKeyRef: name: mongodb-credentials key: username - name: MONGO_INITDB_ROOT_PASSWORD valueFrom: secretKeyRef: name: mongodb-credentials key: password volumes: - name: mongo-data emptyDir: {} --- apiVersion: v1 kind: Service metadata: name: mongodb spec: ports: - port: 27017 selector: app: mongodb

config/mongo-seed-job.yaml

YAML
apiVersion: batch/v1 kind: Job metadata: name: mongo-seed spec: template: spec: initContainers: - name: init-copy image: busybox command: ['sh', '-c', 'cp /config/init.json /data/'] volumeMounts: - name: config-volume mountPath: /config - name: data-volume mountPath: /data containers: - name: seed image: mongo:latest command: ["sh", "-c", "mongoimport --uri mongodb://$(MONGO_USERNAME):$(MONGO_PASSWORD)@mongodb:27017/mydb --collection accounts --type json --file /data/init.json --jsonArray --authenticationDatabase=admin"] env: - name: MONGO_USERNAME valueFrom: secretKeyRef: name: mongodb-credentials key: username - name: MONGO_PASSWORD valueFrom: secretKeyRef: name: mongodb-credentials key: password volumeMounts: - name: data-volume mountPath: /data restartPolicy: Never volumes: - name: config-volume configMap: name: seed-data - name: data-volume emptyDir: {}

If anyone has any alternate solutions it would be good to know.

Thanks @imran-premnawaz for your help

No comments yet.

Discussion

No comments yet.