Public Repository

Last pushed: 2 years ago
Short Description
Using MongoDB as a Docker Container
Full Description

Run a MongoDB instance in a container and see it your host's browser.

1) Build the Docker Image

$ docker build -t <container_name> .

2) Run the instance

$ docker run --name <instance_name> -i -v <absolute_path_data_host>:<container_data_absolute_path> -p <host_port>:<containter_listen_port> <container_name>

Where, for instance:

  • <host_data_absolute_path> is the absolute path for a data subfolder inside the folder you have your Dockerfile
  • <container_data_absolute_path> is the container folder (e.g. /data/)
  • <containter_listen_port> is the port MongoDB will be listening to. We use 27779 but this can be customized in /data/mongodb.conf and Dockerfile EXPOSE command.
  • <host_port> can be set to any port you want to open in your browser

Notes:

1) Instead of running iteratively (tag -i) you can generate a daemon (fork, tag -d). Both will work.

2) Another way to run the container in a more malleable way is to assign a container ID to its daemon:

CID=$(docker run -d -v <absolute_path_data_host>:<container_data_absolute_path> -p <host_port>:<container_listen_port> <container_name>)

This way, you can easily extract information from the instance, such as:

$ docker logs $CID

or

$ docker inspect $CID

3) A good way to test whether the data volume is correctly linking data from the host to the container is adding a test.txt file in the data folder and then add the following line to your Dockerfile:

CMD ['cat', '/data/test.txt']

3) Check it!

In another terminal, check what port Docker is mapping to (the host port):

$ docker ps | grep mongo

You can check if this port is indeed listening:

$ netstat -antp | grep <host_port>

You are ready to see it in your browser!

Shell Access

For debugging and maintenance purposes you may want access the containers shell:

$ docker exec -it <instance-name> bash
Docker Pull Command
Owner
bt337

Comments (0)