无法在主机上创建node_modules文件夹并将主机文件夹安装到容器 [英] Unable to create node_modules folder at host and mount host folder to container

查看:126
本文介绍了无法在主机上创建node_modules文件夹并将主机文件夹安装到容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My dockerfile will run npm install, however after docker-compose build the folder node_modules in my host folder remains empty even after seeing building. How can i get the folder up and reflected in the host too?

这是docker-compose.yml:

This is docker-compose.yml:

这是我的docker-compose

This is my 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

This is dockerfile in app

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

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 logsdocker-compose up错误后返回:

docker logs for the app container returns after docker-compose up error:

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)

推荐答案

从头开始.

我相信以下是您要的内容,但我不确定这是您想要的内容.

The following is, I believe, what you are asking for, but I'm not sure it's what you want.

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

What is the actual use case you are trying to address?

如果要能够开发您的应用程序,请在本地系统上修改文件并将其反映在docker容器中,这不是做到这一点的方法.

If it is to be able to develop your app, modifying files on your local system and have them reflected in the docker container this is not the way to do it..

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

I have used the npm module serve as an easy way to keep the container running.

我使用命名卷使事情变得更容易.卷data是在docker-compose中定义的,并已安装在/var/www/app上.

I have used a named volume to make things easier. The volume data is defined in docker-compose and mounted on /var/www/app.

fyi,您还可以尝试将原始Dockerfile中的VOLUME指令移至底部,如

fyi, you could also try moving the VOLUME instruction in your original Dockerfile to the bottom as at 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.

但这仍然仅在容器中定义了卷安装点,您仍然需要在运行时将其安装在主机上.

But this still only defines the volume mount point in the container, you still need to mount it on the host at run time.

应用文件:

$ 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",
  "version": "0.0.0",
  "private": true,
  "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 主机上,并且只能从正在运行的容器中访问.

Now the volume you are looking for on the host is on the docker host running as a vm on your local machine and can only be accessed from a running container.

我从在Mac/Windows上轻松检查Docker卷,有关更多详细信息,可以参考

I have taken the following from Inspecting Docker Volumes on a Mac/Windows the Easy Way which you can refer to for more detail.

列出docker卷

$ 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复制和创建的.

Start up a simple container, mount "/docker" on the host root, and list the files in the volume which are the ones copied and created by the 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

这篇关于无法在主机上创建node_modules文件夹并将主机文件夹安装到容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆