【问题标题】:Docker: mounting volume and run node appsDocker:安装卷并运行节点应用程序
【发布时间】:2016-05-08 16:36:23
【问题描述】:

我第一次尝试将 docker 用于开发目的。我有一个节点应用程序。我想从 docker 运行应用程序。这是我的 dockerfile:

FROM node:6.0.0-slim
MAINTAINER pyprism

# Prepare app directory
RUN mkdir -p /src/

# Install dependencies
WORKDIR /src/

VOLUME .:/src/

RUN npm install

# Build the app
# RUN npm build

# Expose the app port
EXPOSE 8000

# Start the app
CMD npm start

和 docker-compose 文件:

web:
  build: .
  ports:
    - '8000:8000'

但是当我运行docker-compose up 时,我得到了这个错误:

Building web
Step 1 : FROM node:6.0.0-slim
 ---> 7bf50b1ad9da
Step 2 : MAINTAINER pyprism
 ---> Running in d1defd389fe6
 ---> b684686c614d
Removing intermediate container d1defd389fe6
Step 3 : RUN mkdir -p /src/
 ---> Running in 36b64560f88f
 ---> 8eb6847d67e4
Removing intermediate container 36b64560f88f
Step 4 : WORKDIR /src/
 ---> Running in 00d4c1fd2cf5
 ---> 88a54e6af176
Removing intermediate container 00d4c1fd2cf5
Step 5 : VOLUME .:/src/
 ---> Running in dc0e9d9d973a
 ---> b558f03ce63c
Removing intermediate container dc0e9d9d973a
Step 6 : RUN npm install
 ---> Running in 09445786b71e
npm info it worked if it ends with ok
npm info using npm@3.8.6
npm info using node@v6.0.0
npm info lifecycle undefined~preinstall: undefined
npm info linkStuff !invalid#1
npm info lifecycle undefined~install: undefined
npm info lifecycle undefined~postinstall: undefined
npm info lifecycle undefined~prepublish: undefined
npm WARN enoent ENOENT: no such file or directory, open '/src/package.json'
npm WARN src No description
npm WARN src No repository field.
npm WARN src No README data
npm WARN src No license field.
npm info ok 
 ---> 8c544294e6c5
Removing intermediate container 09445786b71e
Step 7 : EXPOSE 8000
 ---> Running in 965e192bc67e
 ---> daaf52fac6ca
Removing intermediate container 965e192bc67e
Step 8 : CMD npm start
 ---> Running in 890549e3aea7
 ---> 19a3dc786cee
Removing intermediate container 890549e3aea7
Successfully built 19a3dc786cee
WARNING: Image for service web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating redux_web_1
Attaching to redux_web_1
web_1  | npm info it worked if it ends with ok
web_1  | npm info using npm@3.8.6
web_1  | npm info using node@v6.0.0
web_1  | npm ERR! Linux 4.4.0-22-generic
web_1  | npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "start"
web_1  | npm ERR! node v6.0.0
web_1  | npm ERR! npm  v3.8.6
web_1  | npm ERR! path /src/package.json
web_1  | npm ERR! code ENOENT
web_1  | npm ERR! errno -2
web_1  | npm ERR! syscall open
web_1  | 
web_1  | npm ERR! enoent ENOENT: no such file or directory, open '/src/package.json'
web_1  | npm ERR! enoent ENOENT: no such file or directory, open '/src/package.json'
web_1  | npm ERR! enoent This is most likely not a problem with npm itself
web_1  | npm ERR! enoent and is related to npm not being able to find a file.
web_1  | npm ERR! enoent 
web_1  | 
web_1  | npm ERR! Please include the following file with any support request:
web_1  | npm ERR!     /src/npm-debug.log
redux_web_1 exited with code 254

【问题讨论】:

  • 为什么要使用卷?您应该使用COPYADD 命令来反映您的更改。在更新应用时将卷用于您想要保留的数据。
  • 我尝试使用 ADD . /src/ 。它没有反映我的变化。
  • 查看这个 repo:github.com/b00giZm/docker-compose-nodejs-examples/tree/master/… 并查看 Dockerfiledocker-compose.yml

标签: docker docker-compose dockerfile


【解决方案1】:

这不是在 dockerfile 中使用指令 VOLUME 的正确方法。 正如文档所说“VOLUME 指令创建一个具有指定名称的挂载点,并将其标记为保存来自本机主机或其他容器的外部挂载卷”,我认为这不是您想要做的。

您不需要指定 VOLUME 指令,也不需要创建 src 目录。修改 Dockerfile 如下:

FROM node:6.0.0-slim
MAINTAINER pyprism

# Install dependencies
WORKDIR /src/

RUN npm install

# Expose the app port
EXPOSE 8000

# Start the app
CMD npm start

现在您可以导航到 Dockerfile 目录并构建映像:

$docker build -t node .

然后使用-v选项运行容器来挂载你的本地目录,如下:

 $docker run -p 8000:8000 -v path/to/code/dir:/src/ <image-name>

这会将你的代码目录挂载到 /src 中。

如果您想使用 docker compose,只需在 docker-compose.yml 文件中指定卷:

 web:
  build: .
  volumes:
    - path/to/code/dir:/src/ 
  ports:
   - '8000:8000'

【讨论】:

    【解决方案2】:

    您不能在 dockerfile 中执行此操作 VOLUME .:/src/。在 dockerfile 中指定主机挂载点是不合法的语法,因为它会使其依赖于主机。将卷映射定义移动到您的 docker-compose.yml 文件中。

    【讨论】:

      【解决方案3】:

      我添加此答案以防万一接受的解决方案不适用于其他人,因为它不适用于我。虽然我承认我不是 docker 也不是节点专家。

      这就是我所拥有的: Dockerfile

      FROM node
      
      WORKDIR /usr/src/
      
      RUN npm install
      
      EXPOSE 4200
      
      CMD npm start
      

      docker-compose.yml

      version: "3.2"
      services:
        frontend:
          ports:
            - "4200:4200"
          volumes:
            - ./frontend/:/usr/src
      

      为了让它对我有用,我必须从 Dockerfile 中删除 RUN npm install 并将 cmd 更改为 CMD npm install &amp;&amp; npm start

      我认为问题在于执行 RUN 时未安装该卷,而仅在 CMD 之前安装了该卷。另请注意,您必须在编辑 Dockerfile 后重新构建映像。

      这确实对我有用,但同样,我不是专家。因此,如果有人有任何想法或想法,请随时在 cmets 中解释或编辑以改进答案。

      【讨论】:

        猜你喜欢
        • 2023-03-12
        • 2023-03-16
        • 1970-01-01
        • 2020-10-11
        • 1970-01-01
        • 1970-01-01
        • 2020-03-10
        • 1970-01-01
        相关资源
        最近更新 更多