feat(node): improve docker setup for Node server apps (#14647)
This commit is contained in:
parent
6093247df2
commit
9f1ed50fc6
@ -89,7 +89,7 @@
|
||||
"framework": {
|
||||
"description": "Generate the node application using a framework",
|
||||
"type": "string",
|
||||
"enum": ["express", "koa", "fastify", "none"],
|
||||
"enum": ["express", "fastify", "koa", "none"],
|
||||
"default": "none",
|
||||
"x-prompt": "Which framework do you want to use?",
|
||||
"x-priority": "important"
|
||||
|
||||
@ -9,10 +9,12 @@
|
||||
"description": "Nx Node Docker Options Schema.",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"projectName": {
|
||||
"project": {
|
||||
"description": "The name of the project",
|
||||
"$default": { "$source": "argv", "index": 0 },
|
||||
"type": "string"
|
||||
"$default": { "$source": "projectName" },
|
||||
"type": "string",
|
||||
"x-prompt": "What project would you like to add a Dockerfile to?",
|
||||
"x-priority": "important"
|
||||
},
|
||||
"targetName": {
|
||||
"description": "The name of the target to create",
|
||||
|
||||
@ -701,14 +701,14 @@ async function determineFramework(
|
||||
name: 'express',
|
||||
message: 'Express [https://expressjs.com/]',
|
||||
},
|
||||
{
|
||||
name: 'koa',
|
||||
message: 'koa [https://koajs.com/]',
|
||||
},
|
||||
{
|
||||
name: 'fastify',
|
||||
message: 'fastify [https://www.fastify.io/]',
|
||||
},
|
||||
{
|
||||
name: 'koa',
|
||||
message: 'koa [https://koajs.com/]',
|
||||
},
|
||||
];
|
||||
|
||||
if (!parsedArgs.framework) {
|
||||
|
||||
@ -59,7 +59,10 @@ function getWebpackBuildConfig(
|
||||
options: {
|
||||
target: 'node',
|
||||
compiler: 'tsc',
|
||||
outputPath: joinPathFragments('dist', options.appProjectRoot),
|
||||
outputPath: joinPathFragments(
|
||||
'dist',
|
||||
options.rootProject ? options.name : options.appProjectRoot
|
||||
),
|
||||
main: joinPathFragments(
|
||||
project.sourceRoot,
|
||||
'main' + (options.js ? '.js' : '.ts')
|
||||
@ -90,7 +93,10 @@ function getEsBuildConfig(
|
||||
executor: '@nrwl/esbuild:esbuild',
|
||||
outputs: ['{options.outputPath}'],
|
||||
options: {
|
||||
outputPath: joinPathFragments('dist', options.appProjectRoot),
|
||||
outputPath: joinPathFragments(
|
||||
'dist',
|
||||
options.rootProject ? options.name : options.appProjectRoot
|
||||
),
|
||||
format: ['cjs'],
|
||||
main: joinPathFragments(
|
||||
project.sourceRoot,
|
||||
@ -373,7 +379,7 @@ export async function applicationGenerator(tree: Tree, schema: Schema) {
|
||||
if (options.docker) {
|
||||
const dockerTask = await setupDockerGenerator(tree, {
|
||||
...options,
|
||||
projectName: options.name,
|
||||
project: options.name,
|
||||
});
|
||||
|
||||
tasks.push(dockerTask);
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import express from 'express';
|
||||
|
||||
const host = process.env.HOST ?? 'localhost';
|
||||
const port = process.env.PORT ? Number(process.env.PORT) : <%= port %>;
|
||||
|
||||
const app = express();
|
||||
@ -8,6 +9,6 @@ app.get('/', (req, res) => {
|
||||
res.send({ 'message': 'Hello API'});
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`[ ready ] http://localhost:${port}`);
|
||||
app.listen(port, host, () => {
|
||||
console.log(`[ ready ] http://${host}:${port}`);
|
||||
});
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import fastify from 'fastify';
|
||||
|
||||
const host = process.env.HOST ?? 'localhost';
|
||||
const port = process.env.PORT ? Number(process.env.PORT) : <%= port %>;
|
||||
|
||||
const app = fastify();
|
||||
@ -10,8 +11,8 @@ app.get('/', async (req, res) => {
|
||||
|
||||
const start = async() => {
|
||||
try {
|
||||
await app.listen({ port });
|
||||
console.log(`[ ready ] http://localhost:${port}`);
|
||||
await app.listen({ host, port });
|
||||
console.log(`[ ready ] http://${host}:${port}`);
|
||||
} catch (err) {
|
||||
// Errors are logged here
|
||||
process.exit(1);
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import koa from 'koa';
|
||||
|
||||
const host = process.env.HOST ?? 'localhost';
|
||||
const port = process.env.PORT ? Number(process.env.PORT) : <%= port %>;
|
||||
|
||||
const app = new koa();
|
||||
@ -8,6 +9,6 @@ app.use(async ctx =>{
|
||||
ctx.body = { 'message': 'Hello API'};
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`[ ready ] http://localhost:${port}`);
|
||||
app.listen(port, host, () => {
|
||||
console.log(`[ ready ] http://${host}:${port}`);
|
||||
});
|
||||
|
||||
@ -89,7 +89,7 @@
|
||||
"framework": {
|
||||
"description": "Generate the node application using a framework",
|
||||
"type": "string",
|
||||
"enum": ["express", "koa", "fastify", "none"],
|
||||
"enum": ["express", "fastify", "koa", "none"],
|
||||
"default": "none",
|
||||
"x-prompt": "Which framework do you want to use?",
|
||||
"x-priority": "important"
|
||||
|
||||
@ -1,11 +1,20 @@
|
||||
# This file is generated by Nx.
|
||||
#
|
||||
# Build the docker image with `npx nx docker-build <%= project %>`.
|
||||
# Tip: Modify "docker-build" options in project.json to change docker build args.
|
||||
#
|
||||
# Run the container with `docker run -p 3000:3000 -t <%= project %>`.
|
||||
FROM docker.io/node:lts-alpine
|
||||
|
||||
ENV HOST=0.0.0.0
|
||||
ENV PORT=3000
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
RUN addgroup --system <%= projectName %> && \
|
||||
adduser --system -G <%= projectName %> <%= projectName %>
|
||||
RUN addgroup --system <%= project %> && \
|
||||
adduser --system -G <%= project %> <%= project %>
|
||||
|
||||
COPY <%= buildLocation %> dist
|
||||
RUN chown -R <%= projectName %>:<%= projectName %> .
|
||||
COPY <%= buildLocation %> <%= project %>
|
||||
RUN chown -R <%= project %>:<%= project %> .
|
||||
|
||||
CMD [ "node", "dist" ]
|
||||
CMD [ "node", "<%= project %>" ]
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
export interface SetUpDockerOptions {
|
||||
projectName?: string;
|
||||
project?: string;
|
||||
targetName?: string;
|
||||
buildTarget?: string;
|
||||
skipFormat?: boolean;
|
||||
|
||||
@ -6,10 +6,14 @@
|
||||
"description": "Nx Node Docker Options Schema.",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"projectName": {
|
||||
"project": {
|
||||
"description": "The name of the project",
|
||||
"$default": { "$source": "argv", "index": 0 },
|
||||
"type": "string"
|
||||
"$default": {
|
||||
"$source": "projectName"
|
||||
},
|
||||
"type": "string",
|
||||
"x-prompt": "What project would you like to add a Dockerfile to?",
|
||||
"x-priority": "important"
|
||||
},
|
||||
"targetName": {
|
||||
"description": "The name of the target to create",
|
||||
|
||||
@ -25,7 +25,7 @@ describe('setupDockerGenerator', () => {
|
||||
dependsOn: ['build'],
|
||||
executor: 'nx:run-commands',
|
||||
options: {
|
||||
commands: ['docker build -f api/Dockerfile .'],
|
||||
commands: ['docker build -f api/Dockerfile . -t api'],
|
||||
},
|
||||
},
|
||||
})
|
||||
@ -50,7 +50,7 @@ describe('setupDockerGenerator', () => {
|
||||
dependsOn: ['build'],
|
||||
executor: 'nx:run-commands',
|
||||
options: {
|
||||
commands: ['docker build -f Dockerfile .'],
|
||||
commands: ['docker build -f Dockerfile . -t api'],
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
@ -20,14 +20,14 @@ function normalizeOptions(
|
||||
): SetUpDockerOptions {
|
||||
return {
|
||||
...setupOptions,
|
||||
projectName: setupOptions.projectName ?? readNxJson(tree).defaultProject,
|
||||
project: setupOptions.project ?? readNxJson(tree).defaultProject,
|
||||
targetName: setupOptions.targetName ?? 'docker-build',
|
||||
buildTarget: setupOptions.buildTarget ?? 'build',
|
||||
};
|
||||
}
|
||||
|
||||
function addDocker(tree: Tree, options: SetUpDockerOptions) {
|
||||
const project = readProjectConfiguration(tree, options.projectName);
|
||||
const project = readProjectConfiguration(tree, options.project);
|
||||
if (!project || !options.targetName) {
|
||||
return;
|
||||
}
|
||||
@ -43,13 +43,13 @@ function addDocker(tree: Tree, options: SetUpDockerOptions) {
|
||||
tmpl: '',
|
||||
app: project.sourceRoot,
|
||||
buildLocation: outputPath,
|
||||
projectName: options.projectName,
|
||||
project: options.project,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function updateProjectConfig(tree: Tree, options: SetUpDockerOptions) {
|
||||
let projectConfig = readProjectConfiguration(tree, options.projectName);
|
||||
let projectConfig = readProjectConfiguration(tree, options.project);
|
||||
|
||||
projectConfig.targets[`${options.targetName}`] = {
|
||||
dependsOn: [`${options.buildTarget}`],
|
||||
@ -59,12 +59,12 @@ export function updateProjectConfig(tree: Tree, options: SetUpDockerOptions) {
|
||||
`docker build -f ${joinPathFragments(
|
||||
projectConfig.root,
|
||||
'Dockerfile'
|
||||
)} .`,
|
||||
)} . -t ${options.project}`,
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
updateProjectConfiguration(tree, options.projectName, projectConfig);
|
||||
updateProjectConfiguration(tree, options.project, projectConfig);
|
||||
}
|
||||
|
||||
export async function setupDockerGenerator(
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user