简体   繁体   English

错误:使用 docker-compose 时找不到模块“/usr/src/app/nodemon”

[英]Error: Cannot find module '/usr/src/app/nodemon' while using docker-compose

I am trying to create a docker-compose.yml to be able to run my crud-admin panel using both my backend and frontend API together in the same network with docker.我正在尝试创建一个 docker-compose.yml 以便能够使用我的后端和前端 API 在同一个网络中与 docker 一起运行我的 crud-admin 面板。 This is the first time i am using an "docker-compose" file to deploy with docker.这是我第一次使用“docker-compose”文件与 docker 一起部署。 Usually i deploy each API itself using a normal Dockerfile.通常我使用普通的 Dockerfile 部署每个 API 本身。

when running the command: docker-compose up --build i get the error:运行命令时: docker-compose up --build我得到错误:

Error: Cannot find module '/usr/src/app/nodemon'

I have:我有:

  • tried with sudo as well even though my user is added to docker group即使我的用户已添加到 docker 组,也尝试使用 sudo
  • my user has sudo privileges我的用户有 sudo 权限
  • I have tried an update我试过更新

before posting my Dockerfile and docker compose, here is my folder structure:在发布我的 Dockerfile 和 docker 之前,这是我的文件夹结构:

the-admin-panel
│
├──frontend(reactjs/nextjs)
│  ├─ .next
│  ├─ components
│  ├─ content
│  ├─ helper
│  ├─ pages
│  ├─ public
│  ├─ services
│  ├─ static
│  ├─ next.config
│  ├─ next-env.d
│  ├─ package.json
│  ├─ package-lock.json
│  ├─ tsconfig.json
│  ├─ Dockerfile
│  ├─ .dockerignore
│  
│
├──backend(nodejs)
│  ├─ api
│  ├─ startup
│  ├─ middleware
│  ├─ models
│  ├─ server.js
│  ├─ package.json
│  ├─ package-lock.json
│  ├─ .env
│  ├─ Dockerfile
│  ├─ .dockerignore
│  
├──docker-compose.yml

Currently, the frontend is not finished yet, so for now im only trying to deploy the backend, so im focusing on this part for now.目前前端还没有完成,所以我现在只尝试部署后端,所以我现在专注于这部分。

Here is the Dockerfile for my backend:这是我后端的 Dockerfile:

FROM node:13-alpine

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 5000
CMD [ "nodemon", "server.js"]

As you can see, im trying to run it with "nodemon".如您所见,我试图用“nodemon”运行它。

And finally, here is my docker-compose.yml file(notice that i have commented all the lines within the frontend section as the frontend api is not finished yet)最后,这是我的 docker-compose.yml 文件(请注意,我已将前端部分中的所有行注释为前端 api 尚未完成)

version: '3'
services:

#set up the frontend
  #frontend:
  #  build:
  #    context: frontend
  #    dockerfile: Dockerfile
  #  image: frontend
  #    - $PWD:/usr/src/app
  #    - /usr/src/app/node_modules
  #  restart: always
  #  ports:
  #    - "3000:3000"
  #  networks:
  #    testnetwork

#set up the backend
  backend:
    build:
      context: backend
      dockerfile: Dockerfile
    image: nodejs-restapi
    #- $PWD:/usr/src/app
    #- /usr/src/app/node_modules

    restart: always
    ports:
      - "5000:5000"
    networks:
            testnetwork:


# Set up shared network
networks:
  testnetwork:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 10.0.0.0/8

in case if needed, here is my package.json file:如果需要,这是我的 package.json 文件:

{
  "name": "..",
  "version": "1.0.0",
  "description": "..",
  "main": "server.js",
  "scripts": {
    "start": "nodemon server.js"
  },
  "author": "tim",
  "license": "ISC",
  "dependencies": {
    "bcrypt": "^5.0.1",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "fastest-validator": "^1.10.0",
    "helmet": "^4.4.1",
    "jsonwebtoken": "^8.5.1",
    "lodash": "^4.17.21",
    "mysql": "^2.18.1"
  },
  "devDependencies": {
    "nodemon": "^2.0.7"
  }
}

Could someone help me solve this nodemon issue?有人可以帮我解决这个 nodemon 问题吗?

I would simply suggest not using nodemon inside a docker image.我只是建议不要在 docker 图像中使用nodemon nodemon is a tool for development, and I don't think there's much use for running it in production/any other environment you deploy to. nodemon是一种开发工具,我认为在生产/您部署到的任何其他环境中运行它没有多大用处。

simply change the last line in your backend's Dockerfile to CMD [ "node", "server.js"]只需将后端Dockerfile中的最后一行更改为CMD [ "node", "server.js"]

I believe this is just a trial.我相信这只是一个试验。 So, without much production-grade adjustments, I would suggest you try changing your dockerfile.因此,如果没有太多的生产级调整,我建议您尝试更改您的 dockerfile。

FROM node:13-alpine

COPY . /usr/src/app
WORKDIR /usr/src/app

RUN npm install
RUN npm install -g nodemon

EXPOSE 5000
CMD [ "nodemon", "server.js"]

Ok so thanks to Charlie, the solution was a small change in the Dockerfile where "RUN npm install -g nodemon" was added.好的,多亏了查理,解决方案是 Dockerfile 中的一个小改动,其中添加了“RUN npm install -g nodemon”。

FROM node:13-alpine

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

RUN npm install -g nodemon  #this was needed in order to make it work

EXPOSE 5000

CMD [ "nodemon", "server.js"] 

暂无
暂无

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

相关问题 在 docker 容器上运行 nodejs 应用程序会给出“错误:找不到模块'/usr/src/app/nodemon'” - Running a nodejs app on a docker container gives “ Error: Cannot find module '/usr/src/app/nodemon' ” docker-compose:找不到模块“typeorm” - docker-compose: Cannot find module 'typeorm' docker ubuntu 中的 nodejs 找不到模块 /usr/src/app/index.js - nodejs in docker ubuntu cannot find module /usr/src/app/index.js Docker-compose up:没有这样的文件或目录,打开'/usr/src/app/package.json' - Docker-compose up : no such file or directory, open '/usr/src/app/package.json' npm ENOENT:没有这样的文件或目录,打开“/usr/app/package.json”。 在尝试使用 docker-compose 与 docker 共享本地空间时 - npm ENOENT: no such file or directory, open '/usr/app/package.json'. While trying to share local space with docker using docker-compose Docker:错误:找不到模块/app/src/myapp.js - Docker: Error: Cannot find module /app/src/myapp.js 找不到模块“express”-docker-compose nodejs 服务 - Cannot find module "express" - docker-compose nodejs service docker-compose up 在 nodejs-application 上失败:“错误:找不到模块” - docker-compose up failing on nodejs-application: "Error: Cannot find module" 错误:找不到模块 'nuxt-i18n' 和 docker-compose up 无法启动 - Error: Cannot find module 'nuxt-i18n' and docker-compose up does not start 获取错误:运行 docker-compose 时未找到 nodemon - Getting error: nodemon not found when running docker-compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM