【问题标题】:Dockerfile with angular and nginx fails at the build phase带有角度和 nginx 的 Dockerfile 在构建阶段失败
【发布时间】:2021-09-25 22:17:34
【问题描述】:

我正在尝试使用 Angular 和 nginx 为我的项目构建 docker 映像,但在尝试安装映像时在构建阶段出现以下错误(安装似乎工作正常):

#11 103.1 Error: src/app/models/index.ts:5:28 - error TS2307: Cannot find module './inputField' or its corresponding type declarations.
#11 103.1
#11 103.1 5 export { InputField } from './inputField';
#11 103.1                              ~~~~~~~~~~~~~~
#11 103.1
#11 103.1
#11 103.1
#11 103.1 npm ERR! code ELIFECYCLE
#11 103.1 npm ERR! errno 1
#11 103.2 npm ERR! myproject@11.0.0 build: `ng build --build-optimizer --output-hashing=none`
#11 103.2 npm ERR! Exit status 1
#11 103.2 npm ERR!
#11 103.2 npm ERR! Failed at the myproject@11.0.0 build script.
#11 103.2 npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
#11 103.2
#11 103.2 npm ERR! A complete log of this run can be found in:
#11 103.2 npm ERR!     /root/.npm/_logs/2021-07-17T10_12_55_066Z-debug.log
------
executor failed running [/bin/sh -c npm run build]: exit code: 1

我的码头文件:

# Angular image
FROM node:latest as build

WORKDIR /app
COPY . .
RUN npm install 
RUN npm run build

# Nginx image
FROM nginx:latest

COPY --from=build /dist/angular/ /usr/share/nginx/html/

EXPOSE 80

我已经完成了 npm audit fix 来解决几个问题,我已经删除了 node_modules 文件夹、缓存和 package-lock.json,并重新安装,但似乎没有任何帮助。

提前致谢。

【问题讨论】:

  • RUN npm install 之后将所有内容复制到您的工作目录COPY . .
  • 根据输出问题是inputField 这是什么?你在本地有那个文件吗?
  • @JasonWhite 如果您之前没有 COPY,那么 npm install 将找不到 package.json,因为 /app 目录将为空
  • InputField 是一个普通的 .ts 类,位于其他模型中。该特定行来自 index.ts
  • 你有.dockerignore文件吗?如果有,里面有什么?如果你注释掉 RUN npm installdocker run --rm ... bash 生成的图像之后的所有内容,你的源文件在那里吗?

标签: node.js angular docker nginx npm


【解决方案1】:

在构建 docker 映像之前,尝试在本地运行 ng build 命令。它将显示错误。目前无法访问InputField

export { InputField } from './inputField';

【讨论】:

  • 构建运行在第一个容器注释as build
  • ng build 不会给出任何错误,它可以正确构建,仅在失败的 dockerfile 中。除此之外,InputField 是一个普通的 .ts 类,位于其他模型中。该特定行来自 index.ts
【解决方案2】:

根据原问题:

error TS2307: Cannot find module './inputField'

将文件从 InputField.ts 重命名为 inputField.ts 解决了在容器内构建的问题。

查看构建容器:

仅运行构建容器是来自@David Maze 的出色评论

类似这样的:

$ docker run --rm -it <name_of_the_container> /bin/bash

这样你就可以发出npm run build 命令,看看里面发生了什么。

我还建议使用干净的ng new ngApp 作为参考。 如果可以构建该应用程序,那么问题出在其他问题上,而不是Dockerfile

示例:

# Angular build image
FROM node:latest as builder

WORKDIR /app
COPY . .
RUN npm install
RUN npm run build


# Nginx image
FROM nginx:latest

COPY --from=builder /app/dist/ngApp /usr/share/nginx/html/

EXPOSE 80

【讨论】:

    猜你喜欢
    • 2020-12-19
    • 1970-01-01
    • 2020-11-09
    • 2021-10-29
    • 1970-01-01
    • 2020-06-25
    • 2021-09-01
    • 2015-06-16
    • 1970-01-01
    相关资源
    最近更新 更多