Skip to content

Instantly share code, notes, and snippets.

@alaztetik
Last active November 20, 2022 12:18
Show Gist options
  • Save alaztetik/8c44177dd6ab50c8440a3cc9be830fb4 to your computer and use it in GitHub Desktop.
Save alaztetik/8c44177dd6ab50c8440a3cc9be830fb4 to your computer and use it in GitHub Desktop.
Run and connect to MongoDB on Docker locally
# commands may need sudo
# pull MongoDB image:
docker pull mongo
# start container:
docker run -d \
--name CONTAINER_NAME \
-p LOCALHOST_PORT_NUMBER \
-e MONGO_INITDB_ROOT_USERNAME=username \
-e MONGO_INITDB_ROOT_PASSWORD=password \
mongo
# check if the contaier is running:
docker container ls
# start ad connect to a bash shell within the container:
docker exec -it CONTAINER_NAME bash
# access MongoDB instance via CLI:
mongosh --username username --password password
# use URI to connect to db from outside the container:
mongodb://username:password@0.0.0.0:PORT/
@alaztetik
Copy link
Author

alaztetik commented Nov 20, 2022

Reference: Tom Szpytman @szpytfire on Medium

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment