Copying a directory to the docker image while skipping specified subdirectories

I need to create a docker file that copies the MyApp directory to an image.

MyApp -libs -classes -resources 

The Libs directory has about 50 MB, and it does not change often when the Classes directory is about 1 MB, and it undergoes frequent changes. To optimize the docker build, I planned to add the Libs directory at the beginning of the Docker file and add other directories at the end of the Docker file. My current approach is similar to this

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

This format is not supported, as in the future I may have some other directories in the MyApp directory that will be copied to the docker image. My goal 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 the directory that is copied to the docker image?

+5
source share
2 answers

I reviewed the method explained by @nwinkler and added a few steps to make the assembly consistent.

Now my context directory structure is as follows

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

I copied the libs directory to the MyApp external directory. Added .dockerignore file containing the following line

 MyApp/libs/* 

Updated Docker file as

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

Since the dockerignore file ignores the MyApp / lib directory, there is no risk in the libs rewrite directory that I copied earlier.

+2
source

This is not possible out of the box with the current version of Docker. Using the .dockerignore file .dockerignore also not work, as it will always exclude the libs folder.

What you can do is wrap docker build in a shell script and copy the MyApp folder (minus libs ) and the libs folder to temporary directories before calling docker build .

Something like that:

 #!/usr/bin/env bash rm -rf temp mkdir -p temp # Copy the whole folder cp -r MyApp temp # Move the libs folder up one level mv temp/MyApp/libs temp # Now build the Docker image docker build ... 

Then you can change your Dockerfile to copy from temp directory:

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

There is a risk that the second ADD command will remove the /opt/MyApp/libs folder from the image. If this happens, you may have to cancel the ADD commands and add the libs folder after everything else.

+1
source

Source: https://habr.com/ru/post/1262509/


All Articles