Abdelrhman Hussien
Abdelrhman Hussien

Reputation: 374

Sharing files between two containers

For couple of hours I am struggling with docker compose. I am building angular app. And I could see the files in the dist directory. Now I want to share these files with the nginx container. I thought the shared volume will do it. But when I add

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

volumes:
   static:

an try docker-compose up --build I got this error

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! [email protected] build: `ng build --prod`
client_1  | npm ERR! Exit status 1
client_1  | npm ERR! 
client_1  | npm ERR! Failed at the [email protected] build-prod script.
client_1  | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

Any help is fully appreciated

Upvotes: 1

Views: 716

Answers (2)

marckassay
marckassay

Reputation: 1400

I believe this is, as the error suggests, a deadlock situation. Your docker-compose file has 2 services that start approximately, if not, simultaneously. Both of them has some sort of hold on the Docker volume (named, "static"). When Angular executes ng build, by default, --deleteOutputPath is set to true. And when it attempts to delete the output directory, the error message that you received occurs.

If deleteOutputPath is set to false the issue should be resolved. Perhaps that is sufficient for your needs. If not, as an alternative, having the --outputPath set to a temp directory within the project directory and after Angular builds, move the contents into the Docker volume. If the temp directory path is out/dist and the volume maps to dist this can be used:

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

However, that alternative solution is really just working around the issue. To make note, the docker-compose depends_on key will not help in this situation as it simply signifies a dependency and nothing to do with "readiness" of the dependent service.

And also to make note, executing docker volume rm <name> will have no consequences as a solution here. Remember, both services have a hold on the volume as one is trying to remove it.

Just a thought, haven't tested, as another alternative solution is to delete the contents within the output path. And set the deleteOutputPath to false, since Angular seems to be attempting to delete the directory itself.

Update:

So removing the contents in the output path seems to work! As I mentioned, set deleteOutputPath to false. And in your package.json file, in the scripts object, having something similar to this:

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

Upvotes: 3

Max Martynov
Max Martynov

Reputation: 739

You can try to solve it without using named volumes:

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

Upvotes: 1

Related Questions