Deployed app on fly.io, tables created, but no data seed applied

The deployment was good,

my fly.toml

# fly.toml file generated for avo-fly on 2023-02-07T20:59:50-06:00

app = "avo-fly"
kill_signal = "SIGINT"
kill_timeout = 5
processes = []

[deploy]
  release_command = "npx prisma migrate deploy"

[env]
  PORT = "8080"

[experimental]
  auto_rollback = true

[[services]]
  http_checks = []
  internal_port = 8080
  processes = ["app"]
  protocol = "tcp"
  script_checks = []
  [services.concurrency]
    hard_limit = 25
    soft_limit = 20
    type = "connections"

  [[services.ports]]
    force_https = true
    handlers = ["http"]
    port = 80

  [[services.ports]]
    handlers = ["tls", "http"]
    port = 443

  [[services.tcp_checks]]
    grace_period = "1s"
    interval = "15s"
    restart_limit = 0
    timeout = "2s"

package.json

{
  "private": true,
  "sideEffects": false,
  "scripts": {
    "build": "npm run build:css && remix build",
    "build:css": "tailwindcss -m -i ./styles/app.css -o app/styles/app.css",
    "deploy": "fly deploy --remote-only",
    "dev": "remix dev",
    "dev:css": "tailwindcss -w -i ./styles/app.css -o app/styles/app.css",
    "start": "remix-serve build",
    "setup": "prisma generate && prisma migrate deploy && prisma db seed",
    "typecheck": "tsc"
  },
  "prisma": {
    "seed": "node --require esbuild-register prisma/seed.ts"
  },
  "dependencies": {
    "@emotion/react": "^11.10.5",
    "@emotion/styled": "^11.10.5",
    "@heroicons/react": "^1.0.6",
    "@mui/material": "^5.11.8",
    "@prisma/client": "^4.9.0",
    "@remix-run/node": "^1.12.0",
    "@remix-run/react": "^1.12.0",
    "@remix-run/serve": "^1.12.0",
    "@tailwindcss/forms": "^0.5.3",
    "framer-motion": "^9.0.2",
    "i": "^0.3.7",
    "isbot": "^3.6.5",
    "npm": "^9.4.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "spin-delay": "^1.2.0",
    "uuidv4": "^6.2.13",
    "v4": "^0.0.1"
  },
  "devDependencies": {
    "@remix-run/dev": "^1.12.0",
    "@remix-run/eslint-config": "^1.12.0",
    "@types/react": "^18.0.25",
    "@types/react-dom": "^18.0.8",
    "concurrently": "^7.6.0",
    "esbuild-register": "^3.4.2",
    "eslint": "^8.27.0",
    "prisma": "^4.9.0",
    "tailwindcss": "^3.2.4",
    "typescript": "^4.8.4"
  },
  "engines": {
    "node": ">=14"
  }
}

Dockerfile

# base node image
FROM node:16-bullseye-slim as base

# Install openssl for Prisma
RUN apt-get update && apt-get install -y openssl

# Install all node_modules, including dev dependencies
FROM base as deps

RUN mkdir /app
WORKDIR /app

ADD package.json package-lock.json ./
RUN npm install --production=false

# Setup production node_modules
FROM base as production-deps

RUN mkdir /app
WORKDIR /app

COPY --from=deps /app/node_modules /app/node_modules
ADD package.json package-lock.json ./
RUN npm prune --production

# Build the app
FROM base as build

ENV NODE_ENV=production

RUN mkdir /app
WORKDIR /app

COPY --from=deps /app/node_modules /app/node_modules

# If we're using Prisma, uncomment to cache the prisma schema
ADD prisma .
RUN npx prisma generate


ADD . .
RUN npm run build

# Finally, build the production image with minimal footprint
FROM base

ENV NODE_ENV=production

RUN mkdir /app
WORKDIR /app

COPY --from=production-deps /app/node_modules /app/node_modules

# Uncomment if using Prisma
COPY --from=build /app/node_modules/.prisma /app/node_modules/.prisma

COPY --from=build /app/build /app/build
COPY --from=build /app/public /app/public
ADD . .

CMD ["npm", "run", "start"]

I dont know why I cant apply the seed inside fly.

Hi,

Does fly logs reveal any errors, or why itā€™s failing? Thatā€™s generally the first place to look. It will often say canā€™t do X or missing Y, or something that reveals what went wrong.

Not sure if you have used this already, but if not there is an official guide to deploying Remix on Fly which has some handy tips: GitHub - remix-run/blues-stack: The Remix Stack for deploying to Fly with PostgreSQL, authentication, testing, linting, formatting, etc.

it runs perfectly, the thing is that all the data executed by seed doesnt apply. i need to create the table values again manually, but the seed doesnt seems to work

Hi,

Ah, this seems to be a common problem e.g

Do any of the suggested solutions in this thread work? It seems like what worked for one guy didnā€™t for another, oddly! Maybe the one at the very bottom?

Hey there. I encountered the same problem with prisma when porting a node app over to fly. In my case it was related to prisma just being a dev dependency and getting pruned in the final build stage of the docker container.

I recommend transpiling the seeds in the build stage, including them in your docker and then running them with node.

My current setup looks like this:

Add an additional script for transpiling the seeds with tsc like this

    "seeds:build" : "tsc -p tsconfig.seed.json",

Add the tsconfig.seed.json to just transpile the seeds and all its dependencies:

{
  "$schema": "https://json.schemastore.org/tsconfig",

  "compilerOptions": {
    "lib": ["ES2021", "dom"],
    "module": "commonjs",
    "target": "ES2021",
    "skipLibCheck": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "resolveJsonModule": true,
    "outDir": "seeds"
  },
  "include": ["prisma/**/*"],
}

In your Dockerfile somewhere in the build stage add:

npm run seeds:build

And then copy them over in the production stage:

# Copy over the transpiled seed files for database init
COPY --from=build-stage /app/seeds /app/seeds

This has the neat side effect that you can now run this in the fly.toml as a deploy command like this: (In my case i still include prisma as a dev dependency but just use the client to reset everything and apply migrations - In a production deployment you probably would not need a reset and seed so this becomes obsolete there)

[deploy]
    release_command = "npx --no --quiet prisma migrate reset --force --skip-generate --skip-seed && npm run seeds:ci"
1 Like

On another note: It might be tempting to just change the prisma seed in the package.json but changing it there would mean that other developers, or your own local setup will be impacted by a change you made because of an infrastructure requirement (Getting seeds to run now means transpiling them first) which was another reason for me to go with the approach as described above.

error

 => CANCELED FROM docker.io/library/build-stage:latest                                                                        0.7s
 => => resolve docker.io/library/build-stage:latest                                                                           0.7s
 => [base 1/2] FROM docker.io/library/node:16-bullseye-slim@sha256:37c7828676c5a3e06ccf5d2a8e6da011ae4e9148abb68ecd1c5bfab91  0.0s
 => CACHED [base 2/2] RUN apt-get update && apt-get install -y openssl                                                        0.0s
 => CACHED [build 1/8] RUN mkdir /app                                                                                         0.0s
 => CACHED [build 2/8] WORKDIR /app                                                                                           0.0s
 => ERROR [build 3/8] RUN npm run seeds:build                                                                                 0.7s
 => CACHED [deps 1/4] RUN mkdir /app                                                                                          0.0s
 => CACHED [deps 2/4] WORKDIR /app                                                                                            0.0s
 => CACHED [deps 3/4] ADD package.json package-lock.json ./                                                                   0.0s
 => CANCELED [deps 4/4] RUN npm install --production=false                                                                    0.7s
------
 > [build 3/8] RUN npm run seeds:build:
#18 0.681 npm ERR! code ENOENT
#18 0.681 npm ERR! syscall open
#18 0.682 npm ERR! path /app/package.json
#18 0.682 npm ERR! errno -2
#18 0.687 npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json'
#18 0.687 npm ERR! enoent This is related to npm not being able to find a file.
#18 0.688 npm ERR! enoent
#18 0.690
#18 0.690 npm ERR! A complete log of this run can be found in:
#18 0.690 npm ERR!     /root/.npm/_logs/2023-02-08T19_49_53_970Z-debug-0.log
------
Error failed to fetch an image or build from source: error building: executor failed running [/bin/sh -c npm run seeds:build]: exit code: 254

my docker

# base node image
FROM node:16-bullseye-slim as base

# Install openssl for Prisma
RUN apt-get update && apt-get install -y openssl

# Install all node_modules, including dev dependencies
FROM base as deps

RUN mkdir /app
WORKDIR /app

ADD package.json package-lock.json ./
RUN npm install --production=false

# Setup production node_modules
FROM base as production-deps

RUN mkdir /app
WORKDIR /app

COPY --from=deps /app/node_modules /app/node_modules
ADD package.json package-lock.json ./
RUN npm prune --production

# Build the app
FROM base as build

ENV NODE_ENV=production

RUN mkdir /app
WORKDIR /app

RUN npm run seeds:build

COPY --from=deps /app/node_modules /app/node_modules

# If we're using Prisma, uncomment to cache the prisma schema
ADD prisma .
RUN npx prisma generate


ADD . .
RUN npm run build

# Finally, build the production image with minimal footprint
FROM base

ENV NODE_ENV=production

RUN mkdir /app
WORKDIR /app
# Copy over the transpiled seed files for database init
COPY --from=build-stage /app/seeds /app/seeds
COPY --from=production-deps /app/node_modules /app/node_modules

# Uncomment if using Prisma
COPY --from=build /app/node_modules/.prisma /app/node_modules/.prisma

COPY --from=build /app/build /app/build
COPY --from=build /app/public /app/public
ADD . .

CMD ["npm", "run", "start"]

solve that using COPY --from=build /app/seeds /app/seeds INSTEAD OF of COPY --from=build-stage /app/seeds /app/seeds

I believe you need the package.json in your final container. As far as i can tell its only copied over in the build stage. Also you just copied over the docker command referencing --from=build-stage which does not exist in your Dockerfile.

I can share my file which works pretty well and is optimized for size: (make sure to adjust it for your needs)

# -------- Build stage -------- #
FROM node:16-alpine3.16 as build-stage

RUN apk add --update --no-cache curl

WORKDIR /app

COPY package.json .
COPY package-lock.json .
RUN npm ci

COPY . .

RUN npm run build:production && npm run seeds:build && npm prune --production

# -------- Final stage -------- #
FROM node:16-alpine3.16

WORKDIR /app

# Copy over the transpiled seed files for database init
COPY --from=build-stage /app/seeds /app/seeds

# Copy over prisma and database related files
COPY --from=build-stage /app/prisma /app/prisma

COPY --from=build-stage /app/package.json /app

COPY --from=build-stage /app/dist /app

# And all of its dependencies
COPY --from=build-stage /app/node_modules /app/node_modules

CMD ["node", "main"]

I manage to deploy successfully, and the db reset was good, but didnā€™t apply any seed, the info still not showing.

It might help to flyctl ssh console into your app and execute the seed manually and see if any errors pop up in stdout. I have to run a pretty large seed and only noticed later that my app was OOM.

needed to convert the seed.js and execute it, thats the only way i find out it works