COPY with docker but with exclusion

前端 未结 7 2107
轮回少年
轮回少年 2020-12-12 11:48

In a Dockerfile, I have

COPY . .

I want to exclude an entire directory, in my case, node_modules directory.

Something like this:

相关标签:
7条回答
  • 2020-12-12 12:30

    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

    0 讨论(0)
  • 2020-12-12 12:31

    FOR A ONE LINER SOLUTION, type the following in Command prompt or Terminal at project root.

    echo node_modules > .dockerignore

    This creates the extension-less . prefixed file without any issue. Replace node_modules with the folder you want to exclude.

    0 讨论(0)
  • 2020-12-12 12:32

    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.

    0 讨论(0)
  • 2020-12-12 12:40

    Excluding node_modules from current directory

    node_modules
    

    Excluding node_modules in any immediate subdirectories

    */node_modules
    

    Here is the official docs

    0 讨论(0)
  • 2020-12-12 12:44

    For those using gcloud build:

    gcloud build ignores .dockerignore and looks instead for .gcloudignore

    Use:

    cp .dockerignore .gcloudignore

    Source

    0 讨论(0)
  • 2020-12-12 12:48

    Adding .dockerignore works for me. One additional point Those who are trying this solution on Windows , windows will not let you create .dockerignore file (as it doesn't by default allows creating file starting with .)

    To create such file starting with . on Windows, include an ending dot also, like : .dockerignore. and hit enter ( provided you have enabled view extension options from folder options )

    0 讨论(0)
提交回复
热议问题