F

back

How to setup docker compose with node and mongodb application

To set up Docker with a Node.js and MongoDB application, you first need to install Docker on your machine. Then, you'll need to create a Dockerfile in your project directory that defines your application's environment. Finally, run your application using Docker commands.

Here's the application structure:

project/
	│
	├── api/
	│   ├── Dockerfile
	│   ├── package.json
	│   ├── package-lock.json
	│   ├── index.js (or your main Node.js application file)
	│   └── other source files...
	│
	├── docker-compose.yml
	└── README.md
	

In the api/ directory, create an index.js file (or rename your main Node.js application file) and add the following code:

					const express = require('express');
	const app = express();
	const port = 3000;
	
	app.get('/', (req, res) => {
	  res.json({ message: 'Hello from api server' });
	});
	app.listen(port, () => {
	  console.log(`App listening at <http://localhost>:${port}`);
	});
	
	module.exports = app;
	
	

This simple application will create a server that listens on port 3000 and responds with a JSON message to GET requests on the root path.

Next, create a package.json file in the same directory and populate it with the following content. This file outlines the dependencies of your Node.js application and the scripts to initiate it. I aim to use nodemon in my docker, which allows for detecting changes and rerunning the container without the need to rebuild the API docker.

To do so, install nodemon as a development dependency by running npm install --save-dev nodemon. Then, update the scripts section of your package.json file to include a dev script that runs your application with nodemon.

{
					{
	"name": "api",
	"version": "1.0.0",
	"description": "",
	"main": "index.js",
	"scripts": {
	"start": "node index.js",
	dev"": "nodemon index.js"
	},
	"author": "Farnaz Daneshvar",
	"license": "ISC",
	"dependencies": {
	"express": "^4.19.2",
	"nodemon": "^3.1.0"
	}
	}

Step 1: Install Docker

First, you need to install Docker on your machine. Visit the Docker website and download the suitable version for your operating system. Following the instructions provided on the site, install Docker.

Step 2: Create a Dockerfile

Next, navigate to your project directory. Create a new file and name it 'Dockerfile'. This file will define your application's environment. Here is a basic example of how your Dockerfile might look:

FROM node:18-alpine
	
	# Set the working directory within the container
	WORKDIR /api
	
	# Copy package.json and package-lock.json to the working directory
	COPY package*.json ./
	
	# Install dependencies
	RUN npm install
	
	# Copy the rest of the application code
	COPY . .
	
	# Expose the port your app runs on
	EXPOSE 3000
	
	# Command to run the application
	CMD ["npm", "run", "start"]
	

Step 3: Create a Docker Compose File

Now, create a docker-compose.yml file in your project directory. This will define the services that make up your app. It should include services for your Node.js application and MongoDB. Here's a basic example:

version: '1.0'
	services:
	  api:
		container_name: api
		build: ./api
		command: npm run dev
		volumes:
		  - ./api:/api
		ports:
		  - '3000:3000'
	  mongo_db:
		container_name: MongoDB
		image: mongo
		volumes:
		  - mongo_data:/data/db
		ports:
		  - '27017:27017'
	
	volumes:
	  mongo_data:

Step 4: Run Docker Compose

After creating the Docker Compose file, you can now run the application. Navigate to the directory that contains your docker-compose.yml file. Use the following command to start the services:

docker-compose build
	docker-compose up

This command will create and start all the services defined in your Docker Compose file. Your Node.js application is now running inside a Docker container and is linked to a MongoDB service.

Now if you navigate to http://localhost:3000/ in your browser you should see "Hello from api server”.