Issue
This Content is from Stack Overflow. Question asked by Jed Peel
Summary
I am running a Sveltkit (JS Framework) application with Prisma (ORM) which connects to PlanetScale (MySQL cloud database) without issue. When I run it in docker the application installs and runs but cannot connect to PlanetScale. I have found a PlanetScale Docker Image but I am very new to Docker so how would I connect this to my current Docker or how would I allow Docker to connect to PlanetScale?
Error
Failed to get with error Invalid
prisma.category.findMany()
invocation:
Can’t reach database server ataws-eu-west-2.connect.psdb.cloud
:3306
Please make sure your database server is running ataws-eu-west-2.connect.psdb.cloud
:3306
.
I have confirmed the docker can connect to the internet but just has trouble connecting to PlanetScale database.
Code
MySQL Connection URI
Password is starred out but is correct in my .env
file
DATABASE_URL='mysql://7m9rl9ecwydgs7d2oobp:********@aws-eu-west-2.connect.psdb.cloud/blog-database?sslaccept=strict'
Prisma
schema.prisma
generator client {
provider = "prisma-client-js"
previewFeatures = ["referentialIntegrity"]
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
referentialIntegrity = "prisma"
}
prisma.ts
Helper functions
import { PrismaClient, type PrismaPromise, type Category } from '@prisma/client'
const prisma = new PrismaClient()
export function findAllCategory(): Promise<Array<Category>> {
return prisma.category.findMany()
}
Docker
DockerFile
FROM node:18-alpine3.15 AS builder
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npx prisma generate
RUN npm run build && npm prune --production
FROM node:18-alpine3.15
USER node:node
WORKDIR /app
COPY --from=builder --chown=node:node /app/build ./build
COPY --from=builder --chown=node:node /app/node_modules ./node_modules
COPY --chown=node:node prisma .
COPY --chown=node:node package.json .
COPY --chown=node:node .env .
ENV PORT 5050
EXPOSE 5050
CMD ["node", "build"]
Docker Commands
docker build . -t sveltekit:alpine
docker run -d -p 5050:5050 --name sveltekit-app sveltekit:alpine
Docker runs without issue but the error is displayed when going to http://localhost:5050
Solution
After a long amount of searching I found a github issues page which contained some solutions.
Updating my docker file to node:16.15-alpine (the exact same node version as on my Ubuntu subsystem) and combining this with a previously attempted fix of adding ‘connection_timeout=300’ to my Database URL solved the issue with some caveats.
Dockerfile
FROM node:16.15-alpine AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npx prisma generate
RUN npm run build
FROM node:16.15-alpine
WORKDIR /app
COPY --from=build /app/node_modules ./node_modules
COPY --from=build /app/build ./build
COPY package.json .
COPY prisma ./prisma
COPY .env /app/.env
EXPOSE 3000
ENTRYPOINT ["node", "build"]
.env
DATABASE_URL='mysql://7m9rl9ecwydgs7d2oobp:pscale_pw_6KmIc1RUngdwn3sURKzxfDe3Oo7GM0NFeoATFxxNSAG@aws-eu-west-2.connect.psdb.cloud/blog-database?sslaccept=strict&connect_timeout=300'
Caveats
Prisma fails on initial load after starting docker but refreshing resolves this. Reloading some pages for the first time also causes this issue but after this page load is reliable even when switching to incognito to remove cache.
This is a sufficient fix for my solution but I welcome further answers if there is a more reliable solution.
This Question was asked in StackOverflow by Jed Peel and Answered by Jed Peel It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.