无法将映像部署到kubernetes(打包结构问题)

时间:2020-05-21 11:15:58

标签: typescript docker kubernetes kubectl

使用下面的dockerfile成功构建后,我无法在eks上部署我的应用程序。

FROM node:12
# Create app directory
WORKDIR /usr/src/app

COPY udagram-feed/package*.json ./
RUN npm ci 
# Bundle app source
COPY . .
EXPOSE 8080
CMD [ "npm", "run", "prod" ]

从群集中获取日志时,这就是我得到的:

internal/modules/cjs/loader.js:960
  throw err;
  ^
Error: Cannot find module '/usr/src/app/www/server.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:957:15)
    at Function.Module._load (internal/modules/cjs/loader.js:840:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! udagram-user@1.0.0 prod: `tsc && node ./www/server.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the udagram-user@1.0.0 prod script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-05-20T19_11_45_220Z-debug.log

在本地运行图像,我具有以下文件结构:

  • Dockerfile
  • node_modules
  • package.json
  • package-lock.json
  • src

src /目录包含serve.ts,sequelize.ts和其他文件。显然没有www /目录。为何Kubernetes在此目录中查找文件? 任何帮助表示赞赏。我已经坚持了好几天了,不知道该怎么办。 有关文件结构,请参见github repo Github

1 个答案:

答案 0 :(得分:0)

首先,您的Dockerfile创建了一个1GB的映像。我对其进行了修改,以创建一个240MB的映像。该Dockerfile可以进一步优化。您可以参考herehere来获取有关创建多阶段Dockerfile的示例。

FROM node:12-alpine 
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./
RUN apk add --no-cache --virtual .gyp \
        python \
        make \
        g++ \
    && npm ci \
    && apk del .gyp
# Bundle app source
COPY . .
EXPOSE 8080
CMD [ "npm", "run", "prod" ]

以下是docker映像大小:

REPOSITORY                          TAG                 IMAGE ID            CREATED             SIZE
udagram-feed-alpine                 latest              185478b5eabc        11 seconds ago      247MB
udagram-feed                        latest              fbf32e67d4fa        4 minutes ago       1.07GB

第二,您的package.json指向./www/server.js文件