You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker构建React应用时模块未找到,容器无法正常运行的问题排查求助

Docker构建React应用时模块未找到,容器无法正常运行的问题排查求助

大家好,我现在碰到了一个Docker构建React应用的棘手问题,容器没法正常运行,执行构建命令后一直报模块找不到的错误,具体报错内容如下:

Creating an optimized production build...
Failed to compile.
Module not found: Error: Can't resolve './src/theme' in '/usr/bin/app/src'
make: *** [Makefile:16: react-build] Error 1

对应的Makefile第16行命令是:

docker run -it -v $(shell realpath out):/usr/bin/app/build frontend-temp bash -c "npm run build"

下面是我的相关配置文件内容,麻烦各位帮我排查下哪里出问题了:

nginx-container/Dockerfile

FROM nginx

COPY out /usr/share/nginx/html
COPY version.html /usr/share/nginx/html/version.html
COPY nginx-container/nginx.conf /etc/nginx/nginx.conf
COPY nginx-container/default.conf /etc/nginx/conf.d/default.conf

build-container/Dockerfile

FROM debian:buster-slim

RUN apt-get update && \
apt-get install -y curl && \
curl -fsSL https://deb.nodesource.com/setup_16.x | bash - &&\
apt-get update && \
apt-get install -y nodejs

RUN mkdir -p /usr/bin/app
RUN npm install -g npm

COPY web-application/src /usr/bin/app/src
COPY web-application/package.json /usr/bin/app/
COPY web-application/package-lock.json /usr/bin/app/
COPY web-application/public /usr/bin/app/public

WORKDIR /usr/bin/app

RUN npm install --force

#CMD ["npm", "run", "build"]

EXPOSE 8080

最终的Makefile

all: react-build web-app

DOCKERTAG?='cloud-interface:latest'

GIT_VERSION := $(shell git describe --abbrev=7 --dirty --always --tags)

web-app:
	echo $(GIT_VERSION) > version.html
	docker build -t $(DOCKERTAG) -f nginx-container/Dockerfile .
	rm -rf out
	docker rmi -f frontend-temp

react-build: build-container/Dockerfile web-application/src web-application/package-lock.json web-application/package.json web-application/public
	docker build --no-cache -t frontend-temp -f build-container/Dockerfile .
	rm -rf out
	mkdir out
	docker run -it -v $(shell realpath out):/usr/bin/app/build frontend-temp bash -c "npm run build"

.PHONY: clean

我翻来覆去检查了好几遍还是找不到问题所在,有没有大佬能给点修复建议呀?

备注:内容来源于stack exchange,提问作者Igor-Potapov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 12:09:32