我的docker-compose配置文件有问题。我的目标是运行一个带有docker-compose文件的Next.js应用程序,并启用热重载。
从其Dockerfile运行Next.js应用程序可以工作,但热重载不起作用。从docker-compose文件运行Next.js应用程序会触发一个错误:/bin/sh: next: not found
我不知道是什么错了。
Dockerfile
:(摘自Next.js文档网站)
[请注意,这是一个多阶段构建,但我只引用docker-compose文件中的builder
阶段。]
# Install dependencies only when needed
FROM node:18-alpine AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install # --frozen-lockfile
# Rebuild the source code only when needed
FROM node:18-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
ENV NEXT_TELEMETRY_DISABLED 1
RUN yarn build
# If using npm comment out above and use below instead
# RUN npm run build
# Production image, copy all the files and run next
FROM node:18-alpine AS runner
WORKDIR /app
ENV NODE_ENV production
# Uncomment the following line in case you want to disable telemetry during runtime.
ENV NEXT_TELEMETRY_DISABLED 1
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
# You only need to copy next.config.js if you are NOT using the default configuration
# COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/package.json ./package.json
# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
USER nextjs
EXPOSE 3001
ENV PORT 3001
CMD ["node", "server.js"]
docker-compose.yml
:
version: "3.9"
services:
db:
image: postgres
volumes:
- ./tmp/db:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: ${POSTGRESQL_PASSWORD}
backend:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
environment:
DATABASE_USERNAME: ${MYAPP_DATABASE_USERNAME}
DATABASE_PASSWORD: ${POSTGRESQL_PASSWORD}
frontend:
build:
context: ./frontend
dockerfile: Dockerfile
target: builder
command: yarn dev
volumes:
- ./frontend:/app
expose:
- "3001"
ports:
- "3001:3001"
depends_on:
- backend
environment:
FRONTEND_BUILD: ${FRONTEND_BUILD}
PORT: 3001
package.json
:
{
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "latest",
"react": "^18.1.0",
"react-dom": "^18.1.0"
}
}
当从docker-compose.yml
调用yarn dev
时,它实际上调用了next dev
,这就是它触发错误/bin/sh: next: not found
的时候。但是,直接从Dockerfile
运行容器可以正常工作,不会导致此错误。
【更新】:
如果我从docker-compse.yml
文件中删除volume
属性,我不会得到/bin/sh: next: not found
错误,容器会运行,但是,我现在不会得到我正在寻找的热重载特性。你知道为什么卷会在/bin/sh next
命令下出错吗?
2条答案
按热度按时间vc9ivgsu1#
这是因为你的本地文件系统被挂载到了docker容器中。您的docker容器确实在
builder
阶段构建了节点模块,但我猜您的本地文件系统中没有可用的节点模块。要查看是否发生了这种情况,可以在本地文件系统上执行
yarn install
。然后再次尝试通过Docker运行容器。我预测这将工作,因为yarn
将在本地安装next
,并且实际上是您的本地文件系统的节点模块将在docker容器中运行。解决这个问题的一种方法是卷装载所有内容 * 除了 * 节点模块文件夹。详细说明如何做到这一点:Add a volume to Docker, but exclude a sub-folder
所以在你的例子中,我相信你可以在你的compose文件中添加一行:
这应该允许docker容器的
node_modules
不会被任何卷挂载覆盖。1dkrff032#
您也可以将其解析为
volumes
指令: