Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build TS in Docker, not when starting app #1008

Merged
merged 10 commits into from
Feb 17, 2023

Conversation

shayneczyzewski
Copy link
Contributor

@shayneczyzewski shayneczyzewski commented Feb 15, 2023

Description

This PR moves the TS build process (npx tsc) into the Dockerfile, thus happening at build time and not when the app starts. This should reduce the memory requirements for running the app.

In the future, we will fix how production assets are built and shipped here: #897

Select what type of change this PR introduces:

  1. Just code/docs improvement (no functional change).
  2. Bug fix (non-breaking change which fixes an issue).
  3. New feature (non-breaking change which adds functionality).
  4. Breaking change (fix or feature that would cause existing functionality to not work as expected).

Update Waspc ChangeLog and version if needed

If you did a bug fix, new feature, or breaking change, that affects waspc, make sure you satisfy the following:

  1. I updated ChangeLog.md with description of the change this PR introduces.
  2. I bumped waspc version in waspc.cabal to reflect changes I introduced, with regards to the version of the latest wasp release, if the bump was needed.

@@ -47,6 +47,7 @@ WORKDIR /app
COPY --from=server-builder /app/server/node_modules ./server/node_modules
COPY server/ ./server/
COPY db/ ./db/
RUN cd server && npm run build
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't run this in server-builder since I need the source code, which is copied over here. I didn't want to fundamentally restructure this Dockerfile, and instead do the smallest change that will work, since we will rethink this process in the near future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite understand this!
So we need to build a TS project. Why not build it in server-builder? I guess you would need to copy some extra files then, but I guess that wouldn't be too hard? I don't think this is too bad either, it is probably a small impact, but I am curious.

Copy link
Contributor

@infomiho infomiho Feb 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an added benefit if we did it in the server-builder step, we could copy only the node_modules and dist folder to the next step (avoiding copying over the src and everything else).

But we should still separate the copying of src and package*.json files to keep the layer caching benefits.

FROM base AS server-builder
...
COPY server/package*.json ./server/
RUN cd server && npm install
...
COPY server/ ./server/
RUN cd server && npm run build

FROM base AS server-production
...
COPY --from=server-builder /app/server/node_modules ./server/node_modules
COPY --from=server-builder /app/server/dist ./server/dist

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok ok, you all convinced me, I will just refactor it a bit to do all the building where it probably should. The 1 line fix was just so appealing though :D

@shayneczyzewski shayneczyzewski changed the title Draft: Build TS in Docker, not when starting app Build TS in Docker, not when starting app Feb 15, 2023
@shayneczyzewski shayneczyzewski marked this pull request as ready for review February 15, 2023 16:29
Copy link
Member

@Martinsos Martinsos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved but also left a small comment!

@@ -47,6 +47,7 @@ WORKDIR /app
COPY --from=server-builder /app/server/node_modules ./server/node_modules
COPY server/ ./server/
COPY db/ ./db/
RUN cd server && npm run build
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite understand this!
So we need to build a TS project. Why not build it in server-builder? I guess you would need to copy some extra files then, but I guess that wouldn't be too hard? I don't think this is too bad either, it is probably a small impact, but I am curious.

Copy link
Contributor Author

@shayneczyzewski shayneczyzewski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok @Martinsos and @infomiho, made your updates (plus one small other cleanup). Feel free to do a final check if you'd like. Thanks!

Comment on lines -29 to -32
{=# usingServerPatches =}
COPY server/patches ./server/patches
{=/ usingServerPatches =}
COPY server/package*.json ./server/
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We no longer have to selectively, or conditionally in the case of patches, copy a subset of the server dir over. Just copy it all, since we need the source later anyways for TS compilation.

Comment on lines +47 to +48
COPY --from=server-builder /app/server/dist ./server/dist
COPY --from=server-builder /app/server/package*.json ./server/
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to the dist dir, we also need the package.json files to run the npm run command.

RUN cd server && npm install
{=# usingPrisma =}
COPY db/schema.prisma ./db/
RUN cd server && {= serverPrismaClientOutputDirEnv =} npx prisma generate --schema='{= dbSchemaFileFromServerDir =}'
{=/ usingPrisma =}
# Compile TS (depends on Prisma, if in use)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: This comment feels a bit off

The fact that we are compiling TS here, it's not really important, we are building the server in general. If I understood the comment correctly, you wanted to convey the idea "we need to put this AFTER the prisma generate bit". Maybe write it like that 🤷

Copy link
Contributor Author

@shayneczyzewski shayneczyzewski Feb 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I can drop the compile/build note and just say we need to do this after Prisma entities were generated (which was the main point, as you noted). 👍🏻 Will update comment shortly

Copy link
Contributor

@infomiho infomiho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, I'll test it out locally as well.

Copy link
Member

@Martinsos Martinsos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Nice job!

@shayneczyzewski shayneczyzewski merged commit bfbb57e into main Feb 17, 2023
@shayneczyzewski shayneczyzewski deleted the shayne-build-ts-in-dockerfile branch February 17, 2023 18:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants