Tutorials  /  Container & Cloud

Docker and Node.js: Simplifying Development with MongoDB

Ccentron Redaktion · October 2024 ·4 min read ·Container & Cloud, Tutorial

The development and deployment of applications can be greatly simplified with Docker. Docker containers offer several advantages for developers:

  • Consistent environments: You can select the languages and dependencies you want without worrying about system conflicts.
  • Isolated environments: This makes debugging and onboarding new team members easier.
  • Portability: The code can be packaged and easily shared.

In this article, I will show you how to set up a development environment for a Node.js application using Docker. We will create two containers: one for the Node.js application and another for the MongoDB database. Among other things, you will:

  • Synchronize the application code with the code in the container to facilitate changes during development.
  • Ensure that changes are applied without restarting the application.
  • Create a password-protected database that is persistent.

By the end of this tutorial, a shark information application will be running in Docker containers.

Prerequisites

To follow this tutorial, you will need:

  • A development server running Ubuntu 18.04 and a non-root user with sudo privileges.
  • Docker and Docker Compose installed.
K8

Matching infrastructure at centron

From container to cluster: managed Kubernetes with cStack starts at €29.99 per month – control plane, autoscaler and traffic included. Explore managed Kubernetes →

Step 1 – Clone the project and modify dependencies

The first step is to clone the sample code and modify the package.json file. We will add nodemon to the development dependencies so the application automatically restarts when you make changes.

First, clone the repository:

Console
git clone https://github.com/do-community/nodejs-mongo-mongoose.git node_project
cd node_project

Open the package.json file:

Code
nano package.json

Add nodemon under the dependencies:

Code
"devDependencies": {
  "nodemon": "^1.18.10"
}

Save and close the file.

Step 2 – Adapt the application for containers

Now we will modify the code so that it works better with containers. We will use Node.js’s process.env property to dynamically work with environment variables.

Open app.js and modify the port as follows:

JavaScript
const port = process.env.PORT || 8080;
app.listen(port, function () {
  console.log(`App is listening on port ${port}`);
});

Also, modify the database connection in db.js to avoid hardcoding sensitive information:

Code
const {
  MONGO_USERNAME,
  MONGO_PASSWORD,
  MONGO_HOSTNAME,
  MONGO_PORT,
  MONGO_DB
} = process.env;

Create a .env file with the necessary environment variables:

Code
nano .env

And add the following content (with your values):

Code
MONGO_USERNAME=username
MONGO_PASSWORD=password
MONGO_PORT=27017
MONGO_DB=sharkinfo

Ensure that .env is included in both .gitignore and .dockerignore to prevent sensitive data from being uploaded.

Step 3 – Modify database connection settings

Add additional options in db.js to handle connection failures:

JavaScript
const options = {
  useNewUrlParser: true,
  reconnectTries: Number.MAX_VALUE,
  reconnectInterval: 500,
  connectTimeoutMS: 10000,
};
mongoose.connect(url, options).then(() => {
  console.log('MongoDB is connected');
}).catch(err => {
  console.log(err);
});

Step 4 – Define services with Docker Compose

Now we create the docker-compose.yml file to define the services. The first service is for the Node.js application:

YAML
version: '3'

services:
  nodejs:
    build:
      context: .
      dockerfile: Dockerfile
    image: nodejs
    container_name: nodejs
    restart: unless-stopped
    env_file: .env
    environment:
      - MONGO_USERNAME=$MONGO_USERNAME
      - MONGO_PASSWORD=$MONGO_PASSWORD
      - MONGO_HOSTNAME=db
      - MONGO_PORT=$MONGO_PORT
      - MONGO_DB=$MONGO_DB
    ports:
      - "80:8080"
    volumes:
      - .:/home/node/app
      - node_modules:/home/node/app/node_modules
    networks:
      - app-network
    command: ./wait-for.sh db:27017 -- /home/node/app/node_modules/.bin/nodemon app.js

The second service is for the MongoDB database:

YAML
db:
  image: mongo:4.1.8-xenial
  container_name: db
  restart: unless-stopped
  env_file: .env
  environment:
    - MONGO_INITDB_ROOT_USERNAME=$MONGO_USERNAME
    - MONGO_INITDB_ROOT_PASSWORD=$MONGO_PASSWORD
  volumes:
    - dbdata:/data/db
  networks:
    - app-network

Finally, add the network and volume definitions:

YAML
networks:
  app-network:
    driver: bridge

volumes:
  dbdata:
  node_modules:

Step 5 – Test the application

Start the application with:

Console
docker-compose up -d

Check the logs with:

Console
docker-compose logs

If everything runs correctly, you should be able to see your application on your server’s URL.

Conclusion

By following this guide, you have learned how to develop and run a Node.js application with MongoDB in a Docker environment. Docker makes it easier to isolate your environment, secure your data, and seamlessly share your applications.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Container & Cloud
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren