Last active
November 20, 2022 12:18
-
-
Save alaztetik/8c44177dd6ab50c8440a3cc9be830fb4 to your computer and use it in GitHub Desktop.
Run and connect to MongoDB on Docker locally
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Reference: Tom Szpytman @szpytfire on Medium