How do I seed a mongo database using docker-compose?
I do this using another docker container whose only purpose is to seed mongo, then exit. I suspect this is the same idea as ebaxt's, but when I was looking for an answer to this, I just wanted to see a quick-and-dirty, yet straightforward, example. So here is mine:
docker-compose.yml
mongodb: image: mongo ports: - "27017:27017"mongo-seed: build: ./mongo-seed links: - mongodb# my webserver which uses mongo (not shown in example)webserver: build: ./webserver ports: - "80:80" links: - mongodb
mongo-seed/Dockerfile
FROM mongoCOPY init.json /init.jsonCMD mongoimport --host mongodb --db reach-engine --collection MyDummyCollection --type json --file /init.json --jsonArray
mongo-seed/init.json
[ { "name": "Joe Smith", "email": "jsmith@gmail.com", "age": 40, "admin": false }, { "name": "Jen Ford", "email": "jford@gmail.com", "age": 45, "admin": true }]
I have found useful to use Docker Custom Images and using volumes, instead of creating another container for seeding.
File Structure
.├── docker-compose.yml├── mongo│ ├── data│ ├── Dockerfile│ └── init-db.d│ └── seed.js
Every File location mentioned in
Dockerfile
/docker-compose.yml
, is relative to location ofdocker-compose.yml
DOCKERFILE
FROM mongo:3.6COPY ./init-db.d/seed.js /docker-entrypoint-initdb.d
docker-compose.yml
version: '3'services: db: build: ./mongo restart: always volumes: - ./mongo/data:/data/db #Helps to store MongoDB data in `./mongo/data` environment: MONGO_INITDB_ROOT_USERNAME: {{USERNAME}} MONGO_INITDB_ROOT_PASSWORD: {{PWD}} MONGO_INITDB_DATABASE: {{DBNAME}}
seed.js
// Since Seeding in Mongo is done in alphabetical order... It's is important to keep// file names alphabetically ordered, if multiple files are to be run.db.test.drop();db.test.insertMany([ { _id: 1, name: 'Tensor', age: 6 }, { _id: 2, name: 'Flow', age: 10 }])
docker-entrypoint-initdb.d
can be used for creating different users and mongodb administration related stuffs, just create an alphabetical ordered named js-script tocreateUser
etc...
For more details on how to customize MongoDB Docker service, read this
Also, it is good to keep your passwords and usernames secure from Public, DO NOT push credentials on public git, instead use Docker Secrets. Also read this Tutorial on Secrets
Do note, it is not necessary to go into docker-swarm mode to use secrets. Compose Files supports secrets as well. Check this
Secrets can also be used in MongoDB Docker Services
Current answer based on @Jeff Fairley answer and updated according to new Docker docs
docker-compose.yml
version: "3.5"services: mongo: container_name: mongo_dev image: mongo:latest ports: - 27017:27017 networks: - dev mongo_seed: container_name: mongo_seed build: . networks: - dev depends_on: - mongonetworks: dev: name: dev driver: bridge
Dockerfile
FROM mongo:latestCOPY elements.json /elements.jsonCMD mongoimport --host mongo --db mendeleev --collection elements --drop --file /elements.json --jsonArray
You probably need to rebuild current images.