Docker/bin/bash:找不到nodemon:命令

Sur*_*ran 7 makefile node.js docker nodemon docker-compose

我正在尝试将我的工作节点代码从我的主机挂载到docker容器中并使用nodemon使用docker-compose运行它.但容器似乎无法找到nodemon.注意:我的主机上没有安装node或npm.

以下是我的项目(测试)的根文件夹中的文件.(这只是草稿)

Dockerfile

FROM surenderthakran/nodejs:v4
ADD . /test
WORKDIR /test
RUN make install
CMD make run
Run Code Online (Sandbox Code Playgroud)

Makefile文件

SHELL:=/bin/bash
PWD:=$(shell pwd)
export PATH:= $(PWD)/node_modules/.bin:$(PWD)/bin:$(PATH)
DOCKER:=$(shell grep docker /proc/1/cgroup)

install:
    @echo Running make install......
    @npm config set unsafe-perm true
    @npm install

run:
    @echo Running make run......
# Check if we are inside docker container
ifdef DOCKER
    @echo We are dockerized!! :D
    @nodemon index.js
else
    @nodemon index.js
endif

.PHONY: install run
Run Code Online (Sandbox Code Playgroud)

泊坞窗,compose.yml

app:
    build: .
    command: make run
    volumes:
        - .:/test
    environment:
        NODE_ENV: dev
    ports:
        - "17883:17883"
        - "17884:17884"
Run Code Online (Sandbox Code Playgroud)

的package.json

{
  "name": "test",
  "version": "1.0.0",
  "description": "test",
  "main": "index.js",
  "dependencies": {
    "express": "^4.13.3",
    "nodemon": "^1.8.0"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "api",
    "nodejs",
    "express"
  ],
  "author": "test",
  "license": "ISC"
}
Run Code Online (Sandbox Code Playgroud)

index.js

'use strict';

var express = require('express');
Run Code Online (Sandbox Code Playgroud)

我使用构建我的图像docker-compose build.它成功完成.但是当我尝试使用它时docker-compose up,我得到:

Creating test_app_1...
Attaching to test_app_1
app_1 | Running make run......
app_1 | We are dockerized!! :D
app_1 | /bin/bash: nodemon: command not found
app_1 | make: *** [run] Error 127
test_app_1 exited with code 2
Gracefully stopping... (press Ctrl+C again to force)
Run Code Online (Sandbox Code Playgroud)

有人可以请教吗?

注意:我的基本映像的Dockerfile surenderthakran/nodejs:v4可以在这里找到:https://github.com/surenderthakran/dockerfile_nodejs/blob/master/Dockerfile

Sur*_*ran 7

问题已经解决.问题归结为我没有node_modules安装卷.

基本上,在docker-compose build正确构建图像的同时,实际代码被添加到图像并在项目根目录中创建node_modules文件夹npm install.但是docker-compose up代码正在安装在项目根目录中,它覆盖了之前添加的代码,包括新创建的node_modules文件夹.

所以作为一个解决方案,我在我的主机上安装了nodejs并在我的主机上做了一个妥协npm install.因此,当我安装的代码时,我仍然node_modules在我的项目根目录中获取了我的文件夹,因为它也是从我的主机上安装的.

不是一个非常优雅的解决方案,但由于它是一个开发设置,我已准备好妥协.在制作时,我将设置使用docker build,docker run并且不会nodemon反正使用.

如果有人能建议我一个更好的解决方案,我会很高兴.

谢谢!!


aku*_*i66 5

我相信您应该在package.json中使用预安装脚本。

因此,在脚本部分中,只需添加脚本:

 "scritpts": {
     "preinstall": "npm i nodemon -g",
     "start": "nodemon app.js",
 }
Run Code Online (Sandbox Code Playgroud)

你应该可以走了:)