Managing MongoDB on docker with docker-compose

Clavin June
FAUN — Developer Community 🐾
4 min readJul 4, 2019

--

Today I learn how to manage MongoDB on docker with docker-compose, and I want to share with you.

Photo by chuttersnap on Unsplash

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

  1. Pull MongoDB image
  2. Create a file for initiate authenticated database and user
  3. Write docker-compose file
  4. Login to MongoDB with created credentials

Pull MongoDB image

pull latest MongoDB image by executing command below

$ docker pull mongo:latest
result of docker pull

Show pulled images by executing $ docker images make sure, your mongo image is there.

result of docker images

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

Content of init-mongo.js

Write docker-compose file

Create a file named docker-compose.yml for setup your docker-compose stack. Here is our current directory structure

current directory structure
Content of docker-compose.yml

Explanation

  • version is a version of docker-compose file format, you can change to the latest version
  • database on line 3 is just a service name, you can change the name whatever you want
  • image must be mongo, because you want to create a container from mongo image
  • container_name is a name for your container, it’s optional
  • environment is a variables that will be used on the mongo container
  • MONGO_INITDB_DATABASE you fill with a database name that you want to create, make it same like init-mongo.js
  • MONGO_INITDB_ROOT_USERNAME you fill with username of root that you want
  • MONGO_INITDB_ROOT_PASSWORD you fill with password of root that you want
  • volumes 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 named mongo-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 port 27017 until 27019

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 the container

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>
Log into MongoDB user

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!

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

--

--