dockerizing my nodejs app giving error, working perfectly on localhost:
{"level":"error","message":"Failed to connect to database: \u001b[1;91merror\u001b[0m: \u001b[1mError validating datasource db: You must provide a nonempty URL\u001b[0m\n \u001b[1;94m-->\u001b[0m \u001b[4mschema.prisma:11\u001b[0m\n\u001b[1;94m | \u001b[0m\n\u001b[1;94m10 | \u001b[0m provider = \"mysql\"\n\u001b[1;94m11 | \u001b[0m url = \u001b[1;91menv(\"DATABASE_URL_WRITE\")\u001b[0m\n\u001b[1;94m | \u001b[0m\n\nValidation Error Count: 1","metadata":{"clientVersion":"6.11.1","errorCode":"P1012","name":"PrismaClientInitializationError","stack":"PrismaClientInitializationError: \u001b[1;91merror\u001b[0m: \u001b[1mError validating datasource db: You must provide a nonempty (/app/node_modules/@prisma/client/runtime/library.js:112:2770)\n at async exports.default (/app/dist/src/api/loaders/mysql.js:32:9)\n at async exports.default (/app/dist/src/api/loaders/index.js:14:41)\n an at async startServer (/app/dist/src/app.js:13:5)"}}
dockerfile:
FROM node:24-alpine AS development
WORKDIR /app
RUN mkdir -p /app/dist && chmod -R 777 /app
COPY package*.json ./
COPY prisma ./prisma/
RUN npm install
COPY . .
RUN npm run build
FROM node:24-alpine AS production
ENV NODE_ENV=production
ENV HUSKY_SKIP_INSTALL=true
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY --from=development /app/dist ./dist
COPY --from=development /app/prisma ./prisma
ENV DATABASE_URL_WRITE="mysql://admin:vaP@buddy-pawance-1.c3iumg2ai2azonaws.com:3306/buvs?ssl=true"
RUN npm run db:generate
RUN addgroup -g 1001 -S nodegroup && adduser -S nodeuser -u 1001 -G nodegroup
USER nodeuser
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=10s --start-period=10s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:3000/health || exit 1
CMD ["node", "dist/src/app.js"]3 Replies
Skipping coffee for instant knowledge! Let's brew that AI answer now.
Your error message indicates that Prisma cannot find a non-empty value for the environment variable
Remove the quotes from your
DATABASE_URL_WRITE when running inside your Docker container:
This usually happens because the environment variable is not set or is set incorrectly at build or runtime. In your Dockerfile, you have:
Issues:
1. The environment variable is set during the build stage, but Prisma expects it to be available at runtime, especially if you run prisma generate or any Prisma CLI commands.
2. There is a typo: RUN RUN npm run db:generate should be RUN npm run db:generate.
3. Prisma expects environment variables to be available when the app starts, not just at build time.
How to fix:
- Make sure DATABASE_URL_WRITE is available both during the build (for prisma generate) and at runtime (when your app starts).
- Do not use quotes around the value in the Dockerfile's ENV statement (i.e., use ENV DATABASE_URL_WRITE=mysql://... instead of ENV DATABASE_URL_WRITE="mysql://..."). Quoting can cause the variable to be read incorrectly by Prisma, as seen in several issues and discussions source.
Example fix:
Or, better, pass the environment variable at runtime using Docker's -e flag or in your docker-compose.yml file.
Summary:Remove the quotes from your
ENV line and ensure the variable is available at runtime. This should resolve the "You must provide a nonempty URL" error for Prisma in Docker source.
🤔 Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly.
In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.Hey!
The recommendation of droppingthe quotes from connection string didn't solve the issue?
Hi, Narul.
that didn't resolve the issue. It's still the same error.
If you need some additional info, let me know. This works perfectly without docker but when dockerizing then getting this error.
datasource db {
provider = "mysql"
url = "mysql://admin:vk0sSoaP@buws-database-cluster-instance-1.c3iux.eu-2.rds.amazonaws.com:3306/buvs?ssl=true"
}
I have also tried passing the db url directly without env and it's still the same error
Hi Narul/Prisma,
I am still stuck here. Hope you are are going to provide a solution to this issue, I am facing becuase of prisma.