如何在 Dockerfile 中使用 npm run build 来执行 React js

问题描述 投票:0回答:1
babel-preset-react-app is part of the create-react-app project, which
 => => # is not maintianed anymore. It is thus unlikely that this bug will
 => => # ever be fixed. Add "@babel/plugin-proposal-private-property-in-object" to
 => => # your devDependencies to work around this error. This will make this message
 => => # go away.

我尝试在 Dockerfile 中运行

npm run build command
,它显示上述错误。如果我手动输入命令,它就可以工作。

我需要在 Dockerfile 本身内运行

npm install
npm run build
。现在我手动输入
npm run build
在ubuntu服务器中部署节点文件。

如何在 dockerfile 本身内自动运行这些命令?

docker docker-compose dockerfile nginx-config
1个回答
0
投票

要在 Dockerfile 中自动运行 npm install 和 npm run build 命令,您可以将以下行添加到 Dockerfile 中:

# Use the official Node.js image as a base
FROM node:latest

# Set the working directory in the container
WORKDIR /app

# Copy package.json and package-lock.json to the working directory
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of your application code to the working directory
COPY . .

# Run the build command
RUN npm run build

# Specify the command to run your application, assuming it's a web server
CMD ["npm", "start"]

使用此 Dockerfile,当您构建 Docker 映像 (

docker build -t my-app .
) 并从中运行容器 (
docker run -d my-app
) 时,
npm install
npm run build
命令将在构建过程中自动执行。

© www.soinside.com 2019 - 2024. All rights reserved.