22.1 C
Texas
angeloma
Senior Writer and partner

Deploy MongoDB using Docker Compose

Hey, guys. This time, we will show you how to install MongoDB using Docker Compose. I have to admit that these last days I have liked much more the technology of Docker compose. It seems a very simple and fast way to install certain applications. However, from my point of view, a bit of flexibility is lost in the configurations since they are images already created from a base. Of course, we will be able to configure some things but not everything. In Osradar we have written several times about MongoDB. Not in vain is one of the NoSQL database management systems based on the most popular documents out there. Widely used in mobile applications as well as in transactional systems, MongoDB stands out for its flexibility and query speed. Fast, robust and high performance are some of the main features of MongoDB. So, let us start.

Install Docker and Docker Compose on Linux

The first step is to have Docker installed in the system, obviously. And using your distribution’s package manager, you can install Docker Compose. For example:
:~$ sudo apt docker-compose
For Debian, Ubuntu and derivatives.
:~$ sudo yum install docker-compose
Or, for RHEL, CentOS and derivatives.

Deploy MongoDB using Docker Compose

To display the Docker image of MongoDB, first I’m going to create a folder. So open a terminal session and run:
:~$ mkdir mongodb
:~$ cd mongodb
1.- Creating the folder for MongoDB
1.- Creating the folder for MongoDB
Then, in the same folder create the docker-compose.yml file. And add the following content:
:~$ nano docker-compose.yml
- Advertisement -
version: '3.1'

services:

  mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_DATABASE=example
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
    volumes:
      - /home/angelo/mongodata:/data/db

  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example
2.- Deploy MongoDB using docker compose
2.- Deploy MongoDB using docker compose
In this file, two services are specified. Firstly, the one we are interested in is MongoDB. The definition is quite simple because you only have to configure as environment variables, the initial database, as well as the password and the root user. The interesting thing is that I add a local folder /home/angelo/mongodata as volume so that it communicates with the container and thus to store the data. In other words, the database and collections will be stored in that folder. Obviously, specify the one you want but you must create it before making this file. The second service is mongo-express which is a graphical interface built with NodeJS and express to manage MongoDB. There the important thing is that the execution port is 8081. You can change it, obviously. Then, we can “raise” the images. To do this, just use this command:
:~$ sudo docker-compose up -d
After that, open your web browser and go to http://your-server:8081
3.- Mongo express and mongodb running
3.- Mongo express and mongodb running
Next, you can for example, create a new database.
4.- Creating a new database
4.- Creating a new database
And create a new collection.
5.- MongoDB running using Docker compose
5.- MongoDB running using Docker compose
So, everything is perfect.

Conclusion

MongoDB is a powerful database manager. Today we have learned to install it using Docker Compose. So how to use it a lot. Remember that installing it this way is recommended for servers and production systems and not so much for development.
- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article