Jump to content

Docker/MongoDB

From Wikiversity

Try It

[edit | edit source]

Online Free

[edit | edit source]
  1. Use Play with Docker. Create an account and/or log in.
  2. Start an interactive session and add a new instance.
  3. 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.
  4. 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
      Replace 172.17.0.2 with your mongo-server's IP address.
  5. To connect to the mongo-server from a programming language in a separate container:
    • Use mongodb://172.17.0.2 for the host name.
      Replace 172.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.
  6. 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.

On Your Own System

[edit | edit source]
  1. Install Docker Desktop or the Docker Engine.
  2. 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.
  3. 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
      Replace 172.17.0.2 with your mongo-server's IP address.
  4. To connect to the mongo-server from a programming language in a separate container:
    • Use mongodb://172.17.0.2 for the host name.
      Replace 172.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.
  5. 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.

See Also

[edit | edit source]