MongoDB with Docker and initial seed

Kevin Martins
1 min readMar 2, 2020

--

Some steps to create a MongoDB instance with initial data using Docker and Docker Compose

MongoDB logo

Create a JSON file called init.json with your document:

[
{
"name": "Kevin Martins",
"birthday": "29 April , 1995",
"nick": "Red King",
"age": 24
},
{
"name": "Kauan Martins",
"birthday": "02 July , 2008",
"nick": "Red as Floid",
"age": 11
}
]

Create a Dockerfile and copy your JSON file:

FROM mongo

COPY init.json /init.json
CMD mongoimport --host mongo --db reach-engine --collection users --type json --file /init.json --jsonArray

Create a docker-compose.yml and build your Dockerfile :

version: '3'
services:
mongo:
image: mongo:latest
ports:
- "27017:27017"
volumes:
- ./data/db:/data/db
mongo-seed:
build: ./mongo-seed
links:
- mongo

Run docker-compose up and it’s ready !

--

--

Kevin Martins
Kevin Martins

Written by Kevin Martins

A telecommunications engineer who likes to write codes. https://kevinmmartins.github.io/

No responses yet