微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Create React App 无法在 docker 中构建

如何解决Create React App 无法在 docker 中构建

我正在尝试 dockerize 一个 react 应用程序。 它使用 craco 进行构建,因为我使用的是 tailwindcss。 它一直正常工作,直到今天构建开始抛出 CSS 文件错误

错误

 > [admin-build 7/7] RUN npm run build:
#15 1.594 
#15 1.594 > admin@0.1.0 build /app
#15 1.594 > craco build
#15 1.594 
#15 3.555 craco:  *** Cannot find ESLint loader (eslint-loader). ***
#15 3.873 Creating an optimized production build...
#15 89.72 Failed to compile.
#15 89.72 
#15 89.72 ./src/styles/index.css
#15 89.72 TypeError: Cannot convert undefined or null to object
#15 89.72     at Function.entries (<anonymous>)
#15 89.72     at Array.forEach (<anonymous>)
#15 89.72 
#15 89.72 
#15 89.75 npm ERR! code ELIFECYCLE
#15 89.75 npm ERR! errno 1
#15 89.76 npm ERR! admin@0.1.0 build: `craco build`
#15 89.76 npm ERR! Exit status 1
#15 89.76 npm ERR! 
#15 89.76 npm ERR! Failed at the admin@0.1.0 build script.
#15 89.76 npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
#15 89.76 
#15 89.76 npm ERR! A complete log of this run can be found in:
#15 89.76 npm ERR!     /root/.npm/_logs/2021-06-26T14_32_59_262Z-debug.log
------

我的 Dockerfile

# base image
FROM node:14-alpine as admin-build

# workdir
RUN mkdir /app
workdir /app
# Install dependencies
copY package.json ./
RUN npm install
#  copy source
copY . .
# build source
RUN npm run build
# Nginx for serving
FROM Nginx:alpine
# copy configs
copY Nginx.conf /etc/Nginx/conf.d/default.conf
copY --from=admin-build /app/build /usr/share/Nginx/html
EXPOSE 80
CMD ["Nginx","-g","daemon off;"]

应用程序在 docker 之外正确构建。 有什么办法可以解决这个问题,或者至少看看问题出在哪里?

谢谢

解决方法

只需将 ENV 添加到 Dockerfile,

这个对我来说很好用:

FROM node:13.12.0-alpine as build
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY package.json ./
COPY package-lock.json ./
RUN npm install 
COPY . ./
RUN npm run build

# production environment
FROM nginx:stable-alpine
COPY --from=build /app/build /usr/share/nginx/html
COPY ./nginx/default.conf /etc/nginx/conf.d/default.conf

EXPOSE 80
CMD ["nginx","-g","daemon off;"]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。