简体   繁体   English

docker卷不起作用

[英]docker volumes does not work

I am trying to use docker-compose only with a node container to learn and after use another container. 我试图只使用一个节点容器来学习和使用另一个容器后的docker-compose。 I have a Dockerfile of node and I share the node folder. 我有一个节点的Dockerfile,我共享节点文件夹。

Here is my structure: 这是我的结构:

 - front-end folder
   * docker-compose.yml
   - node folder
     * package.json
     * Dockerfile.json
     * server.js

My Dockerfile is the node from dockerhub: 我的Dockerfile是dockerhub的节点:

FROM buildpack-deps:jessie

# gpg keys listed at https://github.com/nodejs/node
RUN set -ex \
  && for key in \
    9554F04D7259F04124DE6B476D5A82AC7E37093B \
    94AE36675C464D64BAFA68DD7434390BDBE9B9C5 \
    0034A06D9D9B0064CE8ADF6BF1747F4AD2306D93 \
    FD3A5288F042B6850C66B31F09FE44734EB7990E \
    71DCFD284A79C3B38668286BC97EC7A07EDE3FC1 \
    DD8F2338BAE7501E3DD5AC78C273792F7D83545D \
    B9AE9905FFD7803F25714661B63B535A4C206CA9 \
    C4F0DFFF4E8C1A8236409D08E73BC641CC11F4C8 \
  ; do \
    gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; \
  done

ENV NODE_VERSION 0.12.15

RUN curl -SLO "https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64.tar.xz" \
  && curl -SLO "https://nodejs.org/dist/v$NODE_VERSION/SHASUMS256.txt.asc" \
  && gpg --batch --decrypt --output SHASUMS256.txt SHASUMS256.txt.asc \
  && grep " node-v$NODE_VERSION-linux-x64.tar.xz\$" SHASUMS256.txt | sha256sum -c - \
  && tar -xJf "node-v$NODE_VERSION-linux-x64.tar.xz" -C /usr/local --strip-components=1 \
  && rm "node-v$NODE_VERSION-linux-x64.tar.xz" SHASUMS256.txt.asc SHASUMS256.txt

CMD [ "node" ]

And my docker-compose.yml is: 而我的docker-compose.yml是:

version: '2'
services:
  node:
    build: ./node
    ports: 
        - '8080:8080'
    volumes: 
        - ./node:/usr/local/src/node

I am can run the command docker-compose run node npm install any_package , but when I run the command docker-compose run node npm install I receive this error: 我可以运行命令docker-compose run node npm install any_package ,但是当我运行命令docker-compose run node npm install any_package docker-compose run node npm install我收到此错误:

npm ERR! install Couldn't read dependencies
npm ERR! Linux 4.4.12-boot2docker
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "i"
npm ERR! node v0.12.15
npm ERR! npm  v2.15.1
npm ERR! path /package.json
npm ERR! code ENOPACKAGEJSON
npm ERR! errno -2

npm ERR! package.json ENOENT, open '/package.json'
npm ERR! package.json This is most likely not a problem with npm itself.
npm ERR! package.json npm can't find a package.json file in your current directory.

npm ERR! Please include the following file with any support request:
npm ERR!     /npm-debug.log

And when I run docker-compose run node npm install any_package , the folder node_modules should appears at my node folder. 当我运行docker-compose run node npm install any_package ,文件夹node_modules应出现在我的节点文件夹中。 Is it right? 这样对吗?

Without seeing your Dockerfile, it's not clear you are pointing your app to /usr/local/src/node. 在没有看到Dockerfile的情况下,您不清楚是否将应用程序指向/ usr / local / src / node。 So your command could be looking for files in a different location. 因此,您的命令可能正在寻找不同位置的文件。

Assuming your Dockerfile does point node to /usr/local/bin/node, then I suspect your issue is with the host volume. 假设您的Dockerfile确实将节点指向/ usr / local / bin / node,那么我怀疑您的问题与主机卷有关。 With host volumes, the two most common errors I've seen are permissions with differing uid's and trying to mount folders from the developer machine from inside the Docker host VM. 对于主机卷,我看到的两个最常见的错误是具有不同uid的权限,并尝试从Docker主机VM内部的开发人员计算机挂载文件夹。 Boot2docker leads me to think you're experiencing the latter. Boot2docker让我觉得你正在体验后者。 Debug with the following: 使用以下调试:

docker-compose run node ls -l /usr/local/bin/node

If that shows an empty directory, then it's mounting a folder from your VM rather than your Windows/MacOS. 如果它显示一个空目录,那么它将从您的VM而不是Windows / MacOS安装一个文件夹。 Use folders in c:/Users (Windows) or /Users (MacOS) to get a share that's mapped into the VM. 使用c:/ Users(Windows)或/ Users(MacOS)中的文件夹获取映射到VM的共享。

If it shows files with different permissions, you can work around that by either opening up the permissions on the Docker host (chmod 777 file) or by making the uid's and gid's inside the container mirror those of your host (this should be done before installing any applications in your Dockerfile). 如果它显示具有不同权限的文件,您可以通过打开Docker主机(chmod 777文件)上的权限或将容器内的uid和gid镜像到主机的镜像来解决这个问题(这应该在安装之前完成) Dockerfile中的任何应用程序)。

你的package.json/usr/local/src/nodenode (从/运行)期望它在/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM