首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Docker-compose: docker卷中不存在node_modules

Docker-compose: docker卷中不存在node_modules
EN

Stack Overflow用户
提问于 2021-07-07 14:49:32
回答 1查看 153关注 0票数 0

我正在使用docker-compose来安装和启动应用程序。但在这里,应用程序无法使用node_modules来运行它。我已经检查了这个Docker-compose: node_modules not present in a volume after npm install succeeds,但它对我没有帮助。

这是我的docker-compose.yml

代码语言:javascript
复制
version: "2"

services:
    neo4j:
        container_name: ms_neo4j
        image: neo4j:latest
        environment:
            - NEO4J_AUTH=none
        ports:
            - "7474:7474"
            - "7687:7687"

    mongo:
        container_name: ms_mongo
        image: mongo:latest
        ports:
            - "27017:27017"
        volumes:
            - "./mongo/db:/data/db"

    nginx:
        build: ./nginx
        container_name: ms_nginx
        links:
            - petstore
            - users
        ports:
            - "80:80"

    petstore:
        build: ./petstore
        container_name: ms_petstore
        environment:
            - loglevel=none
        links:
            - "mongo:mongo"
        volumes:
            - "./petstore:/src/app"
            - node_modules:/petstore/node_modules
        working_dir: "/src/app"
        ports:
            - "8080:8080"
            - "5858:5858"
        # command: npm run start
        command: npm run start:dev

    users:
        build: ./users
        container_name: ms_users
        links:
            - neo4j
        volumes:
            - "./users:/src/app"
            - node_modules:/users/node_modules
        working_dir: "/src/app"
        command: npm start

volumes:
    node_modules:

用户/Dockerfile

代码语言:javascript
复制
FROM node:latest

RUN npm i -g rimraf typescript

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY package.json /usr/src/app/package.json
RUN npm install

COPY . /usr/src/app

petstore/Dockerfile

代码语言:javascript
复制
FROM node:latest

RUN npm i -g nodemon node-inspector@0.7.5 npm-run-all rimraf typescript

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY package.json /usr/src/app/package.json
RUN npm install

COPY . /usr/src/app

获取这些错误

代码语言:javascript
复制
src/config/Application.ts(4,25): error TS2307: Cannot find module 'config' or its corresponding type declarations.

src/config/Mongo.ts(1,33): error TS2307: Cannot find module 'mongodb' or its corresponding type declarations.

src/config/Mongo.ts(2,25): error TS2307: Cannot find module 'config' or its corresponding type declarations.

src/config/Mongo.ts(3,25): error TS2307: Cannot find module 'typedi' or its corresponding type declarations.

src/services/PetSwagger.ts(2,25): error TS2307: Cannot find module 'typedi' or its corresponding type declarations.

src/services/PetSwagger.ts(3,25): error TS2307: Cannot find module 'config' or its corresponding type declarations.

src/services/MongoService.ts(1,33): error TS2307: Cannot find module 'typedi' or its corresponding type declarations.

src/services/PetService.ts(1,33): error TS2307: Cannot find module 'typedi' or its corresponding type declarations.

src/controllers/PetStoreController.ts(1,40): error TS2307: Cannot find module 'routing-controllers' or its corresponding type declarations.

src/controllers/PetStoreController.ts(2,24): error TS2307: Cannot find module 'typedi' or its corresponding type declarations.

src/controllers/PetStoreController.ts(3,40): error TS2307: Cannot find module 'mongodb' or its corresponding type declarations.

src/subscribers/PetSubscriber.ts(1,37): error TS2307: Cannot find module 'event-dispatch' or its corresponding type declarations.

src/tasks/swagger.ts(2,21): error TS2307: Cannot find module 'fs' or its corresponding type declarations.

src/tasks/swagger.ts(3,23): error TS2307: Cannot find module 'glob' or its corresponding type declarations.

src/tasks/swagger.ts(7,38): error TS2580: Cannot find name 'require'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.

有谁知道如何解决这个问题吗?

EN

回答 1

Stack Overflow用户

发布于 2021-07-07 16:53:38

我注意到与文件中的路径相关的一些事情看起来很可疑:

在docker-compose文件中,您使用/src/app作为工作目录,但在docker文件中,您使用的是/usr/src/app

Fix:在两个文件中使用相同的工作目录,从您的docker-compose文件中完全省略working_dir,并修复您的docker-compose文件中的路径。通过使用WORKDIR命令,您的Dockerfile可以简化为

代码语言:javascript
复制
FROM node:latest

RUN npm i -g rimraf typescript

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# notice how I am omitting the /usr/src/app since you already have declared it as the workdir and just copying into the current directory `.`
COPY package.json .
RUN npm install
COPY . .

您的docker-compose文件应该像这样反映这些路径更改:

代码语言:javascript
复制
    petstore:
        build: ./petstore
        container_name: ms_petstore
        environment:
            - loglevel=none
        links:
            - "mongo:mongo"
        volumes:
            - "./petstore:/usr/src/app"
            - ./node_modules:/usr/src/app/node_modules
        ports:
            - "8080:8080"
            - "5858:5858"
        # command: npm run start
        command: npm run start:dev
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68281301

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档