【问题标题】:Sharing files between two containers在两个容器之间共享文件
【发布时间】:2018-05-26 12:30:44
【问题描述】:

几个小时以来,我一直在为 docker compose 苦苦挣扎。我正在构建角度应用程序。我可以看到 dist 目录中的文件。现在我想与 nginx 容器共享这些文件。我认为共享卷会做到这一点。但是当我添加

services:
    client:
       volumes: 
            - static:/app/client/dist
    nginx:
          volumes: 
            - static:share/user/nginx/html

volumes:
   static:

试试docker-compose up --build 我收到了这个错误

client_1  | EBUSY: resource busy or locked, rmdir '/app/client/dist'
client_1  | Error: EBUSY: resource busy or locked, rmdir '/app/client/dist'
client_1  |     at Object.fs.rmdirSync (fs.js:863:18)
client_1  |     at rmdirSync (/app/client/node_modules/fs-extra/lib/remove/rimraf.js:276:13)
client_1  |     at Object.rimrafSync [as removeSync] (/app/client/node_modules/fs-extra/lib/remove/rimraf.js:252:7)
client_1  |     at Class.run (/app/client/node_modules/@angular/cli/tasks/build.js:29:16)
client_1  |     at Class.run (/app/client/node_modules/@angular/cli/commands/build.js:250:40)
client_1  |     at resolve (/app/client/node_modules/@angular/cli/ember-cli/lib/models/command.js:261:20)
client_1  |     at new Promise (<anonymous>)
client_1  |     at Class.validateAndRun (/app/client/node_modules/@angular/cli/ember-cli/lib/models/command.js:240:12)
client_1  |     at Promise.resolve.then.then (/app/client/node_modules/@angular/cli/ember-cli/lib/cli/cli.js:140:24)
client_1  |     at <anonymous>
client_1  | npm ERR! code ELIFECYCLE
client_1  | npm ERR! errno 1
client_1  | npm ERR! app@0.0.0 build: `ng build --prod`
client_1  | npm ERR! Exit status 1
client_1  | npm ERR! 
client_1  | npm ERR! Failed at the app@0.0.0 build-prod script.
client_1  | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

非常感谢任何帮助

【问题讨论】:

  • 听起来像是权限问题。
  • 是的,我想是的
  • 你解决过这个问题吗? @AbdelrhmanHussien 我有同样的问题。每当您将卷附加到节点生成的目录时,就会出现此问题
  • 我已经解决了这个问题,是的,在我这边,目录是在此过程中生成的,所以当我尝试将卷附加到它时。它还不存在。因此,我查看了角度配置以使该目录永久化并为我解决了这个问题。希望对您有所帮助
  • @AbdelrhmanHussien,您是否碰巧在 angular.json 中将 deleteOutputPath 设置为 false 来做到这一点?

标签: docker docker-compose docker-volume


【解决方案1】:

我相信,正如错误所暗示的,这是一个死锁情况。您的 docker-compose 文件有 2 个服务大约同时启动,如果不是,则同时启动。它们都对 Docker 卷有某种保留(命名为“静态”)。当 Angular 执行 ng build 时,默认情况下,--deleteOutputPath 设置为 true。当它尝试删除输出目录时,会出现您收到的错误消息。

如果将deleteOutputPath 设置为false,则应解决该问题。也许这足以满足您的需求。如果没有,作为替代方案,将 --outputPath 设置为项目目录中的临时目录,在 Angular 构建之后,将内容移动到 Docker 卷中。如果临时目录路径是out/dist 并且卷映射到dist,则可以使用:

ng build && cp -rf ./out/dist/* ./dist

但是,该替代解决方案实际上只是解决该问题。需要注意的是,docker-compose depends_on 键在这种情况下无济于事,因为它只是表示依赖关系,与依赖服务的“就绪”无关。

还要注意,执行docker volume rm &lt;name&gt; 作为解决方案不会产生任何后果。请记住,当一个服务试图删除它时,这两个服务都会保留该卷。

只是一个想法,尚未测试,因为另一种替代解决方案是删除输出路径中的内容。并将deleteOutputPath 设置为false,因为Angular 似乎正在尝试删除目录本身。

更新:

所以删除输出路径中的内容似乎可行!正如我提到的,将deleteOutputPath 设置为false。在你的package.json 文件中,在脚本对象中,有类似这样的内容:

{
  "scripts": {
    "build:production": "rm -rf ./dist/* && ng build --configuration production",
  }
}

【讨论】:

  • 这应该是被接受的答案@Abdelrhman Hussien。我遇到了同样的问题,经过一番脑筋急转弯,我读到了这篇文章。 @marckassay 是对的,而且是正确的。这是非常合乎逻辑的,ng 尝试删除输出文件夹。但它不能,因为它已安装。您也可以通过更深一层来解决此问题(只需创建一个子文件夹,如 /dist/build)。
【解决方案2】:

您可以尝试在不使用命名卷的情况下解决它:

services:
    client:
       volumes: 
            - ./static-content:client/app/dist
    nginx:
          volumes: 
            - ./static-content:share/user/nginx/html

【讨论】:

  • 即使没有命名卷也不工作,同样的错误
  • 这是另一个错字
猜你喜欢
  • 2019-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多