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:
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:
# 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
# Set /usr/bin/mongod as the dockerized entry-point application
docker build --tag my_mongodb_1 .
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.