Create a Docker Container with Node.js, MongoDB, Adminer, and NGINX Proxy Manager for SSL using Docker Compose.

Tiempo de lectura: 3 minutos

Reading time: 3 minutes

In this tutorial, you will learn how to create a Docker container that includes Node.js, MongoDB, Adminer, and NGINX Proxy Manager for SSL using Docker Compose.

Step 1: Create the docker-compose.yml file

First, create a new directory on your computer for the project, and then create a docker-compose.yml file in the root directory. This file will define the Docker services we are going to use.

Add the following to the docker-compose.yml file:

version: '3.9'

services:
  nodejs:
    image: node:14
    restart: always
    ports:
      - "3000:3000"
    volumes:
      - ./app:/app
    working_dir: /app
    command: npm run start
    environment:
      - NODE_ENV=production
      - MONGO_URI=mongodb://mongo:27017/mydatabase

  mongo:
    image: mongo:latest
    restart: always
    volumes:
      - mongo-data:/data/db

  adminer:
    image: adminer:latest
    restart: always
    ports:
      - "8080:8080"

  nginx-proxy-manager:
    image: jlesage/nginx-proxy-manager
    container_name: nginx-proxy-manager
    ports:
      - "80:80"
      - "443:443"
    environment:
      - UID=1000
      - GID=1000
    volumes:
      - ./nginx-proxy-manager/data:/config
      - ./nginx-proxy-manager/letsencrypt:/etc/letsencrypt
      - ./nginx-proxy-manager/var:/var

This file defines four Docker services:

  1. nodejs: uses the Node.js 14 image and exposes port 3000. It also defines an environment variable MONGO_URI for the Node.js application to connect to the MongoDB database.
  2. mongo: uses the latest MongoDB image and creates a volume to store the database data.
  3. adminer: uses the latest Adminer image and exposes port 8080.
  4. nginx-proxy-manager: uses the NGINX Proxy Manager image and exposes ports 80 and 443 for HTTPS. It also defines volumes to store the NGINX configuration, SSL certificates, and NGINX logs.

Step 2: Create the Node.js application

Now we need to create the Node.js application. In this tutorial, we will use a simple example that demonstrates how to connect to the MongoDB database.

First, create a new directory app inside the root directory. Then, create a file app/index.js with the following content:

const express = require('express');
const mongoose = require('mongoose');

const app = express();

mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });
const db = mongoose.connection;

db.on('error', console.error.bind(console, 'MongoDB connection error:'));

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(3000, () => {
  console.log('Example app listening on port 3000!');
});

This file defines a basic Node.js application with a root route that returns a greeting message and connects to the MongoDB database using the MONGO_URI environment variable.

Next, create a app/package.json file with the following content:

{
  "name": "nodejs-mongodb-docker",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1",
    "mongoose": "^5.13.8"
  }
}

This file defines the dependencies of the application and a start script that runs the index.js file.

Step 3: Create the necessary directories and files

Now we need to create some additional directories and files for the NGINX Proxy Manager configuration and to store SSL certificates.

Create a new directory nginx-proxy-manager inside the root directory. Then, inside the nginx-proxy-manager directory, create the following directories:

  • data: to store the NGINX Proxy Manager configuration
  • letsencrypt: to store the SSL certificates generated by Let’s Encrypt
  • var: to store the NGINX logs
    Inside the nginx-proxy-manager/data directory, create a default.conf file with the following content:

    server {
        listen 80;
        listen [::]:80;
    
        server_name localhost;
    
        location / {
            return 301 https://$host$request_uri;
        }
    }
    
    server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
    
        server_name localhost;
    
        ssl_certificate /etc/letsencrypt/live/nginx-proxy-manager/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/nginx-proxy-manager/privkey.pem;
    
        location / {
            proxy_pass http://nodejs:3000;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_redirect off;
        }
    
        location /adminer/ {
            proxy_pass http://adminer:8080/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_redirect off;
        }
    }
    

    This file defines two NGINX servers: one that redirects all HTTP requests to HTTPS, and another one that handles HTTPS requests and proxies them to the Node.js application and Adminer.

    Step 4: Run the container

    Finally, we can run the container using Docker Compose.

    Open a terminal in the root directory of the project and run the following command:

    docker-compose up -d

    This command will download the necessary Docker images and create the containers. Once the execution is complete, you can access the Node.js application in your browser at http://localhost:3000, Adminer at http://localhost:8080, and NGINX Proxy Manager at http://localhost.

    To generate SSL certificates with Let’s Encrypt, log in to NGINX Proxy Manager using the default credentials (email admin@example.com and password changeme) and follow the instructions in the web interface.

    Conclusion

    In this tutorial, you learned how to create a Docker container that includes Node.js, MongoDB, Adminer, and NGINX Proxy Manager for SSL.

Leave a Comment