1

I'm using docker-compose to orchestrate containers for multiple separate projects. Each of these projects has their own set of containers and do not relate to other projects.

For example:

/my-projects/project-1/docker-compose.yml
/my-projects/project-2/docker-compose.yml
/my-projects/project-3/docker-compose.yml

These projects are, however, similar in that they are all PHP projects and use webpack for front-end assets, thus share the same package managers: composer and yarn.

I was wondering, in the interest of performance, if it would be possible to mount a shared volume outside the directory root of all the projects for package manager caches?

For example:

/my-projects/caches/composer
/my-projects/caches/npm
/my-projects/project-1/docker-compose.yml
/my-projects/project-2/docker-compose.yml
/my-projects/project-3/docker-compose.yml

Where /my-projects/caches/composer and /my-projects/caches/npm get mounted inside the relevant containers within each project. In case it's not clear, only one project would be spun up at a time.

At the moment, if two projects share the same deps then each downloads and caches it individually. A more performant (in terms of build times) would be to mount a common volume and point the package manager's caches there so that when "Project A" downloads an update to a dip, "Project B" can load it from cache.

yivi
  • 42,438
  • 18
  • 116
  • 138
HelloPablo
  • 615
  • 1
  • 7
  • 22
  • 1
    If you intend to use a bind mount - then just mount it. There is no additional configuration, just bind-mount one directory in as many containers as you like. – anemyte Feb 04 '21 at 09:38
  • Thanks @anemyte! To my knowledge mounts described in the docker-compose file are relative/restricted to the root of the project - these would be directories outside of the project. – HelloPablo Feb 04 '21 at 09:44
  • Wonderful, I missed that! I'll experiment and see what works. In these projects `composer.json` etc aren't available at image build time so are run as a separate build process. Thank you all. – HelloPablo Feb 04 '21 at 09:49
  • 1
    Both absolute and relative paths (via `..`) work. It is Dockerfile that is restricted to a certain context root, not docker-compose.yml. – anemyte Feb 04 '21 at 10:05
  • Thanks everyone, this is all very useful - I was indeed confused between Dockerfile limitations and docker-compose [lack] of limitation in this regards. @NicoHaase that is useful, thank you. – HelloPablo Feb 04 '21 at 12:35

1 Answers1

0

You can simply mount the same directories as volume binds on each of the containers that require it. You can use absolute paths. Even one of the examples in the docs is using an absolute path as bind mount.

However, volumes are not available during image build (docker-compose build, which is where commands like composer install, npm install or yarn install should be run in any case.

Although if you are running these commands at container runtime, nothing stops you from mounting these cache dirs into each container.

HelloPablo
  • 615
  • 1
  • 7
  • 22
yivi
  • 42,438
  • 18
  • 116
  • 138