dockerdockerignore

.dockerignore workflow unclear


I have a question about the .dockerignore workflow which I wasn't really able to understand while browsing through the documentation and different internet topics.

Have the following folder structure:

home
   |
   |- folder_1
   |- folder_2

Inside my dockerfile I want to copy the contents of home directory, so I use

COPY ./ /home

Inside .dockerignore I have:

*
!folder_1
!folder_3

I am referring to a non-existent folder - folder_3, which is supposed to be copied, right? I ran it and it looks like there's no problem with that, thus .dockerignore somehow manages this situation.

If I tried to do the same thing without using .dockerignore, targeting a non-existent directory I would get an error.

If anybody can please clear this workflow, or if a duplicate, please attach some information so I can educate myself.

Thanks in advance!


Solution

  • First of all, .dockerignore works like .gitignore. Inside these files you set the rules on the basis of which files should be added, and which should not.

    In your scenario you COPY the whole home directory which consists of folder_1 and folder_2. Your .dockerignore file sets the following rules:

    *           # ignore all files/directories
    !folder_1   # do not ignore folder_1
    !folder_3   # do not ignore folder_3
    

    Regardless of whether there is a folder_1 or folder_3 in your local home directory or not, it won't show you any errors, because it just tries to find particular files/directories that are inside .dockerignore. If it finds this file/directory, it applies the rules. If it doesn't find this file/directory, it doesn't do anything with it.

    Hope that's a little bit more clear now.