-1

我已按照本教程进行操作,效果很好,但是当我尝试 dockerize 时,构建正常,但是当我运行映像时出现错误:错误:找不到模块'express'

DockerFile

    FROM node:12 as buildContainer
    WORKDIR /app
    COPY ./package.json ./package-lock.json /app/
    RUN npm install
    COPY . /app
    # max-old-space is needed to avoid any compilation issues because of missing memory
    ENV NODE_OPTIONS --max-old-space-size=2048
    RUN npm run build:ssr

    FROM node:12-alpine

    WORKDIR /app
    COPY --from=buildContainer /app/package.json /app

    # Get all the code needed to run the app
    COPY --from=buildContainer /app/dist /app/dist

    EXPOSE 4000

    ENV NODE_ENV production
    CMD ["npm", "run", "serve:ssr"]

我的 dist 结构是:

分布\应用

  • 浏览器 -- en -- fr
  • 服务器 -- en -- fr proxy-server.js

代理服务器.js

    const express = require("express");
    const path = require("path");

    const getTranslatedServer = (lang) => {
      const distFolder = path.join(
        process.cwd(),
        `dist/morefont-v4/server/${lang}`
      );
      const server = require(`${distFolder}/main.js`);
      return server.app(lang);
    };

    function run() {
      const port = 4000;

      // Start up the Node server
      const appFr = getTranslatedServer("fr");
      const appEn = getTranslatedServer("en");

      const server = express();
      server.use("/fr", appFr);
      server.use("/en", appEn);
      server.use("", appEn);

      server.listen(port, () => {
        console.log(`Node Express server listening on http://localhost:${port}`);
      });
    }

    run();

可选问题:您是否有正确的方法将 I18n 与 Angular SSR 一起使用?

问题:如何修复我的 Docker 映像以导入 node_modules?

谢谢

4

1 回答 1

1

当你用一条FROM线开始一个新的构建阶段时,构建顺序基本上是完全重新开始的;除非您在此处明确显示COPYRUN安装步骤,否则最终图像中不会出现任何内容。

使用典型的“在第一阶段构建,在第二阶段运行”设置,您可以跳过在最后阶段安装开发依赖项。这将建议重新运行npm install,设置后NODE_ENV=production

FROM node:12-alpine
WORKDIR /app
COPY --from=buildContainer /app/package.json .

# Install dependencies (but not devDependencies)
ENV NODE_ENV production  # move this up from the bottom
RUN npm install          # add this

COPY --from=buildContainer /app/dist ./dist
EXPOSE 4000
CMD ["npm", "run", "serve:ssr"]
于 2021-01-17T12:09:34.320 回答