I've been told that Docker is the way to go to create user separation from actual production. Is it somehow possible to make further separation and install separate MongoDB databases without affecting the original?

I'd be happy to learn. Thanks a lot in advance!

  • 3
    The question is rather vague here, and the answer might be affected by the exact role played by the MongoDB dataset in the whole project. Can you provide a more precise use case, so that you can get a more precise answer ? – jmary Jun 23 at 10:28

You can and should. Just create several environments, for example for development, testing, and for production. Just create a Dockerfile for one of each.

One way could be:

  1. Create a Dockerfile (the official is broken, so I created this one and a PR):

    # Dockerizing MongoDB: Dockerfile for building MongoDB images
    # Based on ubuntu:18.04, installs MongoDB following the instructions from:
    # http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
    FROM       ubuntu:18.04
    # Installation:
    # Import MongoDB public GPG key AND create a MongoDB list file
    RUN apt-get update && apt-get install -y gnupg2 ca-certificates
    RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
    RUN echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" > /etc/apt/sources.list.d/mongodb-org-4.0.list
    # Update apt-get sources AND install MongoDB (latest stable version)
    RUN apt-get update && apt-get install -y mongodb-org
    # Create the MongoDB data directory
    RUN mkdir -p /data/db
    # Expose port #27017 from the container to the host
    EXPOSE 27017
    # Set /usr/bin/mongod as the dockerized entry-point application
    ENTRYPOINT ["/usr/bin/mongod"]
  2. Build

    docker build --tag my_mongodb_1 .
  3. Run

    docker run -p 27017:27017 --interactive --tty my_mongodb_1

Pay special attention to which user have access to which environment, since is very common and dangerous to forget to change something on the developer setup and end working with production database thinking you are working with development one.

Here is a nice post about how to do all this.

  • Thx for your answer! I wrote this, pastebin.com/6j9GHz6x. I am trying to install Python as well. Also, is it good practice to allow each user to use their account to create a container connected with their account? Including a /home/ folder of theirs. – John Smith Jun 25 at 18:15
  • If you cannot connect to mongodb from Robo 3T on macos, then add --bind_ip_all: ENTRYPOINT ["/usr/bin/mongod", "--bind_ip_all"] – marcinj Aug 6 at 22:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.