[英]docker container runs from Dockerfile but not from docker-compose
[英]Docker container works from Dockerfile but get next: not found from docker-compose container
我的 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
命令混淆了嗎?
發生這種情況是因為您的本地文件系統正在安裝在 docker 容器中的內容上。 您的 docker 容器確實在builder
階段構建了節點模塊,但我猜您的本地文件系統中沒有可用的節點模塊。
要查看這是否正在發生,在您的本地文件系統上,您可以執行yarn install
。 然后嘗試再次通過 docker 運行您的容器。 我預測這會起作用,因為yarn
將在本地安裝next
,實際上是您的本地文件系統的節點模塊將在 docker 容器中運行。
解決此問題的一種方法是卷安裝除節點模塊文件夾之外的所有內容。 有關如何執行此操作的詳細信息: 將卷添加到 Docker,但排除子文件夾
因此,在您的情況下,我相信您可以在撰寫文件中添加一行:
frontend:
...
volumes:
- ./frontend:/app
- ./frontend/node_modules # <-- try adding this!
...
這應該允許 docker 容器的node_modules
不會被任何卷安裝覆蓋。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.