【发布时间】:2021-09-15 12:00:01
【问题描述】:
我正在尝试使用 docker 构建我的 react 应用程序,当我在本地运行 npm run build 时它成功构建,但是当我使用 docker 构建时,它在构建阶段失败。
我的 docker 文件
FROM node:alpine as builder
WORKDIR /app
COPY package.json .
COPY yarn.lock .
RUN yarn
COPY . .
RUN yarn build
# => Run container
FROM nginxinc/nginx-unprivileged:mainline-alpine
# Nginx config
#RUN rm -rf /etc/nginx/conf.d
#COPY conf /etc/nginx
# Static build
COPY --from=builder /app/build /usr/share/nginx/html/
COPY default.conf /etc/nginx/conf.d/
# Default port exposure
EXPOSE 80
# Copy .env file and shell script to container
WORKDIR /usr/share/nginx/html
COPY ./env.sh .
COPY .env .
USER root
# Add bash
RUN apk add --no-cache bash
# Make our shell script executable
RUN chmod +x env.sh
# Start Nginx server
CMD ["/bin/bash", "-c", "/usr/share/nginx/html/env.sh && nginx -g \"daemon off;\""]
运行命令 docker build -t project 。 下面是输出
> [builder 7/7] RUN yarn build --no-cache=true:
#14 1.278 yarn run v1.22.5
#14 1.361 $ react-scripts build --no-cache=true
#14 3.409 Creating an optimized production build...
#14 5.746 Failed to compile.
#14 5.746
#14 5.747 ./src/index.js
#14 5.747 TypeError: invalid options argument
#14 5.747 at new Promise (<anonymous>)
#14 5.747 at Generator.throw (<anonymous>)
#14 5.747
#14 5.747
#14 5.778 error Command failed with exit code 1.
#14 5.778 info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
------
executor failed running [/bin/sh -c yarn build --no-cache=true]: exit code: 1
【问题讨论】: