总之,在try 构建Django、Reaction和Vite应用程序时,我遇到了在Docker容器中安装npm的问题.

最初,npmNode.js安装程序都没有在容器中执行.但是,在添加了我的Dockerfile中引用的两个COPY命令后,我解决了Node.js故障.

然而,即使在try 了许多配置之后,npm故障仍然存在.

Checking 100 installation inside the container:

% docker exec -it docktest-web-1 node -v
  v14.21.3

Checking 100 installation inside the container:

% docker exec -it docktest-web-1 npm -v
  OCI runtime exec failed: exec failed: unable to start container process: exec: "npm": executable file not found in $PATH: unknown

*The build still completes successfully, but 100 is not installed in the container.


我的配置的相关方面如下:

100

# Use the official Node.js image for frontend build
FROM node:14 as frontend-build

# Set the working directory for the frontend build
WORKDIR /frontend

# Copy only the package.json to take advantage of Docker caching mechanism
COPY frontend/package.json ./

# Copy the rest of the frontend code to the container
COPY frontend/ .

# Install frontend dependencies
RUN npm cache clean --force
RUN npm install vite -g
RUN npm install npm@latest -g

# Build the frontend
RUN npm run build

# Create main container for Django and PostgreSQL
FROM python:3

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV NODE_PATH=/usr/local/lib/node_modules

# Without these two lines,the Node.js installation failed as well
COPY --from=frontend-build /usr/local/bin/node /usr/local/bin/
COPY --from=frontend-build /usr/local/lib/node_modules/ /usr/local/lib/node_modules/

# Set the working directory for Django
WORKDIR /code

# Copy the backend requirements and install dependencies
COPY requirements.txt /code/
RUN pip3 install -r requirements.txt

# Copy the frontend build from the previous stage to the working directory of the main container
COPY --from=frontend-build /frontend/ /code/frontend/

# Copy the frontend
COPY . /code/

100

services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
      - /code/frontend/  # Exclude frontend folder from being mounted to avoid conflicts
    ports:
      - "8000:8000"
    environment:
      - POSTGRES_NAME=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    depends_on:
      - db

Addendum – Update 8/1:

  1. 如果带着类似的问题访问这篇文章,在查看了"接受的答案"之后,Vite.js GitHub Repository中的create-vite个模板对于使用ViteReact依赖项配置package.json非常有帮助.@Phil建议的初始链接链接到Vite Documentation.
  2. 为了解决下面的Vite/React错误(below),这篇文章中建议的解决方案也被证明是非常有帮助的:
[ERROR] The JSX syntax extension is not currently enabled

推荐答案

您似乎正在try 为前端和后端运行单独的服务.问题是,您为两者生成的图像都是单一的,这将很难管理.

我会设置单独的组合服务,每个组合服务都有自己的构建上下文.如果您计划同时支持开发和类似于生产的配置,我建议您也使用单独的Docker和Compose文件.

例如,这是一个开发配置,带有源代码的卷装载,并运行带有HMR的vite dev-server.

# ./frontend/Dockerfile.dev

FROM node:14-alpine # it's seriously time to upgrade https://endoflife.date/nodejs

WORKDIR /app
EXPOSE 5173

COPY package*.json .
RUN npm install

# no need to copy other files since we'll volume-mount them

CMD ["npm", "run", "dev", "--", "--host"]
# ./Dockerfile.dev
# You may want to consider putting your backend code into its own folder too

FROM python:3

WORKDIR /code
EXPOSE 8000

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

COPY requirements.txt .
RUN pip3 install -r requirements.txt

CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
# docker-compose.yaml
# ...

react:
  build:
    context: ./frontend
    dockerfile: Dockerfile.dev
  ports:
    - "5173:5173"
  volumes:
    - ./frontend:/app
    - /app/node_modules

django:
  build:
    context: .
    dockerfile: Dockerfile.dev
  ports:
    - "8000:8000"
  volumes:
    - .:/code
  environment:
    - POSTGRES_NAME=postgres
    - POSTGRES_USER=postgres
    - POSTGRES_PASSWORD=postgres
  depends_on:
    - db

生产版本看起来更像原始的Dockerfile,使用multi-stage build编译您的Reaction应用程序,然后将其复制到Django的静态服务目录(note: I'm not a Python dev but it seems a fairly straight forward configuration based on 101)

# Dockerfile

# Frontend build
FROM node:14-alpine AS frontend-build
ENV NODE_ENV=production
WORKDIR /build

COPY frontend/package*.json .
RUN npm ci

COPY frontend .
RUN npm run build

# Backend build
FROM python:3

WORKDIR /app
EXPOSE 8000

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

COPY requirements.txt .
RUN pip3 install -r requirements.txt

COPY . .
COPY --from=frontend-build /build/dist ./staticfiles # ¯\_(ツ)_/¯

CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

Node.js相关问答推荐

如何处理EPIPE时,使用axios并期待413响应?

当建议在第二代上运行云功能时,现在是否有新的Firestore AdminClient可供使用?

在我的Next.js应用程序中没有正确设置Process.env.NODE_ENV

如果我在 tsx 文件中使用了use client,ssr 会如何发生?

无法从 mongoDB 访问数据?

如何在mongoose 聚合中执行全文搜索

TypeError:在使用 Jest、Supertest、Express、Typescript 进行测试时无法读取未定义的属性(读取listen)

如何避免在 mongodb 聚合中查找重复结果?

对 google api v3 的 Axios 请求返回加密(?)数据

npm chokidar 触发事件两次

用户通过 oauth2 twitter 授权或通过Passport discord后如何重定向到 React/Vue 路由?

我可以通过 JNI 从 Node.js 调用 Java 吗?如何?

我可以向NPM添加调试脚本吗?

在多个文件 node.js 之间共享和修改变量

使用 Socket.io 将客户端连接到服务器

如何从命令行在 Node.js 上运行 Jasmine 测试

Nodejs续集批量更新

npm package.json 操作系统特定脚本

在 Jade 包含中使用变量

node.js 中的意外保留字导入