Running Node.js Applications with Docker and MongoDB
Combine the Power of Containers and NoSQL
The ‘Ubuntu System Administration Guide’ offers an excellent example of modern application deployment by showing how to run Rocket.Chat, a Node.js-based chat application, inside a Docker container while connecting it to a MongoDB database running on the host server.
The Hybrid Approach
This setup demonstrates a powerful hybrid model:
- Database on Host: The MongoDB database is installed and runs directly on the Ubuntu server, giving you full control over its data and configuration.
- Application in Container: The Rocket.Chat application itself is run as a container using a `docker-compose.yml` file. This isolates the application and its dependencies, making it easy to deploy and update.
Connecting the Two
The key is the `MONGO_URL` environment variable in the `docker-compose.yml` file. You configure this URL to point to the IP address of your host server, along with the username and password for the dedicated Rocket.Chat database user you created. This allows the containerized application to communicate securely with the database running outside the container.
This post is based from content of the book Ubuntu System adminstration guide. And the book can be found here https://www.amazon.com/stores/Mattias-Hemmingsson/author/B0FF5CQX13