🕵️ SicherheitslückenWhat continuous operational resilience looks like under DORA(09.09.2026 um 17:53 Uhr)
🔧 AI Nachrichten OpenAI seeks tougher AI rules. CIOs may feel the ripple effects(10.09.2026 um 12:11 Uhr)
🔧 AI Nachrichten Mistral valued at €21bn after €3bn Series D funding round(08.09.2026 um 10:19 Uhr)
🪟 Windows TippsWindows XP's Cursor Indicator Is Getting a Windows 11 Refresh(25.08.2026 um 13:00 Uhr)
🕵️ SicherheitslückenWhat continuous operational resilience looks like under DORA(09.09.2026 um 17:53 Uhr)
🔧 AI Nachrichten OpenAI seeks tougher AI rules. CIOs may feel the ripple effects(10.09.2026 um 12:11 Uhr)
🔧 AI Nachrichten Mistral valued at €21bn after €3bn Series D funding round(08.09.2026 um 10:19 Uhr)
🪟 Windows TippsWindows XP's Cursor Indicator Is Getting a Windows 11 Refresh(25.08.2026 um 13:00 Uhr)

🔧 Programmierung 🕛 vor 1 Jahr 4 Min Lesezeit
0

Dockerizing an Express App with MongoDB Database

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

In this blog post, we'll walk through the process of dockerizing an Express.js application with a MongoDB database. We'll create Docker images, set up a Docker Compose file, and run the application using Docker Compose.






Step 1: Set Up the Project Files





  1. Create Project Structure



    In the root of your project directory, express-mongo-docker, create the following files:






CODE
express-mongo-docker/
├── Dockerfile
├── .dockerignore
├── package.json
├── server.js
├── docker-compose.yml
└── config/
├── mongodb.js
└── config.env








  1. Express App Code



Add the Express server code in file index.js




CODE
const express = require('express');
const app = express();
const dotenv = require('dotenv');
const cors = require('cors');
const bodyParser = require('body-parser');
const connectDatabase = require('./config/mongoDb');
const port = process.env.PORT || 5000;

//setting up config file
dotenv.config({ path: 'config/config.env' });

// middleware
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(bodyParser.urlencoded({ extended: true }));

// handle uncaught excepion

process.on('uncaughtException', (err) => {
console.log(`ERROR: ${err.message} \n ${err.stack}`);
console.log('Shutting down the server due to uncaught exception');
process.exit(1);
});

// console.log(a); // reference error: uncaught error

// connecting to Database
connectDatabase();


// Api routes
app.get('/', (req, res) => {
res.json({
message: 'API is running',
docker: 'Docker is running'
});
});



const server = app.listen(port, () => {
console.log(
`Server is running at port ${port} in ${process.env.NODE_ENV} mode`
);
});

// Handle unhandle Promise rejection

process.on('unhandledRejection', (err) => {
console.log(`ERROR : ${err.message}`);
console.log('Shutting down the server due to unhandle promise rejection');
server.close(() => {
process.exit(1);
});
});









  1. Connection To Mongodb
    Connecting to Mongodb database from file ./config/mongoDb




CODE
const mongoose = require('mongoose');

const connectDatabase = async () => {
console.log('db uri', process.env.DB_LOCAL_URI);
try {
await mongoose.connect(process.env.DB_LOCAL_URI).then((con) => {
console.log(
`MongoDb database connected with host: ${con.connection.host}`
);
});
console.log('MongoDB is connected');
} catch (error) {
console.log('MongoDB connection failed', error);
}
};

module.exports = connectDatabase;








  1. Add env file
    Create .env file from file: ./config/config.env




CODE
DB_LOCAL_URI=mongodb://<your-username>:<your-password>@mongo:27017/test-app?authSource=admin
MONGO_INITDB_ROOT_USERNAME=<your-username>
MONGO_INITDB_ROOT_PASSWORD=<your-password>









Step 2. Creating Docker Images



First, let's create a Dockerfilefor our Express application:




CODE
# Use the official Node.js image as the base image
FROM node:18-alpine

# Set the working directory
WORKDIR /app

COPY package*.json ./

RUN npm install

# Copy the rest of the application code
COPY . .

# Expose the port the app runs on
EXPOSE 5000

# Command to run the application
CMD ["npm", "run", "dev"]






Next, create a .dockerignore file to exclude unnecessary files and directories:




CODE
node_modules
npm-debug.log
.git
.gitignore
.dockerignore
Dockerfile
docker-compose.yml






Modify package.json scripts with Express and Mongoose:




CODE
"scripts": {
"start": "node index.js",
"dev": "SET NODE_ENV=DEVELOPMENT& nodemon -L index.js --host",
"prod": "SET NODE_ENV=PRODUCTION& nodemon index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},









Step 3. Creating a Docker Compose File



Now, let's create a docker-compose.yml file to define and run our multi-container Docker application:




CODE
version: '3.8'

services:
# MongoDB service
mongo:
image: mongo:latest
container_name: mongo-container
ports:
- '27017:27017'
volumes:
- mongotestData:/data/db
networks:
- app-network
# For environment variable it'll be beter if you use env file path here
environment:
- MONGO_INITDB_ROOT_USERNAME=<your-username>
- MONGO_INITDB_ROOT_PASSWORD=<your-password>

# Backend service
backend:
build:
context: .
dockerfile: Dockerfile
image: mern-backend-image
container_name: mern-backend-container
ports:
- '5000:5000'
depends_on:
- mongo
env_file:
- ./config/config.env
stdin_open: true
volumes:
- .:/app
- /app/node_modules
networks:
- app-network

# Define volumes
volumes:
mongotestData:

# Define networks
networks:
app-network:







This Docker Compose file defines two services:




  • backend: Our Express application

  • mongo: MongoDB database



The backend service builds from our Dockerfile, maps port 5000, and depends on the mongo service. We also set an environment variable for the MongoDB connection string.



The mongo service uses the official MongoDB image, maps port 27017, and uses a named volume to persist data.( you can also bind local file path to store mongo data)






Step 4. Running the Application



To run the application using Docker Compose, follow these steps:




  1. Open a terminal and navigate to your project directory.

  2. Build and start the containers:




CODE
docker-compose up --build







  1. Your application should now be running. Access it at http://localhost:5000.

  2. To stop the application, use:




CODE
docker-compose down









Congratulations !!!



We've successfully dockerized an Express application with a MongoDB database. This setup allows for easy deployment and scaling of your application. Remember to adjust the configuration as needed for your specific project requirements.



Happy coding!

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
Sam Altman calls GPT-6 Astra rollout ‘messy’ as enterprise users wait for access
1 Quelle
Swiss government explores replacing Microsoft 365 with open-source software
1 Quelle
What continuous operational resilience looks like under DORA
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Dockerizing an Express App with MongoDB Database

Thematisch verwandte Begriffe: Dockerizing, Express, with, MongoDB · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...