Docker (4) Docker and Mongo

1. Start a Mongo server on Docker:

Docker is a good tool for practicing Mongo. You can start a Mongo server on Docker. As we mentioned before, the data would be removed after you remove the container from the system (the data can be kept in the volume and we will demo it later).

$ sudo docker run -it -p 27017:27017 --name mongoContainer mongo:latest mongo

We start a Mongo server on port 27017 and the container is named as "mongoContainer".

The option -p is used to connect the host port with a container port.

If you have installed Mongo on your host, you can try to connect the Mongo server with IP 127.0.0.1:27017.

# open a new terminal on the host and try
# to connect the server with default IP
$ mongo 

Or you can use the terminal provided by the container.

$ sudo docker exec -it mongoContainer mongo

2. Use Volume to keep the data:

$ sudo docker run -it -v /tmp/mongo/data/db:/data/db -p 27017:27017 --name mongoContainer mongo:latest mongo

The option -v would mount the container path to the host path. In our example, we mount the path "/data/db" in the container to the path "/tmp/mongo/data/db" in the host.


留言

熱門文章