Exceptions in .dockerignore

2020-03-24 02:46发布

问题:

How does .dockerignore handle exceptions?

For example, I would like to ignore everything from the src/ directory except for src/web/public.

I've tried...

.git
src
!src/web/public

Seems to not work.

docker build . shows Sending build context to Docker daemon 20.63 MB either way.

回答1:

It does not appear that .dockerignore handles exceptions. If there is a well-known syntax for this, you could propose the change and make a pull request.

In tag 1.3 of commands.go we see this:

    ignore, err := ioutil.ReadFile(path.Join(root, ".dockerignore"))
    // ...
    options := &archive.TarOptions{
        Compression: archive.Uncompressed,
        Excludes:    excludes,
    }
    context, err = archive.TarWithOptions(root, options)

and in archive.go:

    for _, include := range options.Includes {

            // ...
            skip, err := fileutils.Matches(relFilePath, options.Excludes)
            if err != nil {
                log.Debugf("Error matching %s", relFilePath, err)
                return err
            }

            if skip {
                if f.IsDir() {
                    return filepath.SkipDir
                }
                return nil
            }
            // ...
    }


回答2:

Just wanted to add in case others run into this question, that from docker 1.7.0 exclusions are supported in the .dockerignore file. So the example !src/web/public in the question actually works

https://docs.docker.com/engine/reference/builder/#dockerignore-file



回答3:

Using my response to another question https://stackoverflow.com/a/51429852/2419069 you can use the following .dockerignore file:

# Ignore everything
**

# Allow /src/web/public directory
!/src/web/public/**

# You can also ignore unnecessary files inside the /src/web/public to make your image even smaller.
**/*~
**/*.log
**/.DS_Store
**/Thumbs.db


标签: docker