Skip to main content

Mongo-Express - Docker Compose, Nginx Reverse Proxy. Management Interface for MongoDB

771 words·
Mongo-Express MongoDB Docker-Compose Nginx

Prerequisites
#

Folder Structure
#

# Create folder structure
sudo mkdir -p /opt/mongo && cd /opt/mongo

MongoDB & Mongo-Express
#

Environment File
#

# Create environment file
sudo vi .env
# MongoDB Credentials
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: root-pw

# Mongo-Express: MongoDB Authentication
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: root-pw
ME_CONFIG_MONGODB_URL: mongodb://root:root-pw@mongo:27017/

# Mongo-Express: GUI Authentication
ME_CONFIG_BASICAUTH: "true"
ME_CONFIG_BASICAUTH_USERNAME: admin
ME_CONFIG_BASICAUTH_PASSWORD: admin-pw

Docker Compose
#

# Create Docker Compose file
sudo vi docker-compose.yml
version: '3.9'
services:

  mongo:
    image: mongo:latest
    ports:
      - "27017:27017"
    volumes:
      - ./mongodb_data:/data/db
    env_file:
      - .env
    restart: unless-stopped

  mongo-express:
    image: mongo-express:latest
    ports:
      - "8081:8081"
    env_file:
      - .env
    restart: unless-stopped
# Create / start Docker stack
sudo docker compose up -d

With Nginx Container Reverse Proxy
#

Environment File
#

# Create environment file
sudo vi .env
# MongoDB Credentials
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: root-pw

# Mongo-Express: MongoDB Authentication
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: root-pw
ME_CONFIG_MONGODB_URL: mongodb://root:root-pw@mongo:27017/

# Mongo-Express: GUI Authentication
ME_CONFIG_BASICAUTH: "true"
ME_CONFIG_BASICAUTH_USERNAME: admin
ME_CONFIG_BASICAUTH_PASSWORD: admin-pw

Docker Network
#

# Create external Docker network
sudo docker network create nginx

Docker Compose
#

version: '3.9'
services:

  mongo:
    image: mongo:latest
    ports:
      - "27017:27017"
    volumes:
      - ./mongodb_data:/data/db
    env_file:
      - .env
    networks:
      - default
    restart: unless-stopped

  mongo-express:
    image: mongo-express:latest
    ports:
      - "8081"
    env_file:
      - .env
    networks:
      - nginx
      - default
    restart: unless-stopped

  nginx:
    image: nginx:latest
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./default.conf:/etc/nginx/conf.d/default.conf
      - /etc/letsencrypt:/etc/letsencrypt
    networks:
      - nginx
    restart: unless-stopped

networks:
  nginx:
    external: true

Nginx Default Configuration
#

# Create Nginx default configuration file
sudo vi default.conf
# default.conf
server {
    listen 80;
    server_name mongo-express.jklug.work;

    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name mongo-express.jklug.work;

    ssl_certificate         /etc/letsencrypt/live/mongo-express.jklug.work/fullchain.pem;
    ssl_certificate_key     /etc/letsencrypt/live/mongo-express.jklug.work/privkey.pem;

    location / {
        proxy_pass http://mongo-express:8081;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
# Create / start Docker stack
sudo docker compose up -d

With external Nginx Reverse Proxy
#

Environment File
#

# Create environment file
sudo vi .env
# MongoDB Credentials
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: root-pw

# Mongo-Express: MongoDB Authentication
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: root-pw
ME_CONFIG_MONGODB_URL: mongodb://root:root-pw@mongo:27017/

# Mongo-Express: GUI Authentication
ME_CONFIG_BASICAUTH: "true"
ME_CONFIG_BASICAUTH_USERNAME: admin
ME_CONFIG_BASICAUTH_PASSWORD: admin-pw

Docker Network with custom Subnet
#

# Create external Docker network
sudo docker network create --subnet=192.168.11.0/24 nginx

Docker Compose
#

version: '3.9'
services:

  mongo:
    image: mongo:latest
    ports:
      - "27017:27017"
    volumes:
      - ./mongodb_data:/data/db
    env_file:
      - .env
    networks:
      default:
    restart: unless-stopped

  mongo-express:
    image: mongo-express:latest
    ports:
      - "8081"
    env_file:
      - .env
    networks:
      nginx:
        ipv4_address: 192.168.11.11
      default:
    restart: unless-stopped

networks:
  nginx:
    external: true
# Create / start Docker stack
sudo docker compose up -d

Nginx Setup
#

# Install nginx
sudo apt install nginx -y

# Copy default config
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/mongo-express.jklug.work
# Edit config
sudo vi /etc/nginx/sites-available/mongo-express.jklug.work

# mongo-express.jklug.work
server {
    listen 80;
    server_name mongo-express.jklug.work;

    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name mongo-express.jklug.work;

    ssl_certificate         /etc/letsencrypt/live/mongo-express.jklug.work/fullchain.pem;
    ssl_certificate_key     /etc/letsencrypt/live/mongo-express.jklug.work/privkey.pem;


    location / {
        proxy_pass http://192.168.11.11:8081/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
# Disable default config
sudo rm /etc/nginx/sites-enabled/default

# Enable config
sudo ln -s /etc/nginx/sites-available/mongo-express.jklug.work /etc/nginx/sites-enabled/

# Restart Nginx
sudo systemctl restart nginx

MongoDB & Mongo-Express Seperated
#

Docker Network
#

# Create external Docker network
sudo docker network create mongo &&
sudo docker network create nginx

MongoDB
#

Folder Structure
#

# Create folder structure
sudo mkdir -p /opt/mongo && cd /opt/mongo

Environment File
#

# Create environment file
sudo vi .env
# MongoDB Credentials
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: root-pw

Docker Compose
#

# Create Docker Compose file
sudo vi docker-compose.yml
version: '3.9'
services:

  mongo:
    image: mongo:latest
    ports:
      - "27017:27017"
    volumes:
      - ./mongodb_data:/data/db
    env_file:
      - .env
    networks:
      - mongo
    restart: unless-stopped

networks:
  mongo:
    external: true
# Create / start Container
sudo docker compose up -d

Mongo-Express
#

Folder Structure
#

# Create folder structure
sudo mkdir -p /opt/mongo-express && cd /opt/mongo-express

Environment File
#

# Create environment file
sudo vi .env
# Mongo-Express: MongoDB Authentication
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: root-pw
ME_CONFIG_MONGODB_URL: mongodb://root:root-pw@mongo:27017/

# Mongo-Express: GUI Authentication
ME_CONFIG_BASICAUTH: "true"
ME_CONFIG_BASICAUTH_USERNAME: admin
ME_CONFIG_BASICAUTH_PASSWORD: admin-pw

Docker Compose
#

# Create Docker Compose file
sudo vi docker-compose.yml
version: '3.9'
services:

  mongo-express:
    image: mongo-express:latest
    ports:
      - "8081"
    env_file:
      - .env
    networks:
      - mongo
      - nginx
    restart: unless-stopped

  nginx:
    image: nginx:latest
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./default.conf:/etc/nginx/conf.d/default.conf
      - /etc/letsencrypt:/etc/letsencrypt
    networks:
      - nginx
    restart: unless-stopped

networks:
  mongo:
    external: true
  nginx:
    external: true

Nginx Default Configuration
#

# Create Nginx default configuration file
sudo vi default.conf
# default.conf
server {
    listen 80;
    server_name mongo-express.jklug.work;

    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name mongo-express.jklug.work;

    ssl_certificate         /etc/letsencrypt/live/mongo-express.jklug.work/fullchain.pem;
    ssl_certificate_key     /etc/letsencrypt/live/mongo-express.jklug.work/privkey.pem;

    location / {
        proxy_pass http://mongo-express:8081;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
# Create / start Container
sudo docker compose up -d

Links #

# DockerHub MongoDB
https://hub.docker.com/_/mongo

# DockerHub Mongo-Express
https://hub.docker.com/_/mongo-express

# GitHub Mongo-Express
https://github.com/mongo-express/mongo-express/blob/master/README.md