Using Docker Compose 3.8 with Node.js and MongoDB
( 21 Articles)

This article shows you how to use Docker Compose with Node.js and MongoDB in the simplest way.
Note: I’ll use version 3.8 of Compose file format but any 3.x version is totally fine.
Table Of Contents
The Code
The project structure:
├── docker-compose.yml
└── node-web
├── Dockerfile
├── index.js
├── node_modules
└── package.json
The docker-compose.yml file:
version: '3.8'
services:
database:
image: mongo
restart: always
environment:
- MONGO_INITDB_ROOT_USERNAME=my-username
- MONGO_INITDB_ROOT_PASSWORD=my-root-password
volumes:
- my_data:/data/db
node-web:
build:
context: ./node-web
dockerfile: Dockerfile
depends_on:
- database
volumes:
- /app/node_modules
- ./node-web:/app
ports:
- "5000:5000"
volumes:
my_data:
The Dockerfile in the node-web folder:
FROM node:alpine
WORKDIR "/app"
COPY ./package.json ./
RUN npm install
COPY . .
CMD ["npm", "run", "dev"]
The package.json file:
{
"name": "my-node-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "nodemon index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.1",
"mongodb": "^4.9.1"
},
"devDependencies": {
"nodemon": "^2.0.19"
}
}
The index.js file:
const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const app = express();
// The default url used to connect to MongoDB is mongodb://[service-name-decribed-in-the-compose-file]:/27017
// In this case, the service name is "database"
const url = 'mongodb://database:27017';
// Connect to MongoDB
MongoClient.connect(url, (err, client) => {
if(err){
throw new Error('Could not connect to the database');
}
console.log('Successfully connected to the database');
// Interact with Mongodb
// Insert, update ....
// See the docs at:
// https://www.npmjs.com/package/mongodb
// or:
// https://github.com/mongodb/node-mongodb-native
})
// Some other code
app.get('/', (req, res) => {
res.send('Hi!');
});
app.listen(5000, () => {
console.log('Express is listening on port 5000!')
})
Here is the structure of the default URL for connecting to MongoDB:
mongodb://[service-name]:/27017
Run
docker-compose up --build
Wait a minute or two and you will see something like this in the terminal (versions of packages might be different, don’t worry about this):
node-web_1 | Express is listening on port 5000!
database_1 | {"t":{"$date":"2020-10-03T16:43:59.607+00:00"},"s":"I", "c":"NETWORK", "id":22943, "ctx":"listener","msg":"Connection accepted","attr":{"remote":"172.28.0.3:33296","connectionId":1,"connectionCount":1}}
database_1 | {"t":{"$date":"2020-10-03T16:43:59.610+00:00"},"s":"I", "c":"NETWORK", "id":51800, "ctx":"conn1","msg":"client metadata","attr":{"remote":"172.28.0.3:33296","client":"conn1","doc":{"driver":{"name":"nodejs","version":"3.6.2"},"os":{"type":"Linux","name":"linux","architecture":"x64","version":"4.19.76-linuxkit"},"platform":"'Node.js v14.12.0, LE (legacy)"}}}
node-web_1 | Successfully connected to the database
Note:
- You should only use Nodemon in the development environment.
- You may see some warnings when running docker-compose up for the first time but there is no need to worry about them.
Further reading:
- Using Docker Compose to speed up WordPress development
- How to Install Docker Compose on Ubuntu
- Deleting unnecessary Images and Containers in Docker
- Start, Pause, Restart, Stop, and Delete a Docker Container
- Docker: How to Name or Rename a Container
You can also check out our Docker topic page for the latest tutorials, examples, tips, and tricks.
Super helpful.