This post will go through the steps needed to setup a working MongoDB environment inside a Docker container on your local machine. The benefits of having MongoDB inside a container on your local machine is that you can easily change versions and use different MongoDB instances without having to have multiple different versions installed. It also allows you to share your MongoDB setup with your team so that everyone can connect to a similar local database that can be used for testing and development.
You need to have Docker installed to be able to follow this guide, instructions for this can be found here. I am using version
22.214.171.124 (48506) at the time of writing.
Disclaimer: The following approach stores data inside the container itself, meaning that you will lose all your data if you remove the container! This is intended to be used for development and testing purposes.
Running the MongoDB instance
It's surprisingly easy to get this up and running.
Run the following command from a terminal window:
docker run -p27017:27017 --name my-mongodb-container -d mongo:latest
- Start the
mongo:latestimage in a container
- Detach the container
- Name it
- Map the container port
27017to our own machine's
You can find more tags here if you don't want to use the
Connecting to the MongoDB instance
Now that we have a MongoDB container up and running, we can try to connect to it using the following command:
docker run -it --rm mongo mongo --host host.docker.internal
- Start the
mongoimage in a container
- Using interactive mode
- Removing the container once we exit
- Run the
mongocommand inside the container, with the argument
host.docker.internal address is special and can be used as a workaround to connect to the host machine from a container.
That's all there is to it. You should be able to go ahead and use this container instance of MongoDB now for local development and testing purposes.