在两个容器之间共享文件

Sharing files between two containers

几个小时以来,我一直在努力 docker 撰写。我正在构建 angular 应用程序。我可以看到 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.

非常感谢任何帮助

您可以尝试不使用命名卷来解决它:

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

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

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

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

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

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

只是一个想法,没有测试过,另外一种解决方案是删除输出路径中的内容。并将 deleteOutputPath 设置为 false,因为 Angular 似乎试图删除目录本身。

更新:

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

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