Mounting source to multiple containers - good practice?

I’ve dockerized an existing PHP-FPM project.

As base images I’ve used:

  • php:8.0.18-fpm-bullseye (fpm)
  • ubuntu:focal (apache)
  • php:8.0.18-cli-bullseye (nightly maintenance cron job)
  • mongo:5.0.8-focal (db)

It works and I’m very happy with Docker so far :slight_smile:

Now, three of the containers (all apart from the DB) need parts of the source code (php, and static web resources) with some overlap.

So far I’ve just mounted the (built) source with a volume to each of the three containers. Building consists of downloading extensions with php-composer and some minifying of css and js code.

Now I also want to include this building step into the Dockerfiles. The issue is, that this source is used three times (I guess I could theoretically split the static resources away and only have them in the apache container, but the php source is needed in both the fpm and cli)

So the question is in which Dockerfile would I put the build step?
I am looking for some advice on how to handle this in a nice way. I’m also open to rearranging the project into multiple parts, but right now I’m a little stuck because I don’t know enough about good docker design.

Or can I have a Dockerfile which only builds the source and then provides it to containers?

Or is the best practice to COPY / ADD the source directly into the images? In production I wouldn’t want the host to be able to manipulate the source code.