简体   繁体   English

将node.js移动到docker容器,接收无法文件模块错误

[英]Moving node.js to a docker container, receiving a cannot file module error

I am trying to create a node.js docker container for an api layer transpiled from typescript copied from a scr folder to a dist folder. 我正在尝试为从scr文件夹复制到dist文件夹的typescript转换的api层创建一个node.js docker容器。 The node.js container seems to exit immediately and when I issue docker-compose logs I get "Error: Cannot find module '/usr/src/app/webapp.js'", however, when I bring up the container with the -d option, I can see where the file and directory structure is in the correct hierarchical structure. node.js容器似乎立即退出,当我发出docker-compose日志时,我得到“错误:无法找到模块'/usr/src/app/webapp.js'”,但是,当我打开带有 - 的容器时d选项,我可以看到文件和目录结构在正确的层次结构中的位置。 What could be wrong here? 这可能有什么问题? Why is node not able to locate the webapp.js file? 为什么节点无法找到webapp.js文件?

Dockerfile: Dockerfile:

FROM node:latest

# install our dependencies and nodejs
RUN mkdir -p /usr/src
RUN mkdir -p /tmp/dist

ADD Account/package.json /tmp/package.json
RUN cd /tmp && npm install --production
ADD Account/dist /tmp/dist
RUN mkdir -p /tmp/node_modules/mongodb-repository

ADD Account/node_modules/mongodb-repository /tmp/node_modules/mongodb-repository
RUN mkdir -p /usr/src/app && cp -a /tmp/node_modules /usr/src/app && cp -a /tmp/package-lock.json /usr/src/app
RUN cp -a /tmp/dist/application /usr/src/app && cp -a /tmp/dist/config /usr/src/app && cp -a /tmp/dist/domain /usr/src/app && cp -a /tmp/dist/infrastructure /usr/src/app
RUN cp -a /tmp/dist/routes /usr/src/app && cp -a /tmp/dist/types /usr/src/app && cp -a /tmp/dist/webapp.js /usr/src/app && cp -a /tmp/package.json /usr/src/app
RUN chmod 755 /usr/src/app/webapp.js
RUN mkdir -p /usr/src/app/bin

COPY Account/bin /usr/src/app/bin
COPY Account/.env /usr/src/app/.env

WORKDIR /usr/src/app

CMD [ "node", "webapp.js" ]
EXPOSE 3000

docker-compose.yml 泊坞窗,compose.yml

version: '2'
services:
 mongo:
    container_name: "app_mongo"
    tty: true
    image: mongo:latest
    environment:
      - MONGO_DATA_DIR=/data/db
      - MONGO_LOG_DIR=/dev/null
      - MONGO_INITDB_ROOT_USERNAME=********
      - MONGO_INITDB_ROOT_PASSWORD=********
    volumes:
      - /data/dbDocker:/data/db
    ports:
        - 27017:27017
    command: "mongod --smallfiles --auth"
 rabbitmq:
    container_name: "app_rabbitmq"
    tty: true
    image: rabbitmq:management
    ports:
      - "15672:15672"
      - "15671:15671"
      - "5672:5672"
    volumes:
      - /rabbitmq/lib:/var/lib/rabbitmq
      - /rabbitmq/log:/var/log/rabbitmq
      - /rabbitmq/conf:/etc/rabbitmq/
 group:
    container_name: "app_group"
    build:
      context: .
      dockerfile: ./Account/Dockerfile
    volumes:
      - ./Account:/usr/src/app/
    ports:
      - "3000:3000"
    depends_on:
      - mongo
      - rabbitmq

During your image build, you install some things into /usr/src/app , and then you set it as the working directory. 在构建映像期间,将一些内容安装到/usr/src/app ,然后将其设置为工作目录。 Here are the lines from Dockerfile where that happens: 以下是来自Dockerfile的行:

RUN mkdir -p /usr/src
RUN mkdir -p /usr/src/app && cp -a /tmp/node_modules /usr/src/app && cp -a /tmp/package-lock.json /usr/src/app
RUN cp -a /tmp/dist/application /usr/src/app && cp -a /tmp/dist/config /usr/src/app && cp -a /tmp/dist/domain /usr/src/app && cp -a /tmp/dist/infrastructure /usr/src/app
RUN cp -a /tmp/dist/routes /usr/src/app && cp -a /tmp/dist/types /usr/src/app && cp -a /tmp/dist/webapp.js /usr/src/app && cp -a /tmp/package.json /usr/src/app
RUN chmod 755 /usr/src/app/webapp.js
RUN mkdir -p /usr/src/app/bin

COPY Account/bin /usr/src/app/bin
COPY Account/.env /usr/src/app/.env

WORKDIR /usr/src/app

After the build phase, all of that is baked into your image and ready to be used. 在构建阶段之后,所有这些都被烘焙到您的图像中并准备好使用。 But at runtime, you told docker-compose: 但是在运行时,你告诉docker-compose:

volumes:
  - ./Account:/usr/src/app/

This is an overlay mount. 这是一个叠加装载。 Whatever is built into the image at /usr/src/app is completely ignored and replaced by the contents of ./Account from the directory where docker-compose.yml is located. 无论在/usr/src/app构建到映像中的内容都被完全忽略,并替换为./Account -compose.yml所在目录中的./Account内容。

I don't know enough about your project to tell you how to properly fix this, but that's where your error is probably coming from. 我不太了解您的项目如何正确地解决这个问题,但这可能是您的错误所在。 All the work done during build to construct /usr/src/app is being undone by mounting another directory on top of it at runtime. 在构建/usr/src/app过程中完成的所有工作都是通过在运行时在其上安装另一个目录来撤消的。

If you remove that volume mount, all of /usr/src/app is still there and ready to use. 如果删除该卷安装,则所有/usr/src/app仍然存在且可以使用。 But that may have other side effects that you will need to account for to make your app do its job. 但是,这可能会产生其他副作用,您需要考虑这些副作用才能使您的应用程序发挥作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM