Docker.Service.Server.Node.js

JavaScript source for A Docker module for Node.js, 59 lines. Raw file

const Security = require('Security')

const Docker = {
    Service: {
        Start: require("../Docker.Service.Start"),
        Server: {
            Node: (
                serviceName,
                contextPath,
                modulesPath,
                optionalHostPort,
                optionalSslPath,
                developerMode
            ) => {
                Security.String(serviceName);
                Security.String(contextPath);
                Security.String(modulesPath);
                Security.Optional(Security.Integer)(optionalHostPort)  //  min: 1
                Security.Optional(Security.String)(optionalSslPath)
                Security.Optional(Security.Boolean)(developerMode)
                developerMode = developerMode === undefined ? true : developerMode
                const developerModeLines = developerMode
                    ? []
                    : [`COPY ${contextPath} /usr/src/node/server`];

                const dockerfileContent = [
                    `FROM node:latest`,
                    `ENV LANG=C.UTF-8`,
                    `ENV DEBIAN_FRONTEND=noninteractive`,
                    `ENV TZ=Europe`,
                    `RUN apt-get update && apt-get upgrade -y`,
                    `RUN apt-get install -y nano curl wget`,
                    `RUN mkdir -p /usr/src/node/server /usr/src/node/node_modules /usr/src/node/ssl`,
                    ...developerModeLines,
                    `WORKDIR /usr/src/node/server`,
                    `CMD ["node", "index.js"]`,
                ].join(`\n`)

                const args = [
                    `-v"${modulesPath}":"/usr/src/node/node_modules"`,
                ];
                if (developerMode) {
                    args.push(`-v"${contextPath}":"/usr/src/node/server"`,)
                }
                if(optionalHostPort !== undefined ) {
                    args.push( `-p${optionalHostPort}:${optionalHostPort}` )
                }

                if (optionalSslPath !== undefined) {
                    args.push(`-v"${optionalSslPath}":"/usr/src/node/ssl"`);
                }

                return Docker.Service.Start( serviceName, dockerfileContent, contextPath, args )
            }
        }
    }
}

module.exports = Docker.Service.Server.Node