2
0

A new recipe added: nodejs_mongodb_mongoxp.

This commit is contained in:
Kovács Zoltán 2025-03-05 20:44:54 +01:00
parent 05431b716d
commit e3b0dd03de
12 changed files with 472 additions and 0 deletions

BIN
.metadata

Binary file not shown.

View File

@ -0,0 +1,7 @@
# Ignore everything else in this directory.
*
!storage
!tools
!.gitignore
!README.md
!docker-compose.yml

View File

@ -0,0 +1,113 @@
# Node.js with MongoDB and Mongo-Express tool.
#
# Provides a JavaScript runtime environment with MongoDB backend.
# Assumes a suitable JS application in nodejs-apps volume.
#
services:
#
# https://hub.docker.com/_/node
# https://github.com/nodejs/docker-node
#
nodejs:
image: node:latest
restart: unless-stopped
# Choose a suitable Linux account here.
# Must have R/W access to the nodejs-apps volume.
user: "1001"
# The application defines the port(s) to expose.
# Take a look the possible public port collision.
ports:
- 8201:8080
links:
- mongodb
volumes:
- /etc/localtime:/etc/localtime:ro
- ./storage/volumes/nodejs-apps:/home/node/app
environment:
TZ: Europe/Budapest
NODE_ENV: production
NPM_CONFIG_CACHE: /home/node/app/node_modules/.cache
NPM_CONFIG_LOGLEVEL: info
#
# Environment variables to control the docker-skeleton's
# external backup. The Node.JS image doesn't interpret them.
# You may specify the relevant folders for the backup utility.
# By default it backups the entire nodejs-apps folder.
DS_BACKUP_FOLDERS: ''
# These folders below will be excluded from the backup.
DS_BACKUP_EXCLUDES: ''
# You may specify the relevant MongoDB database(s) as well.
DS_BACKUP_DATABASES: ''
#
# Starting the application via npm and package.json:
#command: sh -c "cd /home/node/app && npm install && npm start"
# Starting a single file application (testing only):
command: node /home/node/app/helloworld.js
extra_hosts:
- "host.docker.internal:host-gateway"
labels:
com.centurylinklabs.watchtower.enable: true
#
# https://hub.docker.com/_/mongo
# https://github.com/docker-library/mongo
#
mongodb:
image: mongo:latest
restart: unless-stopped
# Choose a suitable Linux account here.
# Must have R/W access to the mongodb-data volume.
user: "1001"
volumes:
- ./storage/volumes/mongodb-data:/data/db
environment:
MONGO_INITDB_DATABASE: admin
# Sets the DBA (root) credentials below.
MONGO_INITDB_ROOT_USERNAME: admin
# It is highly recommended to change this to a strong random password.
# https://passwordsgenerator.net/
MONGO_INITDB_ROOT_PASSWORD: secret-1
# Sets the DBA (root) credentials below.
extra_hosts:
- "host.docker.internal:host-gateway"
labels:
com.centurylinklabs.watchtower.enable: true
#
# https://hub.docker.com/_/mongo-express
# https://github.com/mongo-express/mongo-express
# https://github.com/mongo-express/mongo-express-docker
#
mongoxp:
image: mongo-express
restart: unless-stopped
# Take a look the possible public port collision.
ports:
- 8202:8081
links:
- mongodb
environment:
# Override the default value set in the docker-entrypoint.sh:
# https://github.com/mongo-express/mongo-express-docker/issues/21
ME_CONFIG_MONGODB_URL: fake,fake
ME_CONFIG_MONGODB_SERVER: mongodb
ME_CONFIG_MONGODB_PORT: 27017
ME_CONFIG_SITE_BASEURL: /mongoxp/
# We don't use SSL behind a local reverse proxy.
ME_CONFIG_SITE_SSL_ENABLED: false
ME_CONFIG_SITE_SSL_CRT_PATH: ''
ME_CONFIG_SITE_SSL_KEY_PATH: ''
# We use the root account here.
ME_CONFIG_MONGODB_ENABLE_ADMIN: true
# Must match MONGO_INITDB_ROOT_* credentials.
ME_CONFIG_MONGODB_ADMINUSERNAME: admin
ME_CONFIG_MONGODB_ADMINPASSWORD: secret-1
# It is recommended to use at least a basic authentication.
ME_CONFIG_BASICAUTH: true
ME_CONFIG_BASICAUTH_USERNAME: admin
# It is highly recommended to change this to a strong random password.
# https://passwordsgenerator.net/
ME_CONFIG_BASICAUTH_PASSWORD: secret-2
ME_CONFIG_OPTIONS_EDITORTHEME: ambiance
extra_hosts:
- "host.docker.internal:host-gateway"
labels:
com.centurylinklabs.watchtower.enable: true

View File

@ -0,0 +1,4 @@
# Ignore everything in this directory except this folders.
*
!.gitignore
!volumes

View File

@ -0,0 +1,5 @@
# Ignore everything in this directory except this folders.
*
!.gitignore
!nodejs-apps
!mongodb-data

View File

@ -0,0 +1,3 @@
# Ignore everything in this directory except this folders.
*
!.gitignore

View File

@ -0,0 +1,4 @@
# Ignore everything in this directory except this folders.
*
!.gitignore
!helloworld.js

View File

@ -0,0 +1,11 @@
/*
A humble test web application.
https://www.w3schools.com/nodejs/nodejs_get_started.asp
*/
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);

View File

@ -0,0 +1,4 @@
# Ignore everything else in this directory.
*
!*.d
!.gitignore

View File

@ -0,0 +1,169 @@
#!/bin/bash
#
# A service script to dump the relevant MongoDB database(s)
# of a docker-composed MongoDB instance. Creates a tarball in
# $BASE_DIR/storage/backups/tarballs folder (by default).
# The relevant databases must be specified within the
# docker-compose.yml in a BACKUP_DATABASES environment variable.
# An optional parameter may change the target folder.
#
# Call as a Docker manager user (member of the docker Linux group) via cron.
#
# Author: Kovács Zoltán <kovacsz@marcusconsulting.hu>
# License: GNU/GPL 3+ https://www.gnu.org/licenses/gpl-3.0.en.html
# 2025-03-05 v0.1.1
# mod: minimally rewrited the description.
# 2024-09-23 v0.1 Initial version.
# Accepted environment variables and their defaults.
#
PAR_BASEDIR=${PAR_BASEDIR:-""} # Service's base folder
PAR_BACKUPDIR=${PAR_BACKUPDIR:-""} # Folder to dump within
# Messages (maybe overridden by configuration).
#
MSG_DOCKERGRPNEED="You must be a member of the docker group."
MSG_DOESNOTRUN="This service doesn't run."
MSG_MISSINGDEP="Fatal: missing dependency"
MSG_MISSINGYML="Fatal: didn't find the docker-compose.yml file"
MSG_NONWRITE="The target directory isn't writable"
MSG_NOLOCATE="Cannot locate the MongoDB container."
MSG_NOPARAM="Missing environment parameter"
# Other initialisations.
#
BACKUPDIR="storage/backups/tarballs" # Folder to dump within
PAR_DATABASES="DS_BACKUP_DATABASES" # List of DB(s) in YMLFILE
SERVICENAME="mongodb" # The composed MongoDB service
USER=${USER:-LOGNAME} # Fix for cron enviroment only
YMLFILE="docker-compose.yml" # Gets the parameters from here
# Checks the dependencies.
#
TR=$(which tr 2>/dev/null)
if [ -z "$TR" ]; then echo "$MSG_MISSINGDEP tr."; exit 1 ; fi
for item in basename cat cut date dirname docker \
grep gzip hostname id pwd tail xargs
do
if [ -n "$(which $item)" ]
then export $(echo $item | "$TR" '[:lower:]' '[:upper:]' | "$TR" '-' '_')=$(which $item)
else echo "$MSG_MISSINGDEP $item." >&2; exit 1; fi
done
# All dependencies are available via "$THECOMMAND" (upper case) call.
#
# Let's find which version of docker-compose is installed.
if [ $($DOCKER compose version 2>&1 >/dev/null; echo $?) -eq 0 ]; then
# We'll use v2 if it is available.
DOCKER_COMPOSE="$DOCKER"
commandstring="compose"
else
# Otherwise falling back to v1.
DOCKER_COMPOSE="$(which docker-compose)"
commandstring=""
fi
# One of the two is mandatory.
if [ -z "$DOCKER_COMPOSE" ];then echo "$MSG_MISSINGDEP docker-compose" >&2; exit 1; fi
# Below docker-compose should be called as "$DOCKER_COMPOSE" $commandstring sequence.
#
# An additional bugfix (use "$(which gzip)" instead of "$GZIP"):
# https://www.gnu.org/software/gzip/manual/html_node/Environment.html
GZIP=""
# Where I'm?
# https://gist.github.com/TheMengzor/968e5ea87e99d9c41782
SOURCE="$0"
while [ -h "$SOURCE" ]; do
# resolve $SOURCE until the file is no longer a symlink
SCRPATH="$( cd -P "$("$DIRNAME" "$SOURCE" )" && echo "$PWD" )" #"
SOURCE="$("$READLINK" "$SOURCE")"
# if $SOURCE was a relative symlink, we need to resolve it
# relative to the path where the symlink file was located
[[ $SOURCE != /* ]] && SOURCE="$SCRPATH/$SOURCE"
done; SCRPATH="$( cd -P "$("$DIRNAME" "$SOURCE" )" && echo "$PWD" )" #"
# Need to be root or a Docker manager user.
#
[[ "$USER" != 'root' ]] \
&& [[ -z "$(echo "$("$ID" -Gn "$USER") " | "$GREP" ' docker ')" ]] \
&& echo "$MSG_DOCKERGRPNEED" >&2 && exit 1 #"
# Searches the base folder, containing a docker-compose.yml file.
#
# Called from the base folder (./)?
BASE_DIR="$PAR_BASEDIR"
TEST_DIR="$SCRPATH"
[[ -z "$BASE_DIR" ]] && [[ -r "$TEST_DIR/$YMLFILE" ]] && BASE_DIR="$TEST_DIR"
# Called from ./tools?
TEST_DIR="$("$DIRNAME" "$TEST_DIR")"
[[ -z "$BASE_DIR" ]] && [[ -r "$TEST_DIR/$YMLFILE" ]] && BASE_DIR="$TEST_DIR"
# Called from ./tools/*.d?
TEST_DIR="$("$DIRNAME" "$TEST_DIR")"
[[ -z "$BASE_DIR" ]] && [[ -r "$TEST_DIR/$YMLFILE" ]] && BASE_DIR="$TEST_DIR"
# On failure gives it up here.
if [ -z "$BASE_DIR" -o ! -r "$BASE_DIR/$YMLFILE" ]; then
echo "$MSG_MISSINGYML" >&2; exit 1
fi
# Sets the absolute paths.
BACKUPDIR="${PAR_BACKUPDIR:-$BASE_DIR/$BACKUPDIR}"
# The dump target folder must be writable.
#
[[ ! -w "$BACKUPDIR" ]] \
&& echo "$MSG_NONWRITE: $BACKUPDIR" >&2 && exit 1
# The service must be running - silently gives up here if not.
#
[[ -z "$(cd "$BASE_DIR"; "$DOCKER_COMPOSE" $commandstring ps --services --filter "status=running")" ]] \
&& exit 1
# Converts the MongoDB service name to an actual running container's name.
#
MDBCONTAINER="$("$DOCKER" inspect -f '{{.Name}}' $(cd "$BASE_DIR"; "$DOCKER_COMPOSE" $commandstring ps -q "$SERVICENAME") | "$CUT" -c2-)"
# Gives up here if failed.
if [ -z "$MDBCONTAINER" ]; then echo "$MSG_NOLOCATE" >&2; exit 1; fi
# Checks and parses the config file for database names to dump
# and DBA (root) credentials for MongoDB.
#
function parse { [[ -z "$1" ]] && return
# Gets the live lines containing the parameter.
value=$("$CAT" "$CONFFILE" 2>/dev/null | "$GREP" -ve '^#' | \
"$GREP" -e " $1:" | "$TR" -d '\r')
# If multiple the last one to consider.
value=$(echo -e "$value" | "$TAIL" -n1)
# Right side of the equal sign W/O leading and trailing spaces and quotes.
value=$(echo -ne "$value" | "$CUT" -d':' -f2 | "$XARGS")
echo -e "$value"; return
}
# Examines the YMLFILE.
CONFFILE="$BASE_DIR/$YMLFILE"
# Gets the list of the databases to dump. Silently exits if it is empty.
DATABASES="$(parse "$PAR_DATABASES")"
if [ -z "$DATABASES" ]; then exit; fi
# All parameters below are mandatories.
DBAUTH="$(parse "MONGO_INITDB_DATABASE")"
if [ -z "$DBAUTH" ]; then echo "$MSG_NOPARAM MONGO_INITDB_DATABASE" >&2; exit 1; fi
DBUSER="$(parse "MONGO_INITDB_ROOT_USERNAME")"
if [ -z "$DBAUTH" ]; then echo "$MSG_NOPARAM MONGO_INITDB_ROOT_USERNAME" >&2; exit 1; fi
DBPASS="$(parse "MONGO_INITDB_ROOT_PASSWORD")"
if [ -z "$DBAUTH" ]; then echo "$MSG_NOPARAM MONGO_INITDB_ROOT_PASSWORD" >&2; exit 1; fi
# We've the configuration parsed.
# Attempts the dump(s) using the mongodump utility existing within the container.
# Uses the DBA (root) credentials parsed from the YMLFILE above.
#
if [ -w "$BACKUPDIR" ]; then
# Enumerates the relevant databases (if any).
for DBNAME in $DATABASES ''
do
# Dumps the actual database as a DBA.
if [ -n "$DBNAME" ]; then
BACKUP_NAME=$SERVICENAME-$DBNAME.$("$DATE" '+%Y%m%d_%H%M%S').$("$HOSTNAME")
"$DOCKER" exec $MDBCONTAINER sh -c "exec mongodump -u $DBUSER -p $DBPASS --authenticationDatabase $DBAUTH -d $DBNAME --quiet --archive" | \
"$(which gzip)" > "$BACKUPDIR/$BACKUP_NAME.archive.gz" 2>>"$BACKUPDIR/$BACKUP_NAME.log"
fi
done
fi
# That's all, Folks! :)

View File

@ -0,0 +1,152 @@
#!/bin/bash
#
# A service script to backup the application's storage (with exceptions)
# of a docker-composed Node.JS instance. Creates a tarball in
# $BASE_DIR/storage/backups/tarballs folder (by default). An optional
# parameter may change the target folder.
#
# The contents of the tarball can be refined by setting the SD_BACKUP_*
# environment variables in the docker-compose.yml file.
#
# Call as a Docker manager user (member of the docker Linux group) via cron.
#
# Author: Kovács Zoltán <kovacsz@marcusconsulting.hu>
# License: GNU/GPL 3+ https://www.gnu.org/licenses/gpl-3.0.en.html
# 2024-09-23 v0.1 Initial version.
# Accepted environment variables and their defaults.
#
PAR_BASEDIR=${PAR_BASEDIR:-""} # Service's base folder
PAR_BACKUPDIR=${PAR_BACKUPDIR:-""} # Folder to dump within
# Messages (maybe overridden by configuration).
#
MSG_DOCKERGRPNEED="You must be a member of the docker group."
MSG_DOESNOTRUN="This service doesn't run."
MSG_MISSINGDEP="Fatal: missing dependency"
MSG_MISSINGYML="Fatal: didn't find the docker-compose.yml file"
MSG_NONWRITE="The target directory isn't writable"
MSG_NOLOCATE="Cannot locate the Node.JS container."
# Other initialisations.
#
APPSDIR="/home/node/app" # Base folder of storage to dump
BACKUPDIR="storage/backups/tarballs" # Folder to dump within
SERVICENAME="nodejs" # The composed Node.JS service
USER=${USER:-LOGNAME} # Fix for cron enviroment only
YMLFILE="docker-compose.yml"
# Checks the dependencies.
#
TR=$(which tr 2>/dev/null)
if [ -z "$TR" ]; then echo "$MSG_MISSINGDEP tr."; exit 1 ; fi
for item in basename cat cut date dirname docker \
find grep hostname id pwd tail xargs
do
if [ -n "$(which $item)" ]
then export $(echo $item | "$TR" '[:lower:]' '[:upper:]' | "$TR" '-' '_')=$(which $item)
else echo "$MSG_MISSINGDEP $item." >&2; exit 1; fi
done
# All dependencies are available via "$THECOMMAND" (upper case) call.
#
# Let's find which version of docker-compose is installed.
if [ $($DOCKER compose version 2>&1 >/dev/null; echo $?) -eq 0 ]; then
# We'll use v2 if it is available.
DOCKER_COMPOSE="$DOCKER"
commandstring="compose"
else
# Otherwise falling back to v1.
DOCKER_COMPOSE="$(which docker-compose)"
commandstring=""
fi
# One of the two is mandatory.
if [ -z "$DOCKER_COMPOSE" ];then echo "$MSG_MISSINGDEP docker-compose" >&2; exit 1; fi
# Below docker-compose should be called as "$DOCKER_COMPOSE" $commandstring sequence.
# Where I'm?
# https://gist.github.com/TheMengzor/968e5ea87e99d9c41782
SOURCE="$0"
while [ -h "$SOURCE" ]; do
# resolve $SOURCE until the file is no longer a symlink
SCRPATH="$( cd -P "$("$DIRNAME" "$SOURCE" )" && echo "$PWD" )" #"
SOURCE="$("$READLINK" "$SOURCE")"
# if $SOURCE was a relative symlink, we need to resolve it
# relative to the path where the symlink file was located
[[ $SOURCE != /* ]] && SOURCE="$SCRPATH/$SOURCE"
done; SCRPATH="$( cd -P "$("$DIRNAME" "$SOURCE" )" && echo "$PWD" )" #"
# Need to be root or a Docker manager user.
#
[[ "$USER" != 'root' ]] \
&& [[ -z "$(echo "$("$ID" -Gn "$USER") " | "$GREP" ' docker ')" ]] \
&& echo "$MSG_DOCKERGRPNEED" >&2 && exit 1 #"
# Searches the base folder, containing a docker-compose.yml file.
#
# Called from the base folder (./)?
BASE_DIR="$PAR_BASEDIR"
TEST_DIR="$SCRPATH"
[[ -z "$BASE_DIR" ]] && [[ -r "$TEST_DIR/$YMLFILE" ]] && BASE_DIR="$TEST_DIR"
# Called from ./tools?
TEST_DIR="$("$DIRNAME" "$TEST_DIR")"
[[ -z "$BASE_DIR" ]] && [[ -r "$TEST_DIR/$YMLFILE" ]] && BASE_DIR="$TEST_DIR"
# Called from ./tools/*.d?
TEST_DIR="$("$DIRNAME" "$TEST_DIR")"
[[ -z "$BASE_DIR" ]] && [[ -r "$TEST_DIR/$YMLFILE" ]] && BASE_DIR="$TEST_DIR"
# On failure gives it up here.
if [ -z "$BASE_DIR" -o ! -r "$BASE_DIR/$YMLFILE" ]; then
echo "$MSG_MISSINGYML" >&2; exit 1
fi
# Sets the absolute paths.
BACKUPDIR="${PAR_BACKUPDIR:-$BASE_DIR/$BACKUPDIR}"
# The dump target folder must be writable.
#
[[ ! -w "$BACKUPDIR" ]] \
&& echo "$MSG_NONWRITE: $BACKUPDIR" >&2 && exit 1
# The service must be running - silently gives up here if not.
#
[[ -z "$(cd "$BASE_DIR"; "$DOCKER_COMPOSE" $commandstring ps --services --filter "status=running")" ]] \
&& exit 1
# Converts the Node.JS service name to an actual running container's name.
#
NDCONTAINER="$("$DOCKER" inspect -f '{{.Name}}' $(cd "$BASE_DIR"; "$DOCKER_COMPOSE" $commandstring ps -q "$SERVICENAME") | "$CUT" -c2-)"
# Gives up here if failed.
if [ -z "$NDCONTAINER" ]; then echo "$MSG_NOLOCATE" >&2; exit 1; fi
# Checks and parses the config file for the folder (path)names
# to dump and to exclude.
#
function parse { [[ -z "$1" ]] && return
# Gets the live lines containing the parameter.
value=$("$CAT" "$CONFFILE" 2>/dev/null | "$GREP" -ve '^#' | \
"$GREP" -e " $1:" | "$TR" -d '\r')
# If multiple the last one to consider.
value=$(echo -e "$value" | "$TAIL" -n1)
# Right side of the equal sign W/O leading and trailing spaces and quotes.
value=$(echo -ne "$value" | "$CUT" -d':' -f2 | "$XARGS")
echo -e "$value"; return
}
# Examines the YMLFILE.
CONFFILE="$BASE_DIR/$YMLFILE"
# Gets the list of folders to dump and makes the path relative (some sanitization).
# Sets the app's root if no folders given.
FOLDERS=""
for folder in $(parse "DS_BACKUP_FOLDERS") ''; do [[ -n "$folder" ]] && FOLDERS+=" ./$folder"; done
[[ -z "$FOLDERS" ]] && FOLDERS="."
# Gets the list of excludes as well. Converts them to tar parameters.
EXCLUDES=""
for exclude in $(parse "DS_BACKUP_EXCLUDES") ''; do [[ -n "$exclude" ]] && EXCLUDES+="--exclude='./$exclude' "; done
# We've folders and excludes prepared.
# Tries the FS backup.
if [ -w "$BACKUPDIR" ]; then
BACKUP_NAME="storage.$("$DATE" '+%Y%m%d_%H%M%S').$("$HOSTNAME")"
"$DOCKER" exec $NDCONTAINER sh \
-c "cd $APPSDIR; tar $EXCLUDES -cz $FOLDERS" \
> "$BACKUPDIR/$BACKUP_NAME.tgz" 2>>"$BACKUPDIR/$BACKUP_NAME.log"
fi
# That's all, Folks! :)