Skip to content

Instantly share code, notes, and snippets.

@dittmaraz
Last active March 5, 2024 19:21
Show Gist options
  • Save dittmaraz/45542157a714f38c2b9aa3976f36198d to your computer and use it in GitHub Desktop.
Save dittmaraz/45542157a714f38c2b9aa3976f36198d to your computer and use it in GitHub Desktop.
MongoDB/Docker Cheatsheet

Mongo/Docker Cheatsheet

To download the latest image:

$ docker pull mongo:latest

To run a mongo container with detached mode(-p) and with ports mapped (-p 27017-27017:27017-27019) and named (--name mongodb):

$ docker run -d -p 27017-27017:27017-27019 --name mongodb mongo:latest

To list the docker images:

$ docker images

To stop a container:

$ docker stop mongodb

To remove a container:

$ docker rm mongodb

To list running containers:

$ docker ps -a

To enter an interactive terminal(-it mongodb bash):

$ docker exec -it mongodb bash

To login as a mongo client, enter a interactive terminal, then type:

$ mongo

To show the databases:

> show dbs

To switch to a database:

> use <database>

To save a record to the database:

> db.people.save({firstname: "sean", lastname: "dittmar" })

To show all records:

> db.people.find({})

To find a record using criteria:

> db.people.find({firstname: "Sean"})

To exit mongo:

> quit

To exit docker container:

$ exit

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