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 LinuxThe first step is to have Docker installed in the system, obviously.
- If you are using Ubuntu: How to install Docker on Ubuntu 18.04?
- For CentOS 8: How to install Docker on CentOS 8?
- For CentOS 7: How to install Docker CE on CentOS 7?
- If you use Windows: How to install Docker in Windows 10?
- Or, if you are using Debian 10: How to install Docker on Debian 10?
:~$ sudo apt docker-composeFor Debian, Ubuntu and derivatives.
:~$ sudo yum install docker-composeOr, for RHEL, CentOS and derivatives.
Deploy MongoDB using Docker ComposeTo display the Docker image of MongoDB, first I’m going to create a folder. So open a terminal session and run:
:~$ mkdir mongodb :~$ cd mongodbThen, in the same folder create the docker-compose.yml file. And add the following content:
:~$ nano docker-compose.yml
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: exampleIn 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/mongodataas 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-expresswhich 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 -dAfter that, open your web browser and go to
http://your-server:8081Next, you can for example, create a new database. And create a new collection. So, everything is perfect.