What is Docker Compose and why use it here?

Docker includes a tool called Docker Compose that helps you create multi container applications. We’ll use it to create two containers that will run nodebb and mongodb (named as such respectively). We’ll set nodebb to depend on mongodb container so it will wait for mongodb to run first. This whole docker-compose file lets us write docker run arguments in a config file so we can easily tweak stuff. 

Benefits of Docker Compose

  • Single host deployment – This means you can run everything on a single piece of hardware
  • Quick and easy configuration – Due to YAML scripts
  • High productivity – Docker Compose reduces the time it takes to perform tasks
  • Security – All the containers are isolated from each other, reducing the threat landscape

How to setup Nodebb in a docker container through docker-compose

version: "3.3"
services:
  nodebb:
    container_name: ${PROJECT_NAME}
    ports:
      - "4567:4567"
    restart: unless-stopped
    volumes:
       - ${NODEBB_VOLUME_PATH}:/data

    environment:
      - URL=${URL} #Nodebb URL for config.js
      - DB_HOST=mongodb:27017
      - DATABASE=mongo
      - DB_PORT=27017
      - DB_USER=${DB_USER}
      - DB_PASSWORD=${DB_PASSWORD}
    image: "nibrev/nodebb:latest"
    depends_on:
      - mongodb
    links:
      - mongodb
    networks:
      - mongodb_network

  mongodb:
    container_name: ${PROJECT_NAME}_mongodb
    image: "docker.io/bitnami/mongodb:4.4-debian-10"
    user: root
    environment:
      - MONGODB_USERNAME=${MONGODB_USERNAME}
      - MONGODB_PASSWORD=${MONGODB_PASSWORD}
      - MONGODB_DATABASE=${MONGODB_DATABASE}
      - MONGODB_ROOT_PASSWORD=${MONGODB_ROOT_PASSWORD}
    restart: unless-stopped
    volumes:
      - ${MONGODB_VOLUME_PATH}:/bitnami/mongodb
    networks:
      - mongodb_network

volumes:
  mongodb_data:
    driver: local

networks:
  mongodb_network:
    driver: bridge
nodebb mongo docker-compose

This works in tandom with a .env file. The env file will hold values of all the variables used here. Please replace them with the name of your website/domain and usernames and password values. This setup will create a user, database and password on mongo db automatically. I like to use bitnami images because they provide comprehensive documentation and maintain there images. You can read their docs here https://github.com/bitnami/bitnami-docker-mongodb.

Example of .env file

PROJECT_NAME=
URL=


#Nodebb specific variables
DB_USER=
DB_PASSWORD=
DB_USER=
NODEBB_VOLUME_PATH=


#Bitnami MongoDB specific variables
MONGODB_USERNAME=
MONGODB_PASSWORD=
MONGODB_DATABASE=
MONGODB_ROOT_PASSWORD=
MONGODB_VOLUME_PATH=
.env for docker-compose
Detailed information

Troubleshooting

Possible Permission Issues

As we are using Bitnami’s public images for Mongodb we need to look at their docs for any mongodb related issues. In recent versions they are defaulting to non-root containers for better security so it is better to setup without the root user. What that means is that your db volume folder needs to give access to 1001 user.

You can remove following from the docker-compose file and give permissions to your shared volume and then try again.

user:root
chown -R 1001:1001 ${MONGODB_DATABASE}

# e.g
chown -R 1001:1001 /mongodb

Benefits of developing on nodebb through docker

This approach is recommended for development setups only

  • Auto installation
  • Automatic Nodebb and Mongodb version updates

  • Auto upgrade when you update image
  • Persistent storage support
  • Easily setup multiple forums
  • No need for a database server
  • Docker configs in Docker compose so need to remember parameters