Build server during Docker build, not when starting app (#1008)

This commit is contained in:
Shayne Czyzewski 2023-02-17 13:33:25 -05:00 committed by GitHub
parent 868362d103
commit bfbb57ecd0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 41 additions and 29 deletions

View File

@ -1,6 +1,9 @@
# Changelog # Changelog
## v0.8.2 ## v0.8.2
### Non-breaking Changes
- The Dockerfile has been updated to build the server files during the Docker build stage instead of during server startup. This will reduce the memory footprint required for running apps.
### Bug fixes ### Bug fixes
- Fixes a file lock error that kills CLI when changing entities with `wasp start` running on newer Macs. - Fixes a file lock error that kills CLI when changing entities with `wasp start` running on newer Macs.

View File

@ -25,16 +25,15 @@ RUN apk add --no-cache openssl1.1-compat
FROM base AS server-builder FROM base AS server-builder
RUN apk add --no-cache build-base libtool autoconf automake RUN apk add --no-cache build-base libtool autoconf automake
WORKDIR /app WORKDIR /app
COPY server/ ./server/
# Install npm packages, resulting in node_modules/. # Install npm packages, resulting in node_modules/.
{=# usingServerPatches =}
COPY server/patches ./server/patches
{=/ usingServerPatches =}
COPY server/package*.json ./server/
RUN cd server && npm install RUN cd server && npm install
{=# usingPrisma =} {=# usingPrisma =}
COPY db/schema.prisma ./db/ COPY db/schema.prisma ./db/
RUN cd server && {= serverPrismaClientOutputDirEnv =} npx prisma generate --schema='{= dbSchemaFileFromServerDir =}' RUN cd server && {= serverPrismaClientOutputDirEnv =} npx prisma generate --schema='{= dbSchemaFileFromServerDir =}'
{=/ usingPrisma =} {=/ usingPrisma =}
# Building the server should come after Prisma generation.
RUN cd server && npm run build
# TODO: Use pm2? # TODO: Use pm2?
@ -45,7 +44,8 @@ RUN apk add --no-cache python3
ENV NODE_ENV production ENV NODE_ENV production
WORKDIR /app WORKDIR /app
COPY --from=server-builder /app/server/node_modules ./server/node_modules COPY --from=server-builder /app/server/node_modules ./server/node_modules
COPY server/ ./server/ COPY --from=server-builder /app/server/dist ./server/dist
COPY --from=server-builder /app/server/package*.json ./server/
COPY db/ ./db/ COPY db/ ./db/
EXPOSE ${PORT} EXPOSE ${PORT}
WORKDIR /app/server WORKDIR /app/server

View File

@ -11,7 +11,7 @@
"file", "file",
"Dockerfile" "Dockerfile"
], ],
"576e1a40a9d7e838cdbe23a504c9a8d4d53d8614e2bcd2d01838fa63541674b6" "ef07c51e4998902c4b76e6e602659125cee390c0640c6b4a1f4f7ba8ac92ec7e"
], ],
[ [
[ [
@ -53,7 +53,7 @@
"file", "file",
"server/package.json" "server/package.json"
], ],
"944ae7609fb9b3ff6f84ea9212e2f82b4fe70f716f31685fee9cc3cf232f67da" "bea0d57a5cec30f522ed36a84db0069498b00f6f3602e3aa035abdb6bfe82905"
], ],
[ [
[ [

View File

@ -24,9 +24,11 @@ RUN apk add --no-cache openssl1.1-compat
FROM base AS server-builder FROM base AS server-builder
RUN apk add --no-cache build-base libtool autoconf automake RUN apk add --no-cache build-base libtool autoconf automake
WORKDIR /app WORKDIR /app
COPY server/ ./server/
# Install npm packages, resulting in node_modules/. # Install npm packages, resulting in node_modules/.
COPY server/package*.json ./server/
RUN cd server && npm install RUN cd server && npm install
# Building the server should come after Prisma generation.
RUN cd server && npm run build
# TODO: Use pm2? # TODO: Use pm2?
@ -37,7 +39,8 @@ RUN apk add --no-cache python3
ENV NODE_ENV production ENV NODE_ENV production
WORKDIR /app WORKDIR /app
COPY --from=server-builder /app/server/node_modules ./server/node_modules COPY --from=server-builder /app/server/node_modules ./server/node_modules
COPY server/ ./server/ COPY --from=server-builder /app/server/dist ./server/dist
COPY --from=server-builder /app/server/package*.json ./server/
COPY db/ ./db/ COPY db/ ./db/
EXPOSE ${PORT} EXPOSE ${PORT}
WORKDIR /app/server WORKDIR /app/server

View File

@ -43,7 +43,7 @@
"postinstall": "patch-package", "postinstall": "patch-package",
"standard": "standard", "standard": "standard",
"start": "NODE_PATH=dist node -r dotenv/config dist/server.js", "start": "NODE_PATH=dist node -r dotenv/config dist/server.js",
"start-production": "NODE_ENV=production npm run build-and-start", "start-production": "NODE_ENV=production npm run start",
"watch": "nodemon --exec 'npm run build-and-start || exit 1'" "watch": "nodemon --exec 'npm run build-and-start || exit 1'"
}, },
"type": "module", "type": "module",

View File

@ -11,7 +11,7 @@
"file", "file",
"Dockerfile" "Dockerfile"
], ],
"576e1a40a9d7e838cdbe23a504c9a8d4d53d8614e2bcd2d01838fa63541674b6" "ef07c51e4998902c4b76e6e602659125cee390c0640c6b4a1f4f7ba8ac92ec7e"
], ],
[ [
[ [
@ -53,7 +53,7 @@
"file", "file",
"server/package.json" "server/package.json"
], ],
"944ae7609fb9b3ff6f84ea9212e2f82b4fe70f716f31685fee9cc3cf232f67da" "bea0d57a5cec30f522ed36a84db0069498b00f6f3602e3aa035abdb6bfe82905"
], ],
[ [
[ [

View File

@ -24,9 +24,11 @@ RUN apk add --no-cache openssl1.1-compat
FROM base AS server-builder FROM base AS server-builder
RUN apk add --no-cache build-base libtool autoconf automake RUN apk add --no-cache build-base libtool autoconf automake
WORKDIR /app WORKDIR /app
COPY server/ ./server/
# Install npm packages, resulting in node_modules/. # Install npm packages, resulting in node_modules/.
COPY server/package*.json ./server/
RUN cd server && npm install RUN cd server && npm install
# Building the server should come after Prisma generation.
RUN cd server && npm run build
# TODO: Use pm2? # TODO: Use pm2?
@ -37,7 +39,8 @@ RUN apk add --no-cache python3
ENV NODE_ENV production ENV NODE_ENV production
WORKDIR /app WORKDIR /app
COPY --from=server-builder /app/server/node_modules ./server/node_modules COPY --from=server-builder /app/server/node_modules ./server/node_modules
COPY server/ ./server/ COPY --from=server-builder /app/server/dist ./server/dist
COPY --from=server-builder /app/server/package*.json ./server/
COPY db/ ./db/ COPY db/ ./db/
EXPOSE ${PORT} EXPOSE ${PORT}
WORKDIR /app/server WORKDIR /app/server

View File

@ -43,7 +43,7 @@
"postinstall": "patch-package", "postinstall": "patch-package",
"standard": "standard", "standard": "standard",
"start": "NODE_PATH=dist node -r dotenv/config dist/server.js", "start": "NODE_PATH=dist node -r dotenv/config dist/server.js",
"start-production": "NODE_ENV=production npm run build-and-start", "start-production": "NODE_ENV=production npm run start",
"watch": "nodemon --exec 'npm run build-and-start || exit 1'" "watch": "nodemon --exec 'npm run build-and-start || exit 1'"
}, },
"type": "module", "type": "module",

View File

@ -11,7 +11,7 @@
"file", "file",
"Dockerfile" "Dockerfile"
], ],
"576e1a40a9d7e838cdbe23a504c9a8d4d53d8614e2bcd2d01838fa63541674b6" "ef07c51e4998902c4b76e6e602659125cee390c0640c6b4a1f4f7ba8ac92ec7e"
], ],
[ [
[ [
@ -53,7 +53,7 @@
"file", "file",
"server/package.json" "server/package.json"
], ],
"3e6985d644af90e8a9329298fd3ffd50235526abebe1b63b5bb300b396745ee7" "ea1a8e739dc99d3f115420c09d131f53f9874db8fa20f95760275235de97b1b5"
], ],
[ [
[ [

View File

@ -24,9 +24,11 @@ RUN apk add --no-cache openssl1.1-compat
FROM base AS server-builder FROM base AS server-builder
RUN apk add --no-cache build-base libtool autoconf automake RUN apk add --no-cache build-base libtool autoconf automake
WORKDIR /app WORKDIR /app
COPY server/ ./server/
# Install npm packages, resulting in node_modules/. # Install npm packages, resulting in node_modules/.
COPY server/package*.json ./server/
RUN cd server && npm install RUN cd server && npm install
# Building the server should come after Prisma generation.
RUN cd server && npm run build
# TODO: Use pm2? # TODO: Use pm2?
@ -37,7 +39,8 @@ RUN apk add --no-cache python3
ENV NODE_ENV production ENV NODE_ENV production
WORKDIR /app WORKDIR /app
COPY --from=server-builder /app/server/node_modules ./server/node_modules COPY --from=server-builder /app/server/node_modules ./server/node_modules
COPY server/ ./server/ COPY --from=server-builder /app/server/dist ./server/dist
COPY --from=server-builder /app/server/package*.json ./server/
COPY db/ ./db/ COPY db/ ./db/
EXPOSE ${PORT} EXPOSE ${PORT}
WORKDIR /app/server WORKDIR /app/server

View File

@ -44,7 +44,7 @@
"postinstall": "patch-package", "postinstall": "patch-package",
"standard": "standard", "standard": "standard",
"start": "NODE_PATH=dist node -r dotenv/config dist/server.js", "start": "NODE_PATH=dist node -r dotenv/config dist/server.js",
"start-production": "NODE_ENV=production npm run build-and-start", "start-production": "NODE_ENV=production npm run start",
"watch": "nodemon --exec 'npm run build-and-start || exit 1'" "watch": "nodemon --exec 'npm run build-and-start || exit 1'"
}, },
"type": "module", "type": "module",

View File

@ -11,7 +11,7 @@
"file", "file",
"Dockerfile" "Dockerfile"
], ],
"e5332a9cfefb7af077ef2eba70e9deb90693997ae4cd2e0256bbe2a2346c465d" "1b945bc99904a58b69338a9154311260598e3f3a2d5d13adccfe44ab07e50076"
], ],
[ [
[ [
@ -53,7 +53,7 @@
"file", "file",
"server/package.json" "server/package.json"
], ],
"d5df9887d57283d8c3dfff1613affa4c06f26f2c9ab60feeef0d79773bb519e4" "13cf8e048fa468ad55beb420658ee909fc387dd375f16aa9f4186f7ef8139989"
], ],
[ [
[ [

View File

@ -24,11 +24,13 @@ RUN apk add --no-cache openssl1.1-compat
FROM base AS server-builder FROM base AS server-builder
RUN apk add --no-cache build-base libtool autoconf automake RUN apk add --no-cache build-base libtool autoconf automake
WORKDIR /app WORKDIR /app
COPY server/ ./server/
# Install npm packages, resulting in node_modules/. # Install npm packages, resulting in node_modules/.
COPY server/package*.json ./server/
RUN cd server && npm install RUN cd server && npm install
COPY db/schema.prisma ./db/ COPY db/schema.prisma ./db/
RUN cd server && PRISMA_CLIENT_OUTPUT_DIR=../server/node_modules/.prisma/client/ npx prisma generate --schema='../db/schema.prisma' RUN cd server && PRISMA_CLIENT_OUTPUT_DIR=../server/node_modules/.prisma/client/ npx prisma generate --schema='../db/schema.prisma'
# Building the server should come after Prisma generation.
RUN cd server && npm run build
# TODO: Use pm2? # TODO: Use pm2?
@ -39,7 +41,8 @@ RUN apk add --no-cache python3
ENV NODE_ENV production ENV NODE_ENV production
WORKDIR /app WORKDIR /app
COPY --from=server-builder /app/server/node_modules ./server/node_modules COPY --from=server-builder /app/server/node_modules ./server/node_modules
COPY server/ ./server/ COPY --from=server-builder /app/server/dist ./server/dist
COPY --from=server-builder /app/server/package*.json ./server/
COPY db/ ./db/ COPY db/ ./db/
EXPOSE ${PORT} EXPOSE ${PORT}
WORKDIR /app/server WORKDIR /app/server

View File

@ -43,7 +43,7 @@
"postinstall": "patch-package", "postinstall": "patch-package",
"standard": "standard", "standard": "standard",
"start": "NODE_PATH=dist node -r dotenv/config dist/server.js", "start": "NODE_PATH=dist node -r dotenv/config dist/server.js",
"start-production": "npm run db-migrate-prod && NODE_ENV=production npm run build-and-start", "start-production": "npm run db-migrate-prod && NODE_ENV=production npm run start",
"watch": "nodemon --exec 'npm run build-and-start || exit 1'" "watch": "nodemon --exec 'npm run build-and-start || exit 1'"
}, },
"type": "module", "type": "module",

View File

@ -29,7 +29,6 @@ import Wasp.Generator.DbGenerator.Common
import Wasp.Generator.FileDraft (FileDraft (..), createTemplateFileDraft) import Wasp.Generator.FileDraft (FileDraft (..), createTemplateFileDraft)
import qualified Wasp.Generator.FileDraft.TemplateFileDraft as TmplFD import qualified Wasp.Generator.FileDraft.TemplateFileDraft as TmplFD
import Wasp.Generator.Monad (Generator, GeneratorError, runGenerator) import Wasp.Generator.Monad (Generator, GeneratorError, runGenerator)
import Wasp.Generator.ServerGenerator (areServerPatchesUsed)
import Wasp.Generator.Templates (TemplatesDir, compileAndRenderTemplate) import Wasp.Generator.Templates (TemplatesDir, compileAndRenderTemplate)
import qualified Wasp.SemanticVersion as SV import qualified Wasp.SemanticVersion as SV
import Wasp.Util (getEnvVarDefinition) import Wasp.Util (getEnvVarDefinition)
@ -40,7 +39,6 @@ genDockerFiles spec = sequence [genDockerfile spec, genDockerignore spec]
-- TODO: Inject paths to server and db files/dirs, right now they are hardcoded in the templates. -- TODO: Inject paths to server and db files/dirs, right now they are hardcoded in the templates.
genDockerfile :: AppSpec -> Generator FileDraft genDockerfile :: AppSpec -> Generator FileDraft
genDockerfile spec = do genDockerfile spec = do
usingServerPatches <- areServerPatchesUsed spec
let dbSchemaFileFromServerDir :: Path' (Rel ServerRootDir) (File PrismaDbSchema) = dbSchemaFileFromAppComponentDir let dbSchemaFileFromServerDir :: Path' (Rel ServerRootDir) (File PrismaDbSchema) = dbSchemaFileFromAppComponentDir
return $ return $
createTemplateFileDraft createTemplateFileDraft
@ -52,7 +50,6 @@ genDockerfile spec = do
"serverPrismaClientOutputDirEnv" .= getEnvVarDefinition serverPrismaClientOutputDirEnv, "serverPrismaClientOutputDirEnv" .= getEnvVarDefinition serverPrismaClientOutputDirEnv,
"dbSchemaFileFromServerDir" .= SP.fromRelFile dbSchemaFileFromServerDir, "dbSchemaFileFromServerDir" .= SP.fromRelFile dbSchemaFileFromServerDir,
"nodeMajorVersion" .= show (SV.major latestMajorNodeVersion), "nodeMajorVersion" .= show (SV.major latestMajorNodeVersion),
"usingServerPatches" .= usingServerPatches,
"userDockerfile" .= fromMaybe "" (AS.userDockerfileContents spec) "userDockerfile" .= fromMaybe "" (AS.userDockerfileContents spec)
] ]
) )

View File

@ -107,7 +107,7 @@ genPackageJson spec waspDependencies = do
"nodeVersionRange" .= show nodeVersionRange, "nodeVersionRange" .= show nodeVersionRange,
"startProductionScript" "startProductionScript"
.= ( (if hasEntities then "npm run db-migrate-prod && " else "") .= ( (if hasEntities then "npm run db-migrate-prod && " else "")
++ "NODE_ENV=production npm run build-and-start" ++ "NODE_ENV=production npm run start"
), ),
"overrides" .= getPackageJsonOverrides "overrides" .= getPackageJsonOverrides
] ]