如何将 Docker Compose 文件与 Next.js、Prisma 和 SQLite 结合使用

问题描述 投票:0回答:1

我有一个接下来的 14 个应用程序,它使用 Prisma 作为 ORM,并在本地使用 Sqlite 数据库进行开发测试。我不想为 MySQL 或 Mongo 或 Postgres 等使用图像...

在尝试使用

docker compose
时,我在运行容器以及让项目显示在端口 3000 上时遇到问题。

具体错误:

Server Error
Error: @prisma/client did not initialize yet. Please run "prisma generate" and try to import it again.
In case this error is unexpected for you, please report it in https://github.com/prisma/prisma/issues

This error happened while generating the page. Any console logs will be displayed in the terminal window.
Source
src/db/index.ts (3:22) @ eval

  1 | import { PrismaClient } from "@prisma/client";
  2 |
> 3 | export const prisma = new PrismaClient();

Dockerfile

# set the base image to create the image for react app
FROM node:20-alpine

# create a user with permissions to run the app
# -S -> create a system user
# -G -> add the user to a group
# This is done to avoid running the app as root
# If the app is run as root, any vulnerability in the app can be exploited to gain access to the host system
# It's a good practice to run the app as a non-root user
RUN addgroup app && adduser -S -G app app

# set the user to run the app
USER app

# set the working directory to /app
WORKDIR /app

# copy package.json and package-lock.json to the working directory
# This is done before copying the rest of the files to take advantage of Docker’s cache
# If the package.json and package-lock.json files haven’t changed, Docker will use the cached dependencies
COPY package*.json ./
COPY ./prisma prisma

# sometimes the ownership of the files in the working directory is changed to root
# and thus the app can't access the files and throws an error -> EACCES: permission denied
# to avoid this, change the ownership of the files to the root user
USER root

# change the ownership of the /app directory to the app user
# chown -R <user>:<group> <directory>
# chown command changes the user and/or group ownership of for given file.
RUN chown -R app:app .

# change the user back to the app user
USER app

# init prisma
RUN npx prisma generate --schema=./prisma/schema.prisma

# install dependencies
RUN npm install

# copy the rest of the files to the working directory
COPY . .

# expose port 3000 to tell Docker that the container listens on the specified network ports at runtime
EXPOSE 3000

# command to run the app
CMD npm run dev

Compose.yaml

version: "3.9"

services:
  web:
    build:
      context: .
    environment:
      DATABASE_URL: file:./prisma/dev.db
    ports:
      - 3000:3000
    volumes:
      - /app/prisma
      - .:/app
      - /app/node_modules

我的 prisma 东西位于 root-dir/prisma 中。在那里我有:

prisma/dev.db
prisma/schema.prisma
prisma/migrations/20231227173804_intial_migration

我尝试运行我的 docker 映像的无缓存重新构建,并且还尝试了带有或不带有特定

prisma generate
标志的
--schema

如何使用

docker compose up
来运行我的 docker 容器?

谢谢。

docker next.js docker-compose prisma
1个回答
0
投票

我想通了。我遇到了权限问题,导致 prisma、dev.db 文件/目录由

root
而不是我在
app
中指定的
Dockerfile
用户拥有。我还需要重新运行 docker compose 命令并排除缓存,这样它就不会使用以前的配置。

更新:

Dockerfile

# set the base image to create the image for react app
FROM node:20-alpine

# create a user with permissions to run the app
# -S -> create a system user
# -G -> add the user to a group
# This is done to avoid running the app as root
# If the app is run as root, any vulnerability in the app can be exploited to gain access to the host system
# It's a good practice to run the app as a non-root user
RUN addgroup app && adduser -S -G app app

# set the user to run the app
USER app

# set the working directory to /app
WORKDIR /app

# copy package.json and package-lock.json to the working directory
# This is done before copying the rest of the files to take advantage of Docker’s cache
# If the package.json and package-lock.json files haven’t changed, Docker will use the cached dependencies
COPY package*.json ./
COPY ./prisma prisma
COPY ./dev.db /app/dev.db

# sometimes the ownership of the files in the working directory is changed to root
# and thus the app can't access the files and throws an error -> EACCES: permission denied
# to avoid this, change the ownership of the files to the root user
USER root

# Ensure write permissions for the app directory and its contents
RUN chmod -R u+w .

# change the ownership of the /app directory to the app user
# chown -R <user>:<group> <directory>
# chown command changes the user and/or group ownership of for given file.
RUN chown -R app:app .

# As root, change the ownership of dev.db to app user
RUN chown app:app dev.db

# change the user back to the app user
USER app

# init prisma
RUN npx prisma generate --schema=./prisma/schema.prisma

# install dependencies
RUN npm install

# copy the rest of the files to the working directory
COPY . .

# expose port 3000 to tell Docker that the container listens on the specified network ports at runtime
EXPOSE 3000

# command to run the app
CMD npm run dev

compose.yaml

version: "3.9"

services:
  web:
    build:
      context: .
    environment:
      DATABASE_URL: file:./dev.db
    ports:
      - 3000:3000
    volumes:
      - /app/prisma
      - ./dev.db:/app/dev.db:rw # Mount the database file with correct ownership
      - .:/app
      - /app/node_modules

命令:

  1. docker-compose build --no-cache
  2. docker compose up

此后,我可以在 Docker 容器内使用 prisma orm 写入我的 sqlite 数据库。

© www.soinside.com 2019 - 2024. All rights reserved.