Running MongoDB inside a Docker container with macOS and access it using Mongo Compass, for newbies

Mangle Kuo
3 min readJan 20, 2021

--

Hi hi hi, welcome.

This article tells you how to create a MongoDB docker container with authorisations where you can connect to and manage using Mongo Compass.

I’m assuming you have docker and docker-compose running on your Mac, if not, please follow the instruction to install Docker Desktop here.

1. Create the folder

Open Terminal and run:

cd ~
mkdir Dev && cd Dev
mkdir docker && cd docker
mkdir mongodb && cd mongodb

This translate to:

Go to “~” aka your home folder aka the “your-username” directory. Create a directory called “Dev”, I recommend every time some online “Getting Started” ask you to install something, you install them here, so things don’t overcrowd your home folder. Now since we are developing using docker, create a directory called “docker”, then create a “mongodb” directory. Now enter the directory.

2. Make the docker-compose.yml file

Create a docker-compose.yml file with the following content, in the “mongodb” directory you just created:

# docker-compose.yml
version: '3.1' #format version for this docker compose file
services:
mongo-container:
image: mongo:latest
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=MyPa$$w0rd
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# Here you are setting the username and password
# so change it to whatever you want
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ports:
- "27017:27017"
command: mongod

To do this in Terminal following the command in step 1, you can run

touch docker-compose.yml
nano docker-compose.yml

“touch” is the linux command to create new file, and “nano” is a text editor that runs in the command line

Now copy and paste the content into your terminal, then press ctrl+X to quit nano, y to confirm you want to save and enter to confirm you want to save to the same file.

Translation for non-programmer humans:

  • Inside the folder, make a docker-compose.yml file (basically a text file with different file extensions). Open the file with the build-in terminal text editor called nano. Copy and paste into the file, save and quit the text editor.
  • Inside the docker-compose.yml: firstly, this file is written with version 3.1 format. Now tell docker when composing, create a service image called mongo-container from the official image for MongoDB (mongo). For the service image, I want to have root as my root username and MyPa$$w0rd as my root password. Externally, use port 27017 to correspond to the internal port 27017 of the virtually running mini Linux inside. Now in my mini Linux. run “mongod” to start my MongoDB.

If you want to understand better where the database data is stored, and want to keep your database when the image is rebuilt, check out this article.

3. Run the Docker compose

Open Terminal, make sure you are still inside the “mongodb” folder and run:

docker-compose build

then

docker-compose up

4. Connect and manage using Mongo Compass

Congrats, you now have a running MongoDB on localhost:27017, now open your Mongo Compass app and use the following URL to connect:

mongodb://root:MyPa$$w0rd@localhost:27017/?authSource=admin&readPreference=primary&appname=MongoDB%20Compass&ssl=false

🎉 🎉 🎉 🎉

Congratulations, you did it!

Please comment below for suggestions on improving this article.

--

--