Docker容器从Dockerfile工作,但获得下一个:未从docker-compose容器中找到

gr8qqesn  于 2023-06-29  发布在  Docker
关注(0)|答案(2)|浏览(177)

我的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命令下出错吗?

vc9ivgsu

vc9ivgsu1#

这是因为你的本地文件系统被挂载到了docker容器中。您的docker容器确实在builder阶段构建了节点模块,但我猜您的本地文件系统中没有可用的节点模块。
要查看是否发生了这种情况,可以在本地文件系统上执行yarn install。然后再次尝试通过Docker运行容器。我预测这将工作,因为yarn将在本地安装next,并且实际上是您的本地文件系统的节点模块将在docker容器中运行。
解决这个问题的一种方法是卷装载所有内容 * 除了 * 节点模块文件夹。详细说明如何做到这一点:Add a volume to Docker, but exclude a sub-folder
所以在你的例子中,我相信你可以在你的compose文件中添加一行:

frontend:
    ...
    volumes:
      - ./frontend:/app
      - ./frontend/node_modules # <-- try adding this!
    ...

这应该允许docker容器的node_modules不会被任何卷挂载覆盖。

1dkrff03

1dkrff032#

您也可以将其解析为volumes指令:

frontend:
    ...
    volumes:
      - ./frontend:/app
      - /app/node_modules # <-- creates the node_modules in frontend path
      - /app/.next # <-- creates the .next folder in frontend path
    ...

相关问题