Docker.Service.Program.PHP.js

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

const Security = require('Security');

const Docker =
{
    Config: require('./../../Docker.Config'),
    Service:
    {
        Start: require('./../Docker.Service.Start'),
        Program:
        {
            PHP: (
                serviceName,
                contextPath,
                executePhpFile,
                hostPort,
                optionalStdIn,
            ) => {
                Security.String(serviceName)
                Security.String(contextPath)
                Security.Optional(Security.String)(executePhpFile)
                Security.Optional(Security.Number)(hostPort)  //  min: 1
                Security.Optional(Security.String)(optionalStdIn)

                let runCommand
                const logPath = `/var/log/${serviceName}`
                if (optionalStdIn !== undefined && executePhpFile !== undefined) {
                    runCommand = `CMD ["php", "/usr/src/php/${executePhpFile}", "<", "${optionalStdIn}"`
                }
                else if (executePhpFile !== undefined) {
                    runCommand = `CMD ["php", "/usr/src/php/${executePhpFile}"`
                }
                else {
                    runCommand = 'CMD ["tail" "-f" "/dev/null"'
                }

                const writeLog = Docker.Config._logPath !== undefined
                if (writeLog) {
                    runCommand += `, ">", "${logPath}/stdout.log"]`
                }
                else {
                    runCommand += `]`
                }

                const dockerfileContent = [
                    `FROM php:8.1-cli`,
                    `WORKDIR /usr/src/php`,
                    `RUN docker-php-ext-install pdo pdo_mysql`,
                    runCommand
                ].join(`\n`)

                const portArg = hostPort !== undefined
                    ? [`-p${hostPort}:${hostPort}`]
                    : []

                const args = [
                    ...portArg,
                    `-v"${contextPath}":"/usr/src/php"`,
                ];

                if (writeLog) {
                    args.push(`-v"${Docker.Config._logPath}":"${logPath}"`)
                }

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

module.exports = Docker.Service.Program.PHP