Docker/MongoDB
< Docker
Try It
editOnline Free
edit- Use Play with Docker. Create an account and/or log in.
- Start an interactive session and add a new instance.
- In the terminal window, enter the following commands:
docker run -d -p 27017:27017 --name=mongo-server mongo
docker exec -it mongo-server hostname -i
Note the IP address displayed.
- To connect to the mongo-server from a second instance of the mongo image:
docker run -it --rm mongo bash
mongo 172.17.0.2
Replace172.17.0.2
with your mongo-server's IP address.
- To connect to the mongo-server from a programming language in a separate container:
- Use
mongodb://172.17.0.2
for the host name.
Replace172.17.0.2
with your mongo-server's IP address. - Modify the
Dockerfile
to install any required libraries, such as mongodb, pymongo, or MongoDB\Driver.
- Use
- To connect to the mongo-server from a programming language not running in a container:
- Use
mongodb://127.0.0.1
for the host name. - Modify the
Dockerfile
to install any required libraries, such as mongodb, pymongo, or MongoDB\Driver.
- Use
On Your Own System
edit- Install Docker Desktop or the Docker Engine.
- In a terminal window, enter the following commands:
docker run -d -p 27017:27017 --name=mongo-server mongo
docker exec -it mongo-server hostname -i
Note the IP address displayed.
- To connect to the mongo-server from a second instance of the mongo image:
docker run -it --rm mongo bash
mongo 172.17.0.2
Replace172.17.0.2
with your mongo-server's IP address.
- To connect to the mongo-server from a programming language in a separate container:
- Use
mongodb://172.17.0.2
for the host name.
Replace172.17.0.2
with your mongo-server's IP address. - Modify the
Dockerfile
to install any required libraries, such as mongodb, pymongo, or MongoDB\Driver.
- Use
- To connect to the mongo-server from a programming language not running in a container:
- Use
mongodb://127.0.0.1
for the host name. - Modify the
Dockerfile
to install any required libraries, such as mongodb, pymongo, or MongoDB\Driver.
- Use