简体   繁体   English

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

[英]Docker container works from Dockerfile but get next: not found from docker-compose container

I am having an issue with my docker-compose configuration file.我的 docker-compose 配置文件有问题。 My goal is to run a Next.js app with a docker-compose file and enable hot reload.我的目标是使用 docker-compose 文件运行 Next.js 应用程序并启用热重载。

Running the Next.js app from its Dockerfile works but hot reload does not work.从 Dockerfile 运行 Next.js 应用程序可以工作,但热重载不起作用。 Running the Next.js app from the docker-compose file triggers an error: /bin/sh: next: not found and I was not able to figure what's wrong...从 docker-compose 文件运行 Next.js 应用程序会触发错误: /bin/sh: next: not found ,我无法弄清楚出了什么问题...

Dockerfile : (taken from Next.js' documentation website) Dockerfile :(取自 Next.js 的文档网站)

[Notice it's a multistage build however, I am only referencing the builder stage in the docker-compose file.] [请注意,这是一个多阶段构建,但我仅引用 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 : 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 : 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"
  }
}

When calling yarn dev from docker-compose.yml it actually calls next dev and that's when it triggers the error /bin/sh: next: not found .当从docker-compose.yml调用yarn dev时,它实际上调用了next dev ,这就是它触发错误/bin/sh: next: not found的时候。 However, running the container straight from the Dockerfile works and does not lead to this error.但是,直接从Dockerfile运行容器可以正常工作并且不会导致此错误。

[Update]: [更新]:

If I remove the volume attribute from my docker-compse.yml file, I don't get the /bin/sh: next: not found error and the container runs however, I now don't get the hot reload feature I am looking for.如果我从我docker-compse.yml文件中删除volume属性,我没有得到/bin/sh: next: not found错误并且容器运行但是,我现在没有得到我正在寻找的热重载功能为了。 Any idea why the volume is messing up with the /bin/sh next command?知道为什么卷与/bin/sh next命令混淆了吗?

This is happening because your local filesystem is being mounted over what is in the docker container.发生这种情况是因为您的本地文件系统正在安装在 docker 容器中的内容上。 Your docker container does build the node modules in the builder stage, but I'm guessing you don't have the node modules available in your local file system.您的 docker 容器确实在builder阶段构建了节点模块,但我猜您的本地文件系统中没有可用的节点模块。

To see if this is what is happening, on your local file system, you can do a yarn install .要查看这是否正在发生,在您的本地文件系统上,您可以执行yarn install Then try running your container via docker again.然后尝试再次通过 docker 运行您的容器。 I'm predicting that this will work, as yarn will have installed next locally, and it is actually your local file system's node modules that will be run in the docker container.我预测这会起作用,因为yarn将在本地安装next ,实际上是您的本地文件系统的节点模块将在 docker 容器中运行。

One way to fix this is to volume mount everything except the node modules folder.解决此问题的一种方法是卷安装节点模块文件夹之外的所有内容。 Details on how to do that: Add a volume to Docker, but exclude a sub-folder有关如何执行此操作的详细信息: 将卷添加到 Docker,但排除子文件夹

So in your case, I believe you can add a line to your compose file:因此,在您的情况下,我相信您可以在撰写文件中添加一行:

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

That should allow the docker container's node_modules to not be overwritten by any volume mount.这应该允许 docker 容器的node_modules不会被任何卷安装覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM