Skip to content

Instantly share code, notes, and snippets.

@TheRealFlyingCoder
Last active January 10, 2023 13:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheRealFlyingCoder/5340b192b8ef091a4203f89bf5497630 to your computer and use it in GitHub Desktop.
Save TheRealFlyingCoder/5340b192b8ef091a4203f89bf5497630 to your computer and use it in GitHub Desktop.
Keystone.js with Docker and Fly.io
So once you figure out all the caveats, running Keystone on Docker and in Fly.io is relatively simple.
package.json:
You need --fix on your setup script for Docker to auto update the schemas
"setup": "keystone postinstall --fix"
NOTES FOR DOCKER:
1. I use Yarn V3 in a monorepo hence the corepack lines and project structure
2. Keystone can't use PnP / Hoisting atm which is where the node linker comes in .yarnrc.yml
3. You have to install the workspace-tools plugin for yarn v3 to use the production install
4. Multi-part dockerfiles reduce overall size and deployment times
5. If you have ENV variable checks that throw anywhere in your code you
have to set them blank at the top as keystone runs your code during setup and build (for no reason)
nodeLinker: node-modules
plugins:
- path: .yarn/plugins/@yarnpkg/plugin-workspace-tools.cjs
spec: "@yarnpkg/plugin-workspace-tools"
FROM node:18-bullseye-slim as setup
ENV PORT 8080
ENV NODE_ENV production
RUN mkdir /app
WORKDIR /app
RUN corepack enable
RUN corepack prepare yarn@3.3.1 --activate
# All Dependencies
FROM setup as base
COPY apps/keystone/package.json package.json
COPY apps/keystone/yarn.lock yarn.lock
COPY apps/keystone/.yarnrc.yml .yarnrc.yml
COPY apps/keystone/.yarn .yarn
FROM base as deps
RUN yarn workspaces focus --all
# Build the app
FROM base as build
COPY --from=deps /app/node_modules node_modules
COPY apps/keystone/keystone.ts keystone.ts
COPY apps/keystone/configuration configuration
COPY apps/keystone/schemas schemas
RUN yarn run setup
RUN yarn run build
# Production Dependencies
FROM base as production-deps
COPY --from=build /app/node_modules node_modules
RUN yarn workspaces focus --production
FROM setup as production
# Install openssl for Prisma
RUN apt-get update && apt-get install -y openssl
COPY --from=build /app/.keystone .keystone
COPY --from=build /app/schema.graphql /app/schema.prisma ./
COPY --from=production-deps /app/node_modules node_modules
COPY /apps/keystone/migrations migrations
COPY /apps/keystone/package.json package.json
#Cleanup a tiny bit
RUN rm -rf /.keystone/admin/.next/cache
CMD ["yarn", "run", "start"]
//ALMOST EVERYTHING HERE IS OOTB WITH FLY INIT
//Only update is that you need to run the prisma migrate deploy method directly
//Keystone has a CLI method but it doesnt work, and essentially just runs the prisma tool
[deploy]
release_command = "npx prisma migrate deploy"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment