This tutorial explains how to install MongoDB and Mongo Express with Docker. It also describes how you can connect Mongo Express with MongoDB.
You need a virtual server from netcup with Debian or Ubuntu installed. This tutorial also requires the setup of Docker and Docker Compose. Among the netcup community tutorials, you can find a tutorial that explains how to set up Docker and Docker Compose on Ubuntu.
Check the Docker version:
sudo docker --version
The output should look like the following, the version and build number may differ:
Docker version 20.10.10, build b485636
Check the Docker Compose version:
docker-compose version
The output should look like the following, the version may differ:
Docker Compose version v2.1.0
Use a text editor to create a docker-compose.yaml file:
vi ./docker-compose.yaml
Insert the following:
version: '3'
services:
mongodb:
container_name: mongodb
image: mongo:4.4.6
environment:
- MONGO_INITDB_DATABASE=mongodb
- MONGO_INITDB_ROOT_USERNAME=mongouser
- MONGO_INITDB_ROOT_PASSWORD=mongopasswd
ports:
- '27017:27017'
volumes:
- mongodb_volume:/data/db
networks:
- mongo_backend_network
mongo-express:
container_name: mongo-express
image: mongo-express:1.0.0-alpha.4
depends_on:
- mongodb
environment:
- ME_CONFIG_MONGODB_SERVER=mongodb
- ME_CONFIG_MONGODB_ADMINUSERNAME=mongouser
- ME_CONFIG_MONGODB_ADMINPASSWORD=mongopasswd
- ME_CONFIG_BASICAUTH_USERNAME=adminuser
- ME_CONFIG_BASICAUTH_PASSWORD=adminpasswd
ports:
- '8081:8081'
volumes:
- mongo_express_volume:/data/db
networks:
- mongo_backend_network
volumes:
mongodb_volume:
mongo_express_volume:
networks:
mongo_backend_network:
driver: bridge
Save the docker-compose.yaml file.
Two services are defined in this file: One for MongoDB and one for Mongo Express. Both services have a fixed container name and both have their own volume. In the environments definition you can see the credentials for accessing the services. The Mongodb service will be exposed to the outside on port 27017
. Mongo Express will be exposed to the outside on port 8081
. The two services use the same Docker network "mongo_backend_network", which is also defined in the networks definition. Exposure of port 27017
is not necessary because mongo-express will use mongo_backend_network to connect to the MongoDB. This is only required if you want to connect from an application outside the Docker network, then you can use host <your-server-ip>:27017
for a connection.
To start all Docker services run:
docker-compose up -d mongodb
docker-compose up -d mongo-express
With the parameter "-d" all services started in detach mode, which means that all services will run in the background without blocking the command line.
Check the status of the Docker containers with the following command:
docker ps -a
The output should look like this:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
64eea9ca7a0f mongo:4.4.6 "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:27017->27017/tcp, :::27017->27017/tcp mongodb
8c525825faf6 mongo-express:1.0.0-alpha.4 "tini -- /docker-ent…" 2 minutes ago Up 2 minutes 0.0.0.0:8081->8081/tcp, :::8081->8081/tcp mongo-express
In this case both services are running, both have the status "Up 2 minutes".
First check the logs of the MongoDB container:
docker logs mongodb
The logs should look like this:
{"t":{"$date":"2021-11-10T22:00:31.237+00:00"},"s":"I", "c":"NETWORK", "id":23015, "ctx":"listener","msg":"Listening on","attr":{"address":"/tmp/mongodb-27017.sock"}}
{"t":{"$date":"2021-11-10T22:00:31.237+00:00"},"s":"I", "c":"NETWORK", "id":23015, "ctx":"listener","msg":"Listening on","attr":{"address":"0.0.0.0"}}
{"t":{"$date":"2021-11-10T22:00:31.237+00:00"},"s":"I", "c":"NETWORK", "id":23016, "ctx":"listener","msg":"Waiting for connections","attr":{"port":27017,"ssl":"off"}}
{"t":{"$date":"2021-11-10T22:00:31.297+00:00"},"s":"I", "c":"NETWORK", "id":22943, "ctx":"listener","msg":"Connection accepted","attr":{"remote":"172.18.0.3:48792","uuid":"5eb1e2c7-f54a-4686-a55b-945ce75e7b96","connectionId":1,"connectionCount":1}}
{"t":{"$date":"2021-11-10T22:00:31.309+00:00"},"s":"I", "c":"NETWORK", "id":51800, "ctx":"conn1","msg":"client metadata","attr":{"remote":"172.18.0.3:48792","client":"conn1","doc":{"driver":{"name":"nodejs","version":"3.7.3"},"os":{"type":"Linux","name":"linux","architecture":"x64","version":"5.4.0-89-generic"},"platform":"'Node.js v12.22.7, LE (legacy)"}}}
{"t":{"$date":"2021-11-10T22:00:31.337+00:00"},"s":"I", "c":"ACCESS", "id":20250, "ctx":"conn1","msg":"Authentication succeeded","attr":{"mechanism":"SCRAM-SHA-256","speculative":true,"principalName":"mongouser","authenticationDatabase":"admin","remote":"172.18.0.3:48792","extraInfo":{}}}
{"t":{"$date":"2021-11-10T22:01:31.233+00:00"},"s":"I", "c":"STORAGE", "id":22430, "ctx":"Checkpointer","msg":"WiredTiger message","attr":{"message":"[1636581691:232977][1:0x7fca4e9d9700], WT_SESSION.checkpoint: [WT_VERB_CHECKPOINT_PROGRESS] saving checkpoint snapshot min: 3, snapshot max: 3 snapshot count: 0, oldest timestamp: (0, 0) , meta checkpoint timestamp: (0, 0) base write gen: 21"}}
{"t":{"$date":"2021-11-10T22:02:31.249+00:00"},"s":"I", "c":"STORAGE", "id":22430, "ctx":"Checkpointer","msg":"WiredTiger message","attr":{"message":"[1636581751:249677][1:0x7fca4e9d9700], WT_SESSION.checkpoint: [WT_VERB_CHECKPOINT_PROGRESS] saving checkpoint snapshot min: 6, snapshot max: 6 snapshot count: 0, oldest timestamp: (0, 0) , meta checkpoint timestamp: (0, 0) base write gen: 21"}}
Then check the logs of the Mongo Express container:
docker logs mongo-express
The logs should look like this:
Welcome to mongo-express
------------------------
(node:7) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
Mongo Express server listening at http://0.0.0.0:8081
Server is open to allow connections from anyone (0.0.0.0)
Open URL http://localhost:8081/
in the browser.
Log in with these credentials:
Username: adminuser
Password: adminpasswd
After successful login you can create a new database with the input field "Database name" and a click on the "Create database" button. After that, the new database is displayed in the table above. With the green "View" button you can open the database in the UI. In this UI you can create a new collection, view a collection, change a collection or delete an existing collection.
If you don't need the MongoDB database and Mongo Express anymore, you can stop and remove the two services with one command. Run in the command-line:
docker-compose down
You learned how to install MongoDB and Mongo Express with Docker and connect Mongo Express with MongoDB. Mongo Express facilitates database management with a web-based UI. You only need a browser for administration. This is very helpful for your local development.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
By making a contribution to this project, I certify that:
The contribution was created in whole or in part by me and I have the right to submit it under the license indicated in the file; or
The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same license (unless I am permitted to submit under a different license), as indicated in the file; or
The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it.
I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the license(s) involved.