无法在主机上创建node_modules文件夹并将主机文件夹装载到容器

dav*_*lee 5 mount docker dockerfile docker-compose

我的dockerfile将运行npm install,但是docker-compose build在我的主机文件夹中的文件夹node_modules即使在看到构建之后仍然为空时.如何获取文件夹并在主机中反映出来?

这是docker-compose.yml:

这是我的docker-compose

version: "2"

volumes: 
  mongostorage:

services:
  app:
    build: ./app
    ports:
      - "3000"
    links:
      - mongo
      - redis
    command: node ./bin/www
  nginx:
    build: ./nginx
    ports:
      - "80:80"
    links:
      - app:app
  mongo:
    image: mongo:latest
    environment:
      - MONGO_DATA_DIR=/data/db
    volumes:
      - mongostorage:/data/db
    ports:
      - "27017:27017"
  redis:
    image: redis
    volumes:
      - ./data/redis/db:/data/db
    ports:
      - "6379:6379"
Run Code Online (Sandbox Code Playgroud)

这是应用程序中的dockerfile

FROM node:6.3

RUN mkdir -p /var/www/app

WORKDIR /var/www/app

VOLUME /var/www/app

COPY . /var/www/app    

COPY package.json /var/www/app

RUN npm install
Run Code Online (Sandbox Code Playgroud)

的package.json

{
  "name": "app",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "body-parser": "^1.13.3",
    "cookie-parser": "~1.3.5",
    "debug": "~2.2.0",
    "express": "~4.13.1",
    "helmet": "^3.0.0",
    "jade": "~1.11.0",
    "redis": "^2.6.2",
    "serve-favicon": "~2.3.0"
  },
  "devDependencies": {
    "gulp": "^3.9.1",
    "gulp-autoprefixer": "^3.1.1",
    "gulp-complexity": "^0.3.2",
    "gulp-concat": "^2.6.1",
    "gulp-cssnano": "^2.1.2",
    "gulp-less": "^3.3.0",
    "gulp-uglify": "^1.5.4",
    "gulp-watch": "^4.3.11",
    "strip-debug": "^1.1.1",
    "util": "^0.10.3"
  }
}
Run Code Online (Sandbox Code Playgroud)

docker logsfor app容器在docker-compose up错误后返回:

module.js:442
    throw err;
    ^

Error: Cannot find module 'dotenv'
    at Function.Module._resolveFilename (module.js:440:15)
    at Function.Module._load (module.js:388:25)
    at Module.require (module.js:468:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/var/www/nodeapp/app.js:3:1)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
Run Code Online (Sandbox Code Playgroud)

lec*_*tor 1

该 VOLUME 指令将您的应用程序目录安装到主机,这将隐藏您放入容器中的任何内容。我实际上很惊讶你看到了 node_modules 目录。您也不需要说明,COPY package.json因为他们COPY .应该处理好这一点。尝试使用以下 Dockerfile 代替..

\n\n
FROM node:6.3\nRUN mkdir -p /var/www/app\nWORKDIR /var/www/app\nCOPY . /var/www/app    \nRUN npm install\n
Run Code Online (Sandbox Code Playgroud)\n\n

使用您提供的信息,这对我来说效果很好。\ndocker-compose build从包含 Dockerfile 的目录运行可以成功构建映像。

\n\n
$ tree\n.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 app\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Dockerfile\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 package.json\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 docker-compose.yml\n\n1 directory, 3 files\n\n$ cat docker-compose.yml\nversion: "2"\nservices:\n  app:\n    build: ./app\n    ports:\n      - "3000"\n    command: node ./bin/www\n\n$ cat app/Dockerfile\nFROM node:6.3\nRUN mkdir -p /var/www/app\nWORKDIR /var/www/app\nCOPY . /var/www/app\nRUN npm install\n\n$ cat app/package.json\n{\n  "name": "app",\n  "version": "0.0.0",\n  "private": true,\n  "scripts": {\n    "start": "node app.js"\n  },\n  "dependencies": {\n    "body-parser": "^1.13.3",\n    "cookie-parser": "~1.3.5",\n    "debug": "~2.2.0",\n    "express": "~4.13.1",\n    "helmet": "^3.0.0",\n    "jade": "~1.11.0",\n    "redis": "^2.6.2",\n    "serve-favicon": "~2.3.0"\n  },\n  "devDependencies": {\n    "gulp": "^3.9.1",\n    "gulp-autoprefixer": "^3.1.1",\n    "gulp-complexity": "^0.3.2",\n    "gulp-concat": "^2.6.1",\n    "gulp-cssnano": "^2.1.2",\n    "gulp-less": "^3.3.0",\n    "gulp-uglify": "^1.5.4",\n    "gulp-watch": "^4.3.11",\n    "strip-debug": "^1.1.1",\n    "util": "^0.10.3"\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

运行构建..

\n\n
$ docker-compose build\nBuilding app\nStep 1/5 : FROM node:6.3\n6.3: Pulling from library/node\n357ea8c3d80b: Pull complete\n...\n646e6da4bf07: Pull complete\nDigest: sha256:cde2bd38dbfec5e6e2981ec7a05056c734148fcd4bff088e143f11b2bafe3b64\nStatus: Downloaded newer image for node:6.3\n---> 0d9089853221\nStep 2/5 : RUN mkdir -p /var/www/app\n---> Running in 018dd9b28c57\n---> 18cc43628367\nRemoving intermediate container 018dd9b28c57\nStep 3/5 : WORKDIR /var/www/app\n---> b8fa2b1a2624\nRemoving intermediate container ba1ad506cab2\nStep 4/5 : COPY . /var/www/app\n---> 3f37c76acdc4\nStep 5/5 : RUN npm install\n---> Running in dd87c26c2546\nnpm info it worked if it ends with ok\nnpm info using npm@3.10.3\nnpm info using node@v6.3.1\nnpm info attempt registry request try #1 at 11:39:41 AM\nnpm http request GET https://registry.npmjs.org/body-parser\n...\nnpm info lifecycle util@0.10.3~postinstall: util@0.10.3\nnpm info lifecycle app@0.0.0~preinstall: app@0.0.0\nnpm info linkStuff app@0.0.0\nnpm info lifecycle app@0.0.0~install: app@0.0.0\nnpm info lifecycle app@0.0.0~postinstall: app@0.0.0\nnpm info lifecycle app@0.0.0~prepublish: app@0.0.0\napp@0.0.0 /var/www/app\n+-- body-parser@1.18.2\n| +-- bytes@3.0.0\n...\nnpm WARN optional Skipping failed optional dependency /chokidar/fsevents:\nnpm WARN notsup Not compatible with your operating system or architecture: fsevents@1.1.2\nnpm info ok\n---> d1bfc665f54c\nRemoving intermediate container dd87c26c2546\nSuccessfully built d1bfc665f54c\nSuccessfully tagged so47173020_app:latest\n
Run Code Online (Sandbox Code Playgroud)\n\n

如下所示修改 Dockerfile 命令我可以运行docker-compose up app并查看安装在 node_modules 目录中的所有模块。

\n\n
version: "2"\nservices:\n  app:\n    build: ./app\n    ports:\n      - "3000"\n    command: ls -la node_modules\n
Run Code Online (Sandbox Code Playgroud)\n\n

输出docker-compose up app:

\n\n
Creating so47173020_app_1 ...\nCreating so47173020_app_1 ... done\nAttaching to so47173020_app_1\napp_1  | total 2004\napp_1  | drwxr-xr-x 497 root root 20480 Nov 10 11:40 .\napp_1  | drwxr-xr-x   1 root root  4096 Nov 10 11:40 ..\napp_1  | drwxr-xr-x   2 root root  4096 Nov 10 11:40 .bin\napp_1  | drwxr-xr-x   2 root root  4096 Nov 10 11:40 accepts\napp_1  | drwxr-xr-x   4 root root  4096 Nov 10 11:40 accord\n...\napp_1  | drwxr-xr-x   3 root root  4096 Nov 10 11:40 x-xss-protection\napp_1  | drwxr-xr-x   2 root root  4096 Nov 10 11:40 xtend\napp_1  | drwxr-xr-x   3 root root  4096 Nov 10 11:40 yargs\nso47173020_app_1 exited with code 0\n
Run Code Online (Sandbox Code Playgroud)\n