42

I'm having in Dockerfile:

ENV DATARATOR_HOME /usr/local/share/datarator
RUN mkdir -p $DATARATOR_HOME
COPY . $DATARATOR_HOME

and .dockerignore file:

/Gemfile.lock
/coverage
/spec
*.bundle
*.so
*.o
*.a
mkmf.log
*.swp
/.*
/tmp
/log

However, once showing files in the built container, I can see also those that are supposed to be ignored:

/usr/local/share/datarator # ls -lha
total 128
drwxr-xr-x   10 root     root        4.0K Mar 29 21:01 .
drwxr-xr-x    4 root     root        4.0K Mar 29 21:00 ..
drwxr-xr-x    2 root     root        4.0K Mar 29 21:01 .bundle
-rw-rw-r--    1 root     root          24 Mar 29 20:37 .coveralls.yml
-rw-rw-r--    1 root     root          81 Mar 29 20:37 .dockerignore
drwxrwxr-x    8 root     root        4.0K Mar 29 20:37 .git
-rw-rw-r--    1 root     root          85 Mar 29 20:37 .gitignore
-rw-rw-r--    1 root     root        1.2K Mar 29 20:37 .travis.yml
-rw-rw-r--    1 root     root         509 Mar 29 20:37 .vimrc
-rw-rw-r--    1 root     root         959 Mar 29 20:37 Dockerfile
-rw-rw-r--    1 root     root          94 Mar 29 20:37 Gemfile
-rw-r--r--    1 root     root        2.7K Mar 29 21:01 Gemfile.lock
-rw-rw-r--    1 root     root         343 Mar 29 20:37 Guardfile
-rw-rw-r--    1 root     root        1.0K Mar 29 20:37 LICENSE.txt
-rw-rw-r--    1 root     root          71 Mar 29 20:37 Procfile
-rw-rw-r--    1 root     root       14.8K Mar 29 20:37 README.md
-rw-rw-r--    1 root     root         198 Mar 29 20:37 Rakefile
drwxrwxr-x    2 root     root        4.0K Mar 29 20:37 bin
drwxrwxr-x    2 root     root        4.0K Mar 29 20:37 config
-rw-rw-r--    1 root     root          97 Mar 29 20:37 config.ru
-rw-r--r--    1 root     root       16.0K Mar 29 21:01 datarator-0.0.1.gem
-rw-rw-r--    1 root     root        1.7K Mar 29 20:37 datarator.gemspec
drwxrwxr-x    4 root     root        4.0K Mar 29 20:37 lib
drwxrwxr-x    2 root     root        4.0K Mar 29 20:37 log
drwxrwxr-x    3 root     root        4.0K Mar 29 20:37 spec
drwxrwxr-x    2 root     root        4.0K Mar 29 20:37 tmp

How can I achieve having all those mentioned in the .dockerignore file ignored?

Peter Butkovic
  • 11,143
  • 10
  • 57
  • 81

2 Answers2

25

The .dockerignore rules follow the filepath/#Match.

Try (for testing) Gemfile.lock instead of /Gemfile.lock.

And check that the eol (end of line) characters are unix-style, not Windows style in your .dockerignore file.

Apparently, (docker 1.10, March 2016) using rule starting with / like /xxx ( or /.*) is not well supported.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I checked and I have unix encoding, moreover I tried with `Gemfile.lock`, didn't help. Any further hints? Full code is available on: https://github.com/datarator/datarator , moreover, the docker image is: `datarator/datarator:edge` – Peter Butkovic Mar 30 '16 at 05:45
  • @PeterButkovic What docker version are tou using? On which OS? – VonC Mar 30 '16 at 05:59
  • locally: docker: `Docker version 1.10.3, build 20f81dd` OS: `RHEL 7.2`, however I have it also running in the travis infrastructure, same problem on both sides. – Peter Butkovic Mar 30 '16 at 06:06
  • @PeterButkovic following https://github.com/docker/docker/issues/17911, can you try without the `/.*` in your `.dockerignore`? (just for testing) – VonC Mar 30 '16 at 06:07
  • well, I even tried with having only: `Gemfile.lock` in my `.dockerignore`, however, it didn't work for me :( – Peter Butkovic Mar 30 '16 at 06:36
  • @PeterButkovic OK so *any* content of the .dockerignore is ignored, right? – VonC Mar 30 '16 at 06:39
  • exactly, sounds like I do some dummy mistake – Peter Butkovic Mar 30 '16 at 06:41
  • @PeterButkovic what command line are you using to build that Dockerfile? – VonC Mar 30 '16 at 06:42
  • `docker build -t datarator:edge .` – Peter Butkovic Mar 30 '16 at 06:42
  • @PeterButkovic For testing, can you try with ADD instead of COPY? – VonC Mar 30 '16 at 06:46
  • 1
    OK, interesting, I've found the following points (regardless of COPY or ADD): using root slash doesn't work for me (=> file/dir is added anyway), moreover I had some weird issues I can't reproduce any more (single `Gemfile.lock` works OK now), @VonC: your answer seems right :) Thanks for your patient support! – Peter Butkovic Mar 30 '16 at 07:03
  • @PeterButkovic So the issue was using /? – VonC Mar 30 '16 at 07:05
  • @PeterButkovic OK, I have included that in the answer for more visibility. – VonC Mar 30 '16 at 07:07
  • @SalahAdDin Can you ask a new question, with the exact version of your docker, and host OS? – VonC Feb 07 '18 at 07:49
1

Here's the complete syntax for the .dockerignore:

pattern:
{ term }
term:
'*' matches any sequence of non-Separator characters
'?' matches any single non-Separator character
'[' [ '^' ] { character-range } ']'
character class (must be non-empty)
c matches character c (c != '*', '?', '\\', '[')
'\\' c matches character c

character-range:
c matches character c (c != '\\', '-', ']')
'\\' c matches character c
lo '-' hi matches character c for lo <= c <= hi

additions:
'**' matches any number of directories (including zero)
'!' lines starting with ! (exclamation mark) can be used to make exceptions to exclusions
'#' lines starting with this character are ignored: use it for comments

Therefore, some of your matches would be changed according to the above syntax:

/Gemfile.lock  --> */Gemfile.lock or **/Gemfile.lock or *.lock 
/spec  --> */spec or **/spec
/tmp  --> in the same way
/log  --> in the same way

Here are two articles to explain more:

Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150