这是indexloc提供的服务,不要输入任何密码
Skip to content

With dockerfile update #10550

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions examples/with-docker/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,8 @@ yarn-error.log*

# turbo
.turbo

# archived dockerfiles
apps/api/archived-dockerfile/
apps/web/archived-dockerfile/
.yarn/cache
Binary file added examples/with-docker/.yarn/install-state.gz
Binary file not shown.
1 change: 1 addition & 0 deletions examples/with-docker/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodeLinker: node-modules
16 changes: 13 additions & 3 deletions examples/with-docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ Each package/app is 100% [TypeScript](https://www.typescriptlang.org/).

### Docker

This repo is configured to be built with Docker, and Docker compose. To build all apps in this repo:
This repo is configured to be built with Docker, and Docker compose.
Inside each app [web & api] there is a folder called: `dockerfile-examples` with example dockerfiles for npm, pnpm, and yarn. These files are used to build the docker images and they are accessible from the `scripts/use-dockerfile.js` script. to The use-dockerfile.js script is accessible from the package.json with the following scripts:

- `use-pnpm-dockerfile`
- `use-yarn-dockerfile`
- `use-npm-dockerfile`
Once you run these scripts you can delete the `dockerfile-examples` folder if you'd like and the subsequent commands in your package.jsob. By default this app comes with yarn as the package maanger which is what the directions below are configured for.

```
# Install dependencies
Expand All @@ -37,12 +43,16 @@ yarn install
# Create a network, which allows containers to communicate
# with each other, by using their container name as a hostname
docker network create app_network
or run the `docker:network` command in the package.json

# Build prod using new BuildKit engine
# Build **prod** using new BuildKit engine
COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1 docker-compose -f docker-compose.yml build

# Start prod in detached mode
# Start **prod** in detached mode
docker-compose -f docker-compose.yml up -d

for **development** you can run this command to start the app in dev mode
docker-compose -f docker-compose.yml up -d or run the `docker:up` command in the package.json
```

Open http://localhost:3000.
Expand Down
3 changes: 3 additions & 0 deletions examples/with-docker/apps/api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# ───────────────────────────────────────────────────────
# YARN-based build
# ───────────────────────────────────────────────────────
FROM node:18-alpine AS base

# The web Dockerfile is copy-pasted into our main docs at /docs/handbook/deploying-with-docker.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# ───────────────────────────────────────────────────────
# NPM-based build
# ───────────────────────────────────────────────────────
FROM node:18-alpine AS base

# The web Dockerfile is copy-pasted into our main docs at /docs/handbook/deploying-with-docker.
# Make sure you update this Dockerfile, the Dockerfile in the web workspace and copy that over to Dockerfile in the docs.

FROM base AS builder
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk update
RUN apk add --no-cache libc6-compat
# Set working directory
WORKDIR /app
RUN npm install -g turbo
COPY . .
RUN turbo prune api --docker

# Add lockfile and package.json's of isolated subworkspace
FROM base AS installer
RUN apk update
RUN apk add --no-cache libc6-compat
WORKDIR /app

# First install dependencies (as they change less often)
COPY --from=builder /app/out/json/ .
RUN npm install

# Build the project and its dependencies
COPY --from=builder /app/out/full/ .

# Uncomment and use build args to enable remote caching
# ARG TURBO_TEAM
# ENV TURBO_TEAM=$TURBO_TEAM

# ARG TURBO_TOKEN
# ENV TURBO_TOKEN=$TURBO_TOKEN

RUN npx turbo build

FROM base AS runner
WORKDIR /app

# Don't run production as root
RUN addgroup --system --gid 1001 expressjs
RUN adduser --system --uid 1001 expressjs
USER expressjs
COPY --from=installer /app .

CMD node apps/api/dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# ───────────────────────────────────────────────────────
# PNPM-based build
# ───────────────────────────────────────────────────────
FROM node:18-alpine AS base

# The web Dockerfile is copy-pasted into our main docs at /docs/handbook/deploying-with-docker.
# Make sure you update this Dockerfile, the Dockerfile in the web workspace and copy that over to Dockerfile in the docs.

FROM base AS builder
RUN apk update && apk add --no-cache libc6-compat
WORKDIR /app

# Set PNPM_HOME to a directory that exists
ENV PNPM_HOME=/pnpm
RUN mkdir -p /pnpm && \
echo "export PATH=$PNPM_HOME:$PATH" >> /etc/profile

# Install pnpm and turbo globally
RUN corepack enable && corepack prepare pnpm@latest --activate
RUN pnpm config set global-bin-dir $PNPM_HOME
RUN pnpm add -g turbo

COPY . .
RUN turbo prune api --docker

# Add lockfile and package.json's of isolated subworkspace
FROM base AS installer
RUN apk update
RUN apk add --no-cache libc6-compat
WORKDIR /app

# First install dependencies (as they change less often)
COPY --from=builder /app/out/json/ .
RUN pnpm install

# Build the project and its dependencies
COPY --from=builder /app/out/full/ .

# Uncomment and use build args to enable remote caching
# ARG TURBO_TEAM
# ENV TURBO_TEAM=$TURBO_TEAM

# ARG TURBO_TOKEN
# ENV TURBO_TOKEN=$TURBO_TOKEN

RUN pnpm turbo build

FROM base AS runner
WORKDIR /app

# Don't run production as root
RUN addgroup --system --gid 1001 expressjs
RUN adduser --system --uid 1001 expressjs
USER expressjs
COPY --from=installer /app .

CMD node apps/api/dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# ───────────────────────────────────────────────────────
# YARN-based build
# ───────────────────────────────────────────────────────
FROM node:18-alpine AS base

# The web Dockerfile is copy-pasted into our main docs at /docs/handbook/deploying-with-docker.
# Make sure you update this Dockerfile, the Dockerfile in the web workspace and copy that over to Dockerfile in the docs.

FROM base AS builder
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk update
RUN apk add --no-cache libc6-compat
# Set working directory
WORKDIR /app
RUN yarn global add turbo
COPY . .
RUN turbo prune api --docker

# Add lockfile and package.json's of isolated subworkspace
FROM base AS installer
RUN apk update
RUN apk add --no-cache libc6-compat
WORKDIR /app

# First install dependencies (as they change less often)
COPY --from=builder /app/out/json/ .
RUN yarn install

# Build the project and its dependencies
COPY --from=builder /app/out/full/ .

# Uncomment and use build args to enable remote caching
# ARG TURBO_TEAM
# ENV TURBO_TEAM=$TURBO_TEAM

# ARG TURBO_TOKEN
# ENV TURBO_TOKEN=$TURBO_TOKEN

RUN yarn turbo build

FROM base AS runner
WORKDIR /app

# Don't run production as root
RUN addgroup --system --gid 1001 expressjs
RUN adduser --system --uid 1001 expressjs
USER expressjs
COPY --from=installer /app .

CMD node apps/api/dist/index.js
4 changes: 4 additions & 0 deletions examples/with-docker/apps/web/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# ───────────────────────────────────────────────────────
# YARN-based build
# ───────────────────────────────────────────────────────

FROM node:18-alpine AS base

# This Dockerfile is copy-pasted into our main docs at /docs/handbook/deploying-with-docker.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# ───────────────────────────────────────────────────────
# NPM-based build
# ───────────────────────────────────────────────────────

FROM node:18-alpine AS base

# Builder stage
FROM base AS builder
RUN apk update && apk add --no-cache libc6-compat
WORKDIR /app

# Install Turbo CLI
RUN npm install -g turbo

COPY . .
# Prune only the web workspace for Docker
RUN turbo prune web --docker

# Installer stage
FROM base AS installer
RUN apk update && apk add --no-cache libc6-compat
WORKDIR /app

# Copy lockfiles and package.jsons
COPY --from=builder /app/out/json/ .
# Install dependencies
RUN npm install

# Copy full workspace and build
COPY --from=builder /app/out/full/ .
RUN npx turbo build

# Runner stage
FROM base AS runner
WORKDIR /app

# Create non-root user
RUN addgroup --system --gid 1001 nodejs \
&& adduser --system --uid 1001 nextjs
USER nextjs

# Copy only what's needed to run Next.js standalone
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/.next/standalone ./
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/.next/static ./apps/web/.next/static
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/public ./apps/web/public

CMD ["node", "apps/web/server.js"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# ───────────────────────────────────────────────────────
# PNPM-based build
# ───────────────────────────────────────────────────────

FROM node:18-alpine AS base
# Enable Corepack and pin pnpm to the version in packageManager
RUN corepack enable \
&& corepack prepare pnpm@10.10.0 --activate

# Builder stage
FROM base AS builder
RUN apk update && apk add --no-cache libc6-compat
WORKDIR /app

# Install Turbo CLI
RUN pnpm add -g turbo

COPY . .
# Prune only the web workspace for Docker
RUN turbo prune web --docker

# Installer stage
FROM base AS installer
RUN apk update && apk add --no-cache libc6-compat
WORKDIR /app

# Copy lockfiles and package.jsons
COPY --from=builder /app/out/json/ .
# Install dependencies
RUN pnpm install --frozen-lockfile

# Copy full workspace and build
COPY --from=builder /app/out/full/ .
RUN pnpm turbo build

# Runner stage
FROM base AS runner
WORKDIR /app

# Create non-root user
RUN addgroup --system --gid 1001 nodejs \
&& adduser --system --uid 1001 nextjs
USER nextjs

# Copy only what's needed to run Next.js standalone
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/.next/standalone ./
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/.next/static ./apps/web/.next/static
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/public ./apps/web/public

CMD ["node", "apps/web/server.js"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# ───────────────────────────────────────────────────────
# YARN-based build
# ───────────────────────────────────────────────────────

FROM node:18-alpine AS base

# This Dockerfile is copy-pasted into our main docs at /docs/handbook/deploying-with-docker.
# Make sure you update both files!

FROM base AS builder
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk update
RUN apk add --no-cache libc6-compat
# Set working directory
WORKDIR /app
RUN yarn global add turbo
COPY . .
RUN turbo prune web --docker

# Add lockfile and package.json's of isolated subworkspace
FROM base AS installer
RUN apk update
RUN apk add --no-cache libc6-compat
WORKDIR /app

# First install the dependencies (as they change less often)
COPY --from=builder /app/out/json/ .
RUN yarn install

# Build the project
COPY --from=builder /app/out/full/ .

# Uncomment and use build args to enable remote caching
# ARG TURBO_TEAM
# ENV TURBO_TEAM=$TURBO_TEAM

# ARG TURBO_TOKEN
# ENV TURBO_TOKEN=$TURBO_TOKEN

RUN yarn turbo build

FROM base AS runner
WORKDIR /app

# Don't run production as root
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
USER nextjs

# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/.next/standalone ./
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/.next/static ./apps/web/.next/static
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/public ./apps/web/public

CMD node apps/web/server.js
8 changes: 4 additions & 4 deletions examples/with-docker/apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
"start": "next start"
},
"dependencies": {
"@repo/ui": "*",
"next": "^14.1.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"@repo/ui": "*"
"react-dom": "^18.2.0"
},
"devDependencies": {
"@next/eslint-plugin-next": "^14.1.1",
"@repo/eslint-config": "*",
"@repo/typescript-config": "*",
"@types/node": "^20.11.24",
"@types/react": "^18.2.61",
"@types/react-dom": "^18.2.19",
"eslint": "^8.57.0",
"@repo/eslint-config": "*",
"@repo/typescript-config": "*",
"typescript": "5.5.4"
}
}
Loading
Loading