Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a devcontainer configuration #163

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# [Choice] Node.js version (use -bullseye variants on local arm64/Apple Silicon): 16, 14, 12, 16-bullseye, 14-bullseye, 12-bullseye, 16-buster, 14-buster, 12-buster
ARG VARIANT=16-buster
FROM mcr.microsoft.com/vscode/devcontainers/javascript-node:0-${VARIANT}

USER node

# Install MongoDB command line tools
ARG MONGO_TOOLS_VERSION=5.0
RUN sudo apt-get update && export DEBIAN_FRONTEND=noninteractive && sudo apt-get install -y gnupg &&\
wget -qO - https://www.mongodb.org/static/pgp/server-${MONGO_TOOLS_VERSION}.asc | sudo apt-key add - &&\
echo "deb [arch=$(dpkg --print-architecture)] http://repo.mongodb.org/apt/debian $(lsb_release -cs)/mongodb-org/${MONGO_TOOLS_VERSION} main" | sudo tee /etc/apt/sources.list.d/mongodb-org-${MONGO_TOOLS_VERSION}.list &&\
sudo apt-get update && export DEBIAN_FRONTEND=noninteractive &&\
sudo apt-get install -y mongodb-database-tools mongodb-mongosh &&\
sudo apt-get clean -y && sudo rm -rf /var/lib/apt/lists/*;

# [Optional] Uncomment this section to install additional OS packages.
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
# && apt-get -y install --no-install-recommends <your-package-list-here>

# [Optional] Uncomment if you want to install an additional version of node using nvm
# ARG EXTRA_NODE_VERSION=10
# RUN su node -c "source /usr/local/share/nvm/nvm.sh && nvm install ${EXTRA_NODE_VERSION}"

# install grunt globally
RUN yarn global add grunt-cli

WORKDIR /opt/nodebb
RUN git clone https://github.com/NodeBB/NodeBB /opt/nodebb
COPY --chown=node:node ["./config.json", "/opt/nodebb/"]

# preemptively copy package.json, since to use yarn nconf will need to be installed before setup
RUN cp /opt/nodebb/install/package.json /opt/nodebb/package.json

CMD grunt -b /opt/nodebb --gruntfile /opt/nodebb/Gruntfile.js
ENTRYPOINT grunt -b /opt/nodebb --gruntfile /opt/nodebb/Gruntfile.js
30 changes: 30 additions & 0 deletions .devcontainer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Plugin devcontainer configuration
This Visual Studio Code devcontianer configuration will set up NodeBB, then link and activate the developed plugin to it. The server will rebuild and restart on any changes to the plugin.

Additionally it includes MongoDB tools ([MongoDB Shell (`mongosh`)](https://www.mongodb.com/docs/mongodb-shell/) and [MongoDB Database Tools](https://www.mongodb.com/docs/database-tools/)) in case viewing or modifying the database manually is needed.

## Credentials
Default credentials for NodeBB and MongoDB (for `nodebb` database) are simply `nodebb:nodebb` (username:password). MongoDB root user is `root:root`.

## Paths and ports
NodeBB is installed under `/opt/nodebb`. By defualt it should be exposed under port `4567` on your host, however VSCode will choose another port if that one is taken.
MongoDB is using a separate container in the same network. By defualt it should be exposed under port `27017` on your host, however VSCode will choose another port if that one is taken.

The plugin will be mounted under `/workspaces`.

## How to run

### Locally
You can use the [Remote - Containers](vscode:extension/ms-vscode-remote.remote-containers) Visual Studio Code extension and run the `Remote-Containers: Open Folder in Container...` command to select your plugin folder, or use `Remote-Containers: Reopen in Container` if you already have the folder open in VS Code.

### SSH host
You can also self-host a remote container using the [Remote - SSH](vscode:extension/ms-vscode-remote.remote-ssh) extension. See Visual Studio Code documentation [here](https://code.visualstudio.com/docs/remote/containers#_open-a-folder-on-a-remote-ssh-host-in-a-container).

### In Codespaces
Simply select `Codespaces` under the `Code` button in GitHub UI and create a new codespace. Alternatively, run `Codespaces: Create New Codespace` command in Visual Studio Code.

## Quirks

- Currently the only way to restart NodeBB is to activate the grunt file watcher by changing a plugin or NodeBB file
- NodeBB is configured with `http://127.0.0.1:4567` as its URL. Even if it doesn't match the actual URL you use it usually doesn't matter, however it can cause issues with certain types of plugins. This value can be changed in `/opt/nodebb/config.json`. Since `config.json` isn't watched by grunt it will require forcing a restart using the aformentioned method (changing another, actually watched file)
- Building the codespace will take a few minutes for NodeBB installation. On GitHub Codespaces this time can be greatly reduced by using [Codespaces Prebuilds](https://docs.github.com/en/codespaces/prebuilding-your-codespaces/about-codespaces-prebuilds).
20 changes: 20 additions & 0 deletions .devcontainer/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"url": "http://127.0.0.1:4567",
"secret": "development-value-do-not-use-in-production",
"database": "mongo",
"mongo": {
"host": "127.0.0.1",
"port": 27017,
"database": "nodebb",
"username": "nodebb",
"password": "nodebb"
},
"port": 4567,
"bind_address": "127.0.0.1",
"admin": {
"email": "[email protected]",
"username": "nodebb",
"password": "nodebb"
},
"package_manager": "yarn"
}
42 changes: 42 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.208.0/containers/javascript-node-mongo
// Update the VARIANT arg in docker-compose.yml to pick a Node.js version
{
"name": "NodeBB plugin",
"dockerComposeFile": "docker-compose.yml",
"service": "nodebb",
"workspaceFolder": "/workspace",
// Set *default* container specific settings.json values on container create.
"settings": {},
// Eslint extension is here since it integrates with the quickstart config
// MongoDB extension can be a useful alternative to `mongosh` for debugging
"extensions": [
"dbaeumer.vscode-eslint",
"mongodb.mongodb-vscode"
],
// Exposes NodeBB and MongoDB ports locally with labels
"forwardPorts": [
4567,
27017
],
"portsAttributes": {
"4567": {
"label": "NodeBB",
// show a notification when NodeBB is ready, useful since it can take a bit to set up
"onAutoForward": "notify"
},
"27017": {
"label": "MongoDB",
"onAutoForward": "silent"
}
},
// Install and setup NodeBB on workspace creation to speed up the setup.
// The build happens on startup anyway, so it can be skipped here
"onCreateCommand": "cd /opt/nodebb && yarn install && node /opt/nodebb/nodebb setup --skip-build",
"updateContentCommand": "yarn install && yarn link && npm_name=$(node -p \"require('./package.json').name\") && cd /opt/nodebb && yarn link $npm_name && /opt/nodebb/nodebb activate $npm_name",
// prepare the plugin and link it to NodeBB, then start NodeBB using grunt so it rebuilds on changes
"postStartCommand": "yarn install && yarn link && npm_name=$(node -p \"require('./package.json').name\") && cd /opt/nodebb && yarn link $npm_name && /opt/nodebb/nodebb activate $npm_name && grunt -b /opt/nodebb --gruntfile /opt/nodebb/Gruntfile.js > /opt/nodebb/logs/output.log",
// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "node",
"overrideCommand": true
}
40 changes: 40 additions & 0 deletions .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
version: '3.9'

services:
nodebb:
build:
context: .
dockerfile: Dockerfile
args:
# Update 'VARIANT' to pick an LTS version of Node.js: 16, 14, 12.
# Append -bullseye or -buster to pin to an OS version.
# Use -bullseye variants on local arm64/Apple Silicon.
VARIANT: 16-buster
volumes:
- ..:/workspace:cached
# Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
network_mode: service:mongodb
# Uncomment the next line to use a non-root user for all processes.
# user: node

# Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
# (Adding the "ports" property to this file will not forward from a Codespace.)

mongodb:
image: mongo:latest
restart: unless-stopped
volumes:
- mongodb-data:/data/db
# script that creates the nodebb user in nodebb database
- ./nodebb-db-init.js:/docker-entrypoint-initdb.d/nodebb-db-init.js
# Uncomment to change startup options
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: root
MONGO_INITDB_DATABASE: nodebb

# Add "forwardPorts": ["27017"] to **devcontainer.json** to forward MongoDB locally.
# (Adding the "ports" property to this file will not forward from a Codespace.)

volumes:
mongodb-data:
1 change: 1 addition & 0 deletions .devcontainer/nodebb-db-init.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.