Managing MongoDB on docker with docker-compose
Today I learn how to manage MongoDB on docker with docker-compose, and I want to share with you.
docker + docker-compose
I just learned how to use docker with docker-compose to simplified my full-stack side-project. It’s easy to make a project with docker as a container to images like MongoDB, Redis, Elastic Search, etc. You only need to pull the image when you need it and remove the container after you don’t need them.
To-Do List
- Pull MongoDB image
- Create a file for initiate authenticated database and user
- Write docker-compose file
- Login to MongoDB with created credentials
Pull MongoDB image
pull latest MongoDB image by executing command below
$ docker pull mongo:latest
Show pulled images by executing $ docker images
make sure, your mongo image is there.
Create a file for initiate authenticated database and user
Create a file named init-mongo.js
or whatever as long as it’s a Javascript file
Write docker-compose file
Create a file named docker-compose.yml for setup your docker-compose stack. Here is our current directory structure
Explanation
version
is a version of docker-compose file format, you can change to the latest versiondatabase
on line 3 is just a service name, you can change the name whatever you wantimage
must be mongo, because you want to create a container frommongo
imagecontainer_name
is a name for your container, it’s optionalenvironment
is a variables that will be used on the mongo containerMONGO_INITDB_DATABASE
you fill with a database name that you want to create, make it same like init-mongo.jsMONGO_INITDB_ROOT_USERNAME
you fill with username of root that you wantMONGO_INITDB_ROOT_PASSWORD
you fill with password of root that you wantvolumes
to define a file/folder that you want to use for the container./init-mongo.js:/docker-entrypoint-initdb.d/init-mongo-js:ro
means you want to copy init-mongo.js to/docker-entrypoint-initdb.d/
as a read only file./docker-entrypoint-initdb.d
is a folder that already created inside the mongo container used for initiating database, so we copy our script to that folder./mongo-volume:/data/db
means you want to set data on container persist on your local folder namedmongo-volume
./data/db/
is a folder that already created inside the mongo container.ports
is to define which ports you want to expose and define, in this case I use default mongoDB port27017
until27019
Execution
Now run the docker-compose file with $ docker-compose up
or$ docker-compose up -d
to run containers in the background
Open another terminal to login to the container. Type $ docker container ls
to see our running container
Login to your container by using container names
$ docker exec -it <container-name> bash
Login to MongoDB with created User & Database by using
$ mongo -u <your username> -p <your password> --authenticationDatabase <your database name>### OR ###$ mongo -u <your username> --authenticationDatabase <your database name>
Finally
Now you have created your Persistent MongoDB container, your MongoDB user, superuser, and database. You can connect your program to the database by using this URL as a connection mongodb://YourUsername:YourPasswordHere@127.0.0.1:27017/your-database-name
Thank you for reading!
Follow us on Twitter 🐦 and Facebook 👥 and join our Facebook Group 💬.
To join our community Slack 🗣️ and read our weekly Faun topics 🗞️, click here⬇