Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 43 additions & 7 deletions apps/docs/content/docs/guides/deployment/docker.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ Now, create a `prisma.config.ts` file in the root of your project:

```typescript title="prisma.config.ts"
import "dotenv/config";
import { defineConfig, env } from "prisma/config";
import { defineConfig } from "prisma/config";

export default defineConfig({
schema: "prisma/schema.prisma",
migrations: {
path: "prisma/migrations",
},
datasource: {
url: env("DATABASE_URL"),
url: process.env["DATABASE_URL"],
},
});
```
Expand Down Expand Up @@ -198,10 +198,27 @@ Update the `package.json` scripts to include commands for running the server and
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1", // [!code --]
"dev": "node index.js", // [!code ++]
"db:deploy": "npx prisma migrate deploy && npx prisma generate" // [!code ++]
"db:generate": "npx prisma generate", // [!code ++]
"db:deploy": "npx prisma migrate deploy" // [!code ++]
}
```

Add the generated Prisma Client output to `.gitignore` and `.dockerignore`. The client is generated during the Docker image build, so it does not need to be committed or sent as part of the Docker build context:

```text title=".gitignore"
node_modules
generated
.env
.env.*
```

```text title=".dockerignore"
node_modules
generated
.env
.env.*
```

Now that the application is set up, let's move on to configuring a PostgreSQL database using Docker Compose.

## 2. Set up a PostgreSQL database with Docker Compose
Expand Down Expand Up @@ -337,9 +354,14 @@ COPY package.json package-lock.json ./

RUN npm ci

COPY prisma ./prisma
COPY prisma.config.ts ./

RUN npm run db:generate

COPY . .

CMD ["sh", "-c", "npm run db:deploy && npm run dev"]
CMD ["npm", "run", "dev"]
```

:::note
Expand Down Expand Up @@ -372,11 +394,22 @@ COPY package.json package-lock.json ./

RUN npm ci

COPY prisma ./prisma
COPY prisma.config.ts ./

RUN npm run db:generate

COPY . .

CMD ["sh", "-c", "npm run db:deploy && npm run dev"]
CMD ["npm", "run", "dev"]
```

:::tip[Production migrations]

Generate Prisma Client during the Docker image build, but run `prisma migrate deploy` separately from the long-running application container. In production, this is usually a CI/CD release step or a one-off job that runs before the new application version starts. Avoid running `migrate deploy` in the Dockerfile build because the build should not need access to your production database.

:::

Related Docker images:

- `node:lts-slim`
Expand Down Expand Up @@ -455,10 +488,13 @@ DATABASE_URL="postgresql://postgres:prisma@postgres_db:5432/postgres?schema=publ

### 3.4. Build and run the application

With everything set up, it's time to build and run the app using Docker Compose. Run the following command:
With everything set up, build the app image, start PostgreSQL, apply pending migrations once, and then start the app:

```bash
docker compose -f docker-compose.yml up --build -d
docker compose -f docker-compose.yml build
docker compose -f docker-compose.yml up -d postgres_db
docker compose -f docker-compose.yml run --rm server npm run db:deploy
docker compose -f docker-compose.yml up -d server
```

Visit `http://localhost:3000` to see your app running with the message:
Expand Down
8 changes: 5 additions & 3 deletions apps/docs/content/docs/guides/deployment/turborepo.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,22 @@ The `prisma.config.ts` file created in the `packages/database` directory should

```typescript title="packages/database/prisma.config.ts"
import "dotenv/config";
import { defineConfig, env } from "prisma/config";
import { defineConfig } from "prisma/config";

export default defineConfig({
schema: "prisma/schema.prisma",
migrations: {
path: "prisma/migrations",
},
datasource: {
url: env("DATABASE_URL"),
url: process.env["DATABASE_URL"],
},
});
```

:::warning

It is recommended to add `packages/database/generated` to your root `.gitignore` because generated Prisma Client code is a build artifact that can be recreated with `db:generate`.
It is recommended to add `packages/database/generated` to your root `.gitignore` because generated Prisma Client code is a build artifact that can be recreated with `db:generate`. If you build apps from this monorepo with Docker, add the same path to `.dockerignore` and run `db:generate` during the image build instead of copying generated files from your host machine.

:::

Expand Down Expand Up @@ -306,6 +306,8 @@ If you're not using a bundler, use the [Compiled Packages](https://turborepo.dev

By completing these steps, you'll make the Prisma types and `PrismaClient` instance accessible throughout the monorepo.

Export Prisma Client from the workspace package (`@repo/db`) and import that package from apps. Avoid importing the generated client with a relative path from another workspace, because that couples apps to the database package's internal folder layout and makes Docker build contexts harder to keep minimal.

## 3. Import the `database` package in the web app

The `turborepo-prisma` project should have an app called `web` at `apps/web`. Add the `database` dependency to `apps/web/package.json`:
Expand Down
Loading