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

前端之家收集整理的这篇文章主要介绍了docker – 无法在主机上创建node_modules文件夹并将主机文件夹装载到容器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我的dockerfile将运行npm install,但是在docker-compose构建之后,即使在看到构建之后,我的主机文件夹中的文件夹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"

这是应用程序中的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

的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"
  }
}

在docker-compose错误之后,应用程序容器的docker日志返回:

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.
最佳答案
开始新鲜..

以下是,我相信,你要求的是什么,但我不确定这是你想要的.

您尝试解决的实际用例是什么?

如果要开发您的应用程序,修改本地系统上的文件并将它们反映在docker容器中,这不是这样做的方法.

我使用npm模块作为保持容器运行的简单方法.

我使用了一个命名卷来简化操作.卷数据在docker-compose中定义并安装在/ var / www / app上.

fyi,你也可以尝试将原始Dockerfile中的VOLUME指令移到底部,如https://docs.docker.com/engine/reference/builder/#volume所示:

Changing the volume from within the Dockerfile: If any build steps change the data within the volume after it has been declared,those changes will be discarded.

但这仍然只定义容器中的卷装入点,您仍需要在运行时将其装载到主机上.

应用文件

$tree
.
├── app
│   ├── Dockerfile
│   └── package.json
└── docker-compose.yml

1 directory,3 files

$cat app/Dockerfile
FROM node:6.3
RUN mkdir -p /var/www/app
WORKDIR /var/www/app
COPY . /var/www/app
RUN npm install

$cat app/package.json
{
  "name": "app","scripts": {
    "start": "serve"
  },"dependencies": {
    "serve": "*"
  }
}

$cat docker-compose.yml
version: "2"

services:
  app:
    build: ./app
    ports:
      - "3000"
    command: npm start
    volumes:
      - data:/var/www/app
volumes:
  data:

构建应用程序:

$docker-compose build app
Building app
Step 1/5 : FROM node:6.3
---> 0d9089853221
Step 2/5 : RUN mkdir -p /var/www/app
---> Using cache
---> 18cc43628367
Step 3/5 : WORKDIR /var/www/app
---> Using cache
---> b8fa2b1a2624
Step 4/5 : COPY . /var/www/app
---> de38a6c3f784
Step 5/5 : RUN npm install
---> Running in 5a035f687de1
npm info it worked if it ends with ok
npm info using npm@3.10.3
npm info using node@v6.3.1
npm info attempt registry request try #1 at 1:19:21 PM
npm http request GET https://registry.npmjs.org/serve
...
npm info ok
---> 76b99c707ac1
Removing intermediate container 5a035f687de1
Successfully built 76b99c707ac1
Successfully tagged 47173020_app:latest

打开应用程序

$docker-compose up -d app
Recreating 47173020_app_1 ...
Recreating 47173020_app_1 ... done

检查应用程序日志

$docker-compose logs app
Attaching to 47173020_app_1
app_1  | npm info it worked if it ends with ok
app_1  | npm info using npm@3.10.3
app_1  | npm info using node@v6.3.1
app_1  | npm info lifecycle app@0.0.0~prestart: app@0.0.0
app_1  | npm info lifecycle app@0.0.0~start: app@0.0.0
app_1  |
app_1  | > app@0.0.0 start /var/www/app
app_1  | > serve
app_1  |
app_1  | serve: Running on port 5000

现在,您在主机上查找的卷位于作为本地计算机上的vm运行的docker主机上,并且只能从正在运行的容器中访问.

我从Inspecting Docker Volumes on a Mac/Windows the Easy Way开始采取以下内容,您可以参考更多细节.

列出泊坞窗卷

$docker volume ls
local               47173020_data

检查音量以获得它的安装点

$docker volume inspect 47173020_data
[
    {
        "CreatedAt": "2017-11-13T13:15:59Z","Driver": "local","Labels": null,"Mountpoint": "/var/lib/docker/volumes/47173020_data/_data","Name": "47173020_data","Options": {},"Scope": "local"
    }
]

启动一个简单的容器,在主机根目录下安装“/ docker”,并列出卷中的文件,这些文件是由Dockerfile复制和创建的文件

$docker run --rm -it -v /:/docker alpine:edge ls -l /docker/var/lib/docker/volumes/47173020_data/_data
total 12
-rw-r--r--    1 root     root           119 Nov 13 12:30 Dockerfile
drwxr-xr-x  153 root     root          4096 Nov 13 13:14 node_modules
-rw-r--r--    1 root     root           144 Nov 13 13:03 package.json
原文链接:https://www.f2er.com/docker/435787.html

猜你在找的Docker相关文章