21 Days of Docker-Day 6- Introduction to Dockerfile – Part 2


  • Before wrapping up for today, let’s discuss about .dockerignore
  • If you are aware of .gitignore, .dockerignore is similar to that, it’s useful if we want to exclude files we don’t want to be copied over when building an image.
  • Let’s create a simple Dockerfile to test it
FROM busybox
COPY ignore/ /tmp/
  • Create a directory
$ mkdir ignore
  • Create two file
touch index.html test.txt
  • Now at one level up, where we created a Dockerfile, create the .dockerignore file with the following content where we are ignoring txt file
$ cat .dockerignore 
  • Now build this image
$ docker build -t myignoreimage1 .
Sending build context to Docker daemon  4.096kB
Step 1/2 : FROM busybox
 ---> 19485c79a9bb
Step 2/2 : COPY ignore/ /tmp/
 ---> 13783322a7f4
Successfully built 13783322a7f4
Successfully tagged myignoreimage1:latest
  • Run and test it
$ docker run -it myignoreimage1 sh
/ # cd /tmp
/tmp # ls -la
total 4
drwxrwxrwt    2 root     root            24 Oct 13 01:03 .
drwxr-xr-x   12 root     root          4096 Oct 13 01:04 ..
-rw-rw-r--    1 root     root             0 Oct 13 01:00 index.html
  • As you can see, COPY directive only copy index.html and exclude test.txt

This is a good place, to stop for Day 6

Please follow me with my Journey

This time to make learning more interactive, I am adding

  • Slack
  • Meetup

Please feel free to join this group.



Meetup Group

If you are in the bay area, please join this meetup group https://www.meetup.com/100daysofdevops/