How to create a configmap which references the .JS file containing the MongoDB commands?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
If you want Kubernetes to provide a JavaScript file containing MongoDB commands, the normal pattern is to store the file contents in a ConfigMap and mount that ConfigMap into a pod. The ConfigMap does not execute the script by itself; it only makes the file available to a container that runs mongosh or another MongoDB shell command.
Start With the JavaScript File
Suppose you have an initialization script named init.js.
This is ordinary text, which makes it a good fit for ConfigMap data.
Create the ConfigMap From the File
The simplest command is:
That stores the file content under the key init.js.
If you prefer a declarative manifest:
Both approaches produce the same basic result: a ConfigMap containing the JavaScript source.
Mount the ConfigMap Into a Container
The script becomes useful only after it is mounted into a pod.
Now the file appears at /scripts/init.js and the container explicitly runs it with mongosh.
ConfigMap vs "Reference"
The title says "references the JS file," but in Kubernetes terms the ConfigMap does not store a pointer to some external file on disk. It stores the file content as ConfigMap data, then mounts that content into the container filesystem.
So the usual real workflow is:
- create the file
- put its contents into a ConfigMap
- mount the ConfigMap
- run the script from the mounted path
Keep Secrets Separate
Initialization scripts often need database credentials, but ConfigMaps are not for secrets. If authentication values are required, put them in a Kubernetes Secret and pass them to the pod via environment variables or mounted secret files.
That keeps the script and the sensitive values separated properly.
Jobs and Init Containers Are Common Execution Points
In practice, teams usually run MongoDB setup scripts in one of two places: a dedicated Job that can be rerun intentionally, or an init-style startup hook supported by a container image. The right choice depends on whether the script is a one-time database bootstrap step or part of each environment's startup flow.
That distinction matters operationally, because rerunnable jobs are often safer for troubleshooting than hiding all initialization logic inside an image startup path.
Common Pitfalls
The biggest pitfall is assuming the ConfigMap executes the JavaScript. It does not. A pod command or image entrypoint has to run the script explicitly.
Another common mistake is creating the ConfigMap correctly but forgetting to mount it into the container. If the file is never mounted, the container cannot execute it.
Developers also sometimes put passwords directly into the ConfigMap because the file is convenient. That is poor security practice; use Secrets for sensitive data.
Summary
- Put the MongoDB JavaScript file contents into a ConfigMap.
- Mount the ConfigMap into a pod so the file appears in the container filesystem.
- Run the mounted file with
mongoshor another MongoDB shell command. - A ConfigMap stores file content; it does not execute scripts automatically.
- Keep credentials in Secrets, not in the ConfigMap itself.
Related reading
- How to create a kubectl config file for serviceaccount
- How to create a local development environment for Kubernetes?
- How to create a namespace if it doesn't exists from HELM templates?
- How to create a post-init container in Kubernetes?
- How to create a database from shell command in MySQL?
- How to create a DB for MongoDB container on start up?
- How to create a folder in an AWS S3 bucket using Terraform?
- How to create a new docker image from a running container on Amazon?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.