Skip to content

Docker mongodb image

nSoftteco edited this page Mar 5, 2016 · 1 revision

Docker + MongoDB

First get the latest mongo image

    docker pull mongo:latest

Launch MongoDB container

    docker run -v "$(pwd)":/data --name mongo -d mongo mongod --smallfiles

The running containers can be checked by

    # display only running containers
    docker ps

    # or if you want all containers displayed
    docker ps -a

There are two options for connecting to your Mongo database.

    docker run -it --link mongo:mongo --rm mongo sh -c 'exec mongo "$MONGO_PORT_27017_TCP_ADDR:$MONGO_PORT_27017_TCP_PORT/test"'
    > show dbs
    > exit

Personally, I like just connecting to a running container and executing needed commands from it. You can do what with:

    docker exec -it 442c2541fe1a bash # by ID
    # or
    docker exec -it mongo bash # by Name

To test that your mongodb is working execute the following commands from mongo container:

    db.col.insert({"a": 4})
    db.col.find().pretty()