Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying a directory into a docker image while skipping given sub directories

I have to create a docker file that copies MyApp directory into the image.

MyApp
  -libs
  -classes
  -resources

Libs directory has around 50 MB and it is not frequently changing where as Classes directory is around 1 MB and it is subjected to frequent changes. To optimize the docker build, I planned to add Libs directory at the beginning of the Dockerfile and add other directories at the end of the Dockerfile. My current approach is like this

ADD MyApp/libs /opt/MyApp/libs
## do other operations
ADD MyApp/classes /opt/MyApp/resources
ADD MyApp/classes /opt/MyApp/classes

This is not a maintainable format as in future I may have some other directories in the MyApp directory to be copied into the docker image. My target is to write a docker file like this

ADD MyApp/libs /opt/MyApp/libs
## do other operations
ADD MyApp -exclude MyApp/libs /opt/MyApp

Is there a similar command to exclude some files in a directory which is copied into the docker image?

like image 724
Dimuthu Avatar asked Oct 18 '22 19:10

Dimuthu


1 Answers

I considered the method explained by @nwinkler and added few steps to make the build consistent.

Now my context directory structure is as follows

-Dockerfile
-MyApp
  -libs
  -classes
  -resources
-.dockerignore
-libs

I copied the libs directory to the outer of the MyApp directory. Added a .dockerignore file which contains following line

MyApp/libs/*

Updated the Dockerfile as this

ADD libs /opt/MyApp/libs
## do other operations
ADD MyApp /opt/MyApp

Because dockerignore file ignores MyApp/lib directory, there is no risk in over-writing libs directory I have copied earlier.

like image 141
Dimuthu Avatar answered Oct 21 '22 02:10

Dimuthu