I'm learning Docker and I'm facing the following issue, mainly related to sharing files between services. I've prepared an illustration:
I have 3 stages in my Dockerfile
: builder, php and caddy. My docker-compose.yml
defines 2 services: php and caddy (the target is the correspoding stage in the Dockerfile
). Requirements:
- builder stage produces some files needed by php service at runtime (i.e.
public/build/metatada.json
) - builder stage produces some files needed by caddy service at runtime (i.e.
public/js/foo.js
) - php stage produces some files that should be available to caddy service at runtime (i.e.
public/bundles/foo.js
) - caddy service must share
public/
folder with php service, because new files can be created (i.e. userpublic/uploads/newfile.pdf
) - New static files in source code must be taken into account (i.e.
public/img/newimage.png
)
Solution 1: my first naive solution won't work because point 4: caddy service can't access resources created by the php services at runtime (i.e. public/uploads/new.png
):
FROM node:current-alpine as builder
WORKDIR /app
COPY . .
RUN npm run build
FROM php:fpm-latest as php
COPY . .
COPY --from=builder /app/public/ public/
FROM caddy:latest as caddy
WORKDIR /srv
COPY --from=builder /app/public/ public/
Solution 2: bind mount source code public
folder to caddy /srv/public
and php /var/www/html/public
folder. This solves 4 and 5, but completely wipes the builder output so points 1, 2, 3 are not respected.
Which other option I have? I already tried to share a volume (see my other SO question) but it won't work either.
php
andcaddy
tobuilder
? This is supposed to wait for the container to be up before starting the others .. Plus it is weird that bind mount wipes files. Can you post the command/volume definition for mounting? – Legislatepublic
folder because during the build my images will populate sub-folders ofpublic
folder. On startup, the local content will replace the one populated by imaes. That's how bind-mount works. – Nickeliferous