我使用npx create-next-app@latest
创建了一个全新的NextJS应用程序,我试图从它构建一个Docker镜像,但在构建步骤中它无法解析我的路径别名。构建步骤在我的本地机器上工作,但当我试图将其构建到docker镜像中时,它不起作用。
错误消息:
#14 3.497 info - Creating an optimized production build...
#14 5.828 Failed to compile.
#14 5.828
#14 5.828 ./pages/_app.tsx
#14 5.828 Module not found: Can't resolve '@src/styles/globals.css'
#14 5.828
#14 5.828 https://nextjs.org/docs/messages/module-not-found
#14 5.828
#14 5.828 ./pages/index.tsx
#14 5.828 Module not found: Can't resolve '@src/styles/Home.module.css'
#14 5.828
#14 5.828 https://nextjs.org/docs/messages/module-not-found
#14 5.828
#14 5.829
#14 5.830 > Build failed because of webpack errors
这里有一个GitHub仓库,你可以克隆并尝试自己:
https://github.com/TJBlackman/nextjs-docker-test
npm run build
工作!npm run build:docker
不工作...
下面是来自repo的Dockerfile:
# Use an official Node.js runtime as a parent image
FROM node:18-alpine
# Set the working directory to /app
WORKDIR /app
# Copy package.json to the container
COPY package.json .
COPY tsconfig.json .
COPY next.config.js .
COPY next-env.d.ts .
COPY src .
# Install dependencies
RUN npm install
# Build the Next.js app
RUN npm run build
# Set the environment variable for running the app
ENV NODE_ENV=production
# Expose port 3000 for the app to listen on
EXPOSE 3000
# Start the app
CMD ["npm", "run", "start"]
提前感谢您的帮助!
编辑:
- 我尝试更新Dockerfile以使用
node
而不是node:18-alphine
,但没有成功。 - 我将
"baseUrl": "."
添加到tsconfig.json文件中,但没有成功。
1条答案
按热度按时间fhg3lkii1#
将
COPY src .
更改为COPY src/ src/
头脑 Storm 。我们中最优秀的人都会遇到...¯_()_/¯