Skip to content

Commit

Permalink
fix: resolve build issues with prisma
Browse files Browse the repository at this point in the history
issues caused by bundling and needing to run prisma generate in the final containers. Added fixes for when a prisma folder is detected.
  • Loading branch information
jyecusch committed May 19, 2023
1 parent 7128d55 commit 89eb164
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 21 deletions.
39 changes: 28 additions & 11 deletions pkg/runtime/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ func TestGenerate(t *testing.T) {
{
name: "ts",
handler: "functions/list.ts",
wantFwriter: `FROM node:alpine as build
wantFwriter: `# syntax=docker/dockerfile:1
FROM node:alpine as build
ARG HANDLER
WORKDIR /usr/app
# Python and make are required by certain native package build processes in NPM packages.
ENV PYTHONUNBUFFERED=1
RUN apk add --update --no-cache python3 make g++ && ln -sf python3 /usr/bin/python
Expand All @@ -48,28 +51,35 @@ COPY . .
RUN yarn import || echo Lockfile already exists
RUN set -ex; yarn install --production --frozen-lockfile --cache-folder /tmp/.cache; rm -rf /tmp/.cache;
RUN set -ex; yarn install --frozen-lockfile --cache-folder /tmp/.cache; rm -rf /tmp/.cache;
RUN test -f tsconfig.json || echo "{\"compilerOptions\":{\"esModuleInterop\":true,\"target\":\"es2015\",\"moduleResolution\":\"node\"}}" > tsconfig.json
RUN ncc build ${HANDLER} -m --v8-cache -o lib/
# make prisma external to bundle - https://github.com/prisma/prisma/issues/16901#issuecomment-1362940774 \
# TODO: remove when custom dockerfile support is available
RUN ncc build ${HANDLER} -o lib/ -e .prisma/client -e @prisma/client
FROM node:alpine as final
WORKDIR /usr/app
RUN apk update && \
apk add --no-cache ca-certificates && \
update-ca-certificates
COPY --from=build "package.json" "package.json"
COPY --from=build /usr/app/lib/ ./lib/
COPY --from=build "node_modules/" "node_modules/"
COPY . .
COPY --from=build lib/ /
RUN set -ex; \
yarn install --production --frozen-lockfile --cache-folder /tmp/.cache; \
rm -rf /tmp/.cache; \
# prisma fix for docker installs: https://github.com/prisma/docs/issues/4365
# TODO: remove when custom dockerfile support is available
test -d ./prisma && npx prisma generate || echo "";
# Copy any other non-ignored assets to be included
COPY . .
ENTRYPOINT ["node", "index.js"]`,
ENTRYPOINT ["node", "lib/index.js"]`,
},
{
name: "go",
Expand Down Expand Up @@ -133,7 +143,8 @@ ENTRYPOINT python $HANDLER
{
name: "js",
handler: "functions/list.js",
wantFwriter: `FROM "node:alpine"
wantFwriter: `# syntax=docker/dockerfile:1
FROM node:alpine
ARG HANDLER
ENV HANDLER=${HANDLER}
Expand All @@ -152,7 +163,13 @@ COPY . .
RUN yarn import || echo Lockfile already exists
RUN set -ex; yarn install --production --frozen-lockfile --cache-folder /tmp/.cache; rm -rf /tmp/.cache;
RUN \
set -ex; \
yarn install --production --frozen-lockfile --cache-folder /tmp/.cache; \
rm -rf /tmp/.cache \
# prisma fix for docker installs: https://github.com/prisma/docs/issues/4365
# TODO: remove when custom dockerfile support is available
test -d ./prisma && npx prisma generate || echo "";
ENTRYPOINT node $HANDLER
`,
Expand Down
11 changes: 9 additions & 2 deletions pkg/runtime/javascript.dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM "node:alpine"
# syntax=docker/dockerfile:1
FROM node:alpine

ARG HANDLER
ENV HANDLER=${HANDLER}
Expand All @@ -17,6 +18,12 @@ COPY . .

RUN yarn import || echo Lockfile already exists

RUN set -ex; yarn install --production --frozen-lockfile --cache-folder /tmp/.cache; rm -rf /tmp/.cache;
RUN \
set -ex; \
yarn install --production --frozen-lockfile --cache-folder /tmp/.cache; \
rm -rf /tmp/.cache \
# prisma fix for docker installs: https://github.com/prisma/docs/issues/4365
# TODO: remove when custom dockerfile support is available
test -d ./prisma && npx prisma generate || echo "";

ENTRYPOINT node $HANDLER
26 changes: 18 additions & 8 deletions pkg/runtime/typescript.dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# syntax=docker/dockerfile:1
FROM node:alpine as build

ARG HANDLER

WORKDIR /usr/app

# Python and make are required by certain native package build processes in NPM packages.
ENV PYTHONUNBUFFERED=1
RUN apk add --update --no-cache python3 make g++ && ln -sf python3 /usr/bin/python
Expand All @@ -14,25 +17,32 @@ COPY . .

RUN yarn import || echo Lockfile already exists

RUN set -ex; yarn install --production --frozen-lockfile --cache-folder /tmp/.cache; rm -rf /tmp/.cache;
RUN set -ex; yarn install --frozen-lockfile --cache-folder /tmp/.cache; rm -rf /tmp/.cache;

RUN test -f tsconfig.json || echo "{\"compilerOptions\":{\"esModuleInterop\":true,\"target\":\"es2015\",\"moduleResolution\":\"node\"}}" > tsconfig.json

RUN ncc build ${HANDLER} -m --v8-cache -o lib/
# make prisma external to bundle - https://github.com/prisma/prisma/issues/16901#issuecomment-1362940774 \
# TODO: remove when custom dockerfile support is available
RUN ncc build ${HANDLER} -o lib/ -e .prisma/client -e @prisma/client

FROM node:alpine as final

WORKDIR /usr/app

RUN apk update && \
apk add --no-cache ca-certificates && \
update-ca-certificates

COPY --from=build "package.json" "package.json"
COPY --from=build /usr/app/lib/ ./lib/

COPY --from=build "node_modules/" "node_modules/"
COPY . .

COPY --from=build lib/ /
RUN set -ex; \
yarn install --production --frozen-lockfile --cache-folder /tmp/.cache; \
rm -rf /tmp/.cache; \
# prisma fix for docker installs: https://github.com/prisma/docs/issues/4365
# TODO: remove when custom dockerfile support is available
test -d ./prisma && npx prisma generate || echo "";

# Copy any other non-ignored assets to be included
COPY . .

ENTRYPOINT ["node", "index.js"]
ENTRYPOINT ["node", "lib/index.js"]

0 comments on commit 89eb164

Please sign in to comment.