COPY With Docker But With Exclusion
Answer :
Create file .dockerignore
in your docker build context directory (so in this case, most likely a directory that is a parent to node_modules) with one line in it:
**/node_modules
although you probably just want:
node_modules
Info about dockerignore: https://docs.docker.com/engine/reference/builder/#dockerignore-file
For those who can't use a .dockerignore file (e.g. if you need the file in one COPY but not another):
Yes, but you need multiple COPY instructions. Specifically, you need a COPY for each letter in the filename you wish to exclude.
COPY [^n]* # All files that don't start with 'n'
COPY n[^o]* # All files that start with 'n', but not 'no'
COPY no[^d]* # All files that start with 'no', but not 'nod'
Continuing until you have the full file name, or just the prefix you're reasonably sure won't have any other files.
In my case, my Dockerfile contained an installation step, which produced the vendor
directory (the PHP equivalent of node_modules
). I then COPY
this directory over to the final application image. Therefore, I could not put vendor
in my .dockerignore
. My solution was simply to delete the directory before performing composer install
(the PHP equivalent of npm install
).
FROM composer AS composer
WORKDIR /app
COPY . .
RUN rm -rf vendor \
&& composer install
FROM richarvey/nginx-php-fpm
WORKDIR /var/www/html
COPY --from=composer /app .
This solution works and does not bloat the final image, but it is not ideal, because the vendor
directory on the host is copied into the Docker context during the build process, which adds time.
Comments
Post a Comment