总之,在try 构建Django、Reaction和Vite应用程序时,我遇到了在Docker容器中安装npm
的问题.
最初,npm
和Node.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:个
- 如果带着类似的问题访问这篇文章,在查看了"接受的答案"之后,Vite.js GitHub Repository中的
create-vite
个模板对于使用Vite
和React
依赖项配置package.json
非常有帮助.@Phil建议的初始链接链接到Vite Documentation. - 为了解决下面的
Vite
/React
错误(below),这篇文章中建议的解决方案也被证明是非常有帮助的:
[ERROR] The JSX syntax extension is not currently enabled