docker - 在两个容器之间共享文件

标签 docker docker-compose docker-volume

几个小时以来,我一直在为 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.

任何帮助都将不胜感激

最佳答案

正如错误所暗示的那样,我相信这是一个僵局。您的 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 key 在这种情况下无济于事,因为它仅表示依赖关系,与依赖服务的“准备就绪”无关。

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

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

更新:

所以删除输出路径中的内容似乎可行!正如我提到的,设置 deleteOutputPathfalse .在您的 package.json文件,在脚本对象中,具有类似于此的内容:
{
  "scripts": {
    "build:production": "rm -rf ./dist/* && ng build --configuration production",
  }
}

关于docker - 在两个容器之间共享文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50542783/

相关文章:

php - Nginx+PHP-FPM : connection refused while connecting to upstream (502)

java - Spring JPA 的最低要求是什么

docker-compose - pg_restore : [archiver] could not open input file. 没有这样的文件或目录

java - 无法从 docker 容器连接到 mysql 主机

docker - 无法使用dockerfile golang打开文件

node.js - 错误 : @prisma/client did not initialize yet. 请运行 "prisma generate"

大量的Docker卷

docker - 如何允许 docker 同级容器从现有卷绑定(bind)子目录

docker - 找不到RPI Kubernetes节点上的Traefik返回404页面

docker - 将 docker 1.13.1 容器作为守护进程运行并告诉等待交互式输入 - 仍然停止