Today I will prepare and create a MongoDB server for testing purpose by using Docker and Docker Compose. It's simple.
Here is my scenario. I have Linux Ubuntu 19.10 (eoan) and I want to have MongoDB server instance. I don't want to install MongoDB in traditional way but instead just use Docker.
I think everyone already knows MongoDB right? It's one of the best NoSQL database in the market.
According to Wikipedia.
MongoDB is a cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with schema. MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License.
You can get more information from https://www.mongodb.com.
I assume that you already have Docker and Docker Compose installed on your machine. If not, please refer to https://docs.docker.com/install/ for Docker installation and refer to https://docs.docker.com/compose/install/ for Docker Compose installation.
In this case I am on Linux Ubuntu, but the result I think should be similar for other OS.
version: '3.7' services: mongodb_container: image: mongo:latest environment: MONGO_INITDB_ROOT_USERNAME: root MONGO_INITDB_ROOT_PASSWORD: rootpassword ports: - 27017:27017 volumes: - mongodb_data_container:/data/db volumes: mongodb_data_container:
What is the Docker Compose file above means?
It will have MongoDB image and it will take the latest version. As of this writing, the latest version of MongoDB is 4.2.2. MongoDB will have user
root and password
rootpassword. It will expose port 27017 to the host machine.
It will also using data container to save the data, called
mongodb_data_container. It's useful for data persistent, so the data will not be deleted even later you call command
docker-compose up -d
Above command will start the services on detach mode (similar like running in the background).
If everything OK then our MongoDB server will start.
Type this command.
The result it will look like this.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4056a520c8ea mongo:latest "docker-entrypoint.s…" 19 hours ago Up 19 hours 0.0.0.0:27017->27017/tcp mongodb_mongodb_container_1
Looks good and our MongoDB container is running well.
To check our data container, check with command below.
docker volume ls
It will show like below.
DRIVER VOLUME NAME local mongodb_mongodb_data_container
That's looks good.
mongo admin -u root -p rootpassword
It will connect to localhost port 27017.
mongo command should be installed on the computer. On Linux this should be install
mongodb-org-shell only. Refer to this for more detail https://docs.mongodb.com/manual/installation/
Some useful commands.
Create new non-existant database:
Show contents/documents of a collection:
Save a data to a collection:
Show database version:
Now we can enjoy our local MongoDB database server for any purpose we want. For me this setup is fine for testing purpose.
To shutdown database without delete all containers.
To shutdown database and delete all containers.
That's it and we are done. Relax, if you already create some data it will not gone.
The code above also available on my GitHub repository at https://github.com/sonyarianto/docker-compose-mongodb
Thank you and I hope you enjoy it.
- Cover image photo by Kevin Ku from Pexels at https://www.pexels.com/photo/coding-computer-data-depth-of-field-577585/