Quickly Add MongoDB to Your App With Docker
MongoDB is an open-source NoSQL database that stores data in JSON-like documents.
NoSQL databases are a natural choice if you don't know your data requirements upfront.
They are also a good fit for applications like product catalogs or blog content.
That's where an object-oriented approach shines.
Let's see how we can easily add a Mongo database with Docker and Docker Compose.
Prerequisites:
Run MongoDB with Docker
In your project folder, open the terminal and run:
docker run --name mongo -p 27017:27017 -d mongo
The command will pull the default image from Dockerhub and start the database on port 27017 on localhost.
Connect Your App to MongoDB
That's fine and dandy, but how can I use the database from my app?
The easiest way is to use the URL connection string.
For the default configuration, you have to use the following format with the library of your choice (TypeORM or similar):
mongodb://localhost/<db-name>
Replace <db-name>
with the name of your database.
Please note that you don't have to supply a username or password. The default setup has no user and no password.
Docker Compose
You can add a docker-compose.yml
file to your project. With Docker Compose, you don't have to remember the exact commands for docker run
.
In your project folder, add the docker-compose.yml
file with the following content:
version: '2.4'
services:
db:
image: mongo:4.0.17-xenial
ports:
- 27017:27017
environment:
- MONGO_INITDB_DATABASE=${DB_NAME}
restart: always
volumes:
- db-data:/data/db:delegated
volumes:
db-data:
(For alternative MongoDB versions, check out Dockerhub.)
We create a service with the name db
(name it however you want). The container runs on port 27017 and binds that port to localhost. That way, you can connect to the database from outside of the container.
We also create a volume. Now the data persists between subsequent Docker runs. Volumes can be tricky, but for local development, that setup should suffice.
You can read more about volumes on the Docker documentation site.
Now we need an environment file to create the database. Create a new file called .env
in your project:
DB_NAME=school
You can reuse that file for your app, too. For example, with JavaScript, you can use dotenv to read the content of your file.
Here's an example of a TypeORM configuration:
import { TypeOrmModuleOptions } from '@nestjs/typeorm'
import 'dotenv/config'
export const typeOrmConfig: TypeOrmModuleOptions = {
type: 'mongodb',
url: `mongodb://localhost/${process.env.DB_NAME}`,
useUnifiedTopology: true,
}
Docker Compose Commands
Inside the root directory, open the terminal and type:
docker-compose up -d
The command will start the container in detached mode (in the background).
If you want to stop the container:
docker-compose down -v
(-v
for bringing down the volume as well.)
If you want to use the command line to connect to the database:
docker-compose exec db mongo
-
docker-compose exec
: execute a command inside a running container -
db
: name of the service (see configuration indocker-compose.yml
) -
mongo
: terminal command to run, seemongo
Now you are in the Mongo shell. Let's say you want to see all entries in the database school
.
Select database school
:
use school
Show all data from the collection lessons:
db.lessons.find().pretty()
Recap
You've seen how to set up a MongoDB instance on your computer with only a few lines of code.
Now you can develop your application with a NoSQL database!
If you'd like to use a relational database, you can read my guide on using Postgres with Docker.
Top comments (0)