Skip to content

Commit

Permalink
Build server during Docker build, not when starting app (#1008)
Browse files Browse the repository at this point in the history
  • Loading branch information
shayneczyzewski authored Feb 17, 2023
1 parent 868362d commit bfbb57e
Show file tree
Hide file tree
Showing 16 changed files with 41 additions and 29 deletions.
3 changes: 3 additions & 0 deletions waspc/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog
## 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
- Fixes a file lock error that kills CLI when changing entities with `wasp start` running on newer Macs.

Expand Down
10 changes: 5 additions & 5 deletions waspc/data/Generator/templates/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@ RUN apk add --no-cache openssl1.1-compat
FROM base AS server-builder
RUN apk add --no-cache build-base libtool autoconf automake
WORKDIR /app
COPY server/ ./server/
# Install npm packages, resulting in node_modules/.
{=# usingServerPatches =}
COPY server/patches ./server/patches
{=/ usingServerPatches =}
COPY server/package*.json ./server/
RUN cd server && npm install
{=# usingPrisma =}
COPY db/schema.prisma ./db/
RUN cd server && {= serverPrismaClientOutputDirEnv =} npx prisma generate --schema='{= dbSchemaFileFromServerDir =}'
{=/ usingPrisma =}
# Building the server should come after Prisma generation.
RUN cd server && npm run build


# TODO: Use pm2?
Expand All @@ -45,7 +44,8 @@ RUN apk add --no-cache python3
ENV NODE_ENV production
WORKDIR /app
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/
EXPOSE ${PORT}
WORKDIR /app/server
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions waspc/src/Wasp/Generator/DockerGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import Wasp.Generator.DbGenerator.Common
import Wasp.Generator.FileDraft (FileDraft (..), createTemplateFileDraft)
import qualified Wasp.Generator.FileDraft.TemplateFileDraft as TmplFD
import Wasp.Generator.Monad (Generator, GeneratorError, runGenerator)
import Wasp.Generator.ServerGenerator (areServerPatchesUsed)
import Wasp.Generator.Templates (TemplatesDir, compileAndRenderTemplate)
import qualified Wasp.SemanticVersion as SV
import Wasp.Util (getEnvVarDefinition)
Expand All @@ -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.
genDockerfile :: AppSpec -> Generator FileDraft
genDockerfile spec = do
usingServerPatches <- areServerPatchesUsed spec
let dbSchemaFileFromServerDir :: Path' (Rel ServerRootDir) (File PrismaDbSchema) = dbSchemaFileFromAppComponentDir
return $
createTemplateFileDraft
Expand All @@ -52,7 +50,6 @@ genDockerfile spec = do
"serverPrismaClientOutputDirEnv" .= getEnvVarDefinition serverPrismaClientOutputDirEnv,
"dbSchemaFileFromServerDir" .= SP.fromRelFile dbSchemaFileFromServerDir,
"nodeMajorVersion" .= show (SV.major latestMajorNodeVersion),
"usingServerPatches" .= usingServerPatches,
"userDockerfile" .= fromMaybe "" (AS.userDockerfileContents spec)
]
)
Expand Down
2 changes: 1 addition & 1 deletion waspc/src/Wasp/Generator/ServerGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ genPackageJson spec waspDependencies = do
"nodeVersionRange" .= show nodeVersionRange,
"startProductionScript"
.= ( (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
]
Expand Down

0 comments on commit bfbb57e

Please sign in to comment.