<p>I am trying to get my app running on minikube, reachable at the minikube ip address. Below are the commands I am using to get it running. It all works as I want it to (if I try to log in, it returns the expected 401 since the mongo DB is empty).</p>

<p>I would like to actually include a database in my app with, say, an existing user. I cannot find a clear way with how to include data (say in a  tar file that works with mongo) with what I am doing. How can I accomplish this?</p>

<pre><code>### Get mongo up
docker pull mongo:3.2
kubectl run mongo --image=mongo:3.2 --port=27017
kubectl expose deployment mongo

### Get API up
cd /api
docker build -t api:v1 .
kubectl run api --image=api:v1 --port=3000 --env="MONGODB_URI=mongodb://mongo:27017/myapp-dev"
kubectl apply -f api-deployment.yml
kubectl expose deployment api --type=LoadBalancer

### Get dashboard up
docker build -t myapp-dashboard:v1 .
kubectl apply -f ./tooling/minikube/myapp-dashboard-qa-config-map.yml
kubectl apply -f ./tooling/minikube/myapp-dashboard-deployment.yml
kubectl expose deployment myapp-dashboard --type=LoadBalancer

### Ingress setup
minikube addons enable ingress
kubectl create -f ingress.yml
kubectl apply -f ./tooling/minikube/ingress.yml
</code></pre>

<p>The nice way of doing it making use of the <code>initdb</code> infrastructure in the <code>mongo</code> image. The <code>mongo:3.2</code> includes an <a href="https://github.com/docker-library/mongo/blob/master/3.2/docker-entrypoint.sh" rel="noreferrer"><code>entrypoint</code> shell script</a> that iterates trough <code>/docker-entrypoint-initdb.d/*.{sh,js}</code> (in the container).</p>

<p>Depending on the type of data you need to insert into the newly made database using a <a href="https://kubernetes.io/docs/concepts/configuration/secret/" rel="noreferrer"><code>ConfigMap</code></a> or a <a href="https://kubernetes.io/docs/concepts/configuration/secret/" rel="noreferrer"><code>Secret</code></a> as a volume is the way to go. But you can't do that with <code>kubectl run</code>.</p>

<p><strong>1.</strong> Create a <code>mongo.yaml</code> like this:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mongo
spec:
  template:
    metadata:
      labels:
        app: mongo
    spec:
      containers:
      - image: mongo:3.2
        name: mongo
        ports:
        - containerPort: 27017
        volumeMounts:
        - name: mongo-initdb
          mountPath: /docker-entrypoint-initdb.d
      volumes:
      - name: mongo-initdb
        configMap:
          name: mongo-initdb
</code></pre>

<p><strong>2.</strong> Create <code>createuser.sh</code> like this (just an <a href="https://docs.mongodb.com/manual/tutorial/create-users/" rel="noreferrer">example</a>):</p>

<pre><code>mongo &lt;&lt;EOF
use reporting
db.createUser(
  {
    user: "reportsUser",
    pwd: "12345678",
    roles: [
       { role: "read", db: "reporting" },
       { role: "read", db: "products" },
       { role: "read", db: "sales" },
       { role: "readWrite", db: "accounts" }
    ]
  }
)
EOF
</code></pre>

<p><strong>3.</strong> Create the <a href="https://docs.mongodb.com/manual/tutorial/create-users/" rel="noreferrer"><code>ConfigMap</code></a>:</p>

<pre><code>$ kubectl create configmap mongo-initdb --from-file=createuser.sh
configmap "mongo-initdb" created
</code></pre>

<p><strong>4.</strong> Create the mongo deployment (instead of <code>kubectl run mongo --image=mongo:3.2 --port=27017</code>):</p>

<pre><code>$ kubectl apply -f mongo.yaml
deployment "mongo" created
</code></pre>

<p><strong>5.</strong> Check the logs:</p>

<pre><code>$ kubectl logs -f deploy/mongo
[...]
/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/createuser.sh
MongoDB shell version: 3.2.17
[...]
Successfully added user: {
[...]
</code></pre>

<p>As I mentioned a very similar solution can be done using <a href="https://kubernetes.io/docs/concepts/configuration/secret/" rel="noreferrer"><code>Secrets</code></a> for sensitive data. The <code>kubectl create configmap --from-file=...</code> command also accepts a name of a directory instead of a file and will work as expected.</p>
