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

1. Create the folder

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

2. Make the docker-compose.yml file

# 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
touch docker-compose.yml
nano docker-compose.yml

3. Run the Docker compose

docker-compose build
docker-compose up

4. Connect and manage using Mongo Compass

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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store