GoogleContainerTools / jib

🏗 Build container images for your Java applications.
Apache License 2.0
13.58k stars 1.43k forks source link

Exclude folders not working #4186

Closed skumaravelan closed 6 months ago

skumaravelan commented 6 months ago

Hi ,

Environment:

Description of the issue:

Extradirectory exclusion not working .

      <extraDirectories>
                        <!-- Copy only directories from the /path/to/your/source-directory into the /app directory in the container -->
                        <paths>
                            <path>
                                <from>/mycontent/init/</from>
                                <into>/app</into>
                                <excludes>
                                    <exclude>**/target/**/**</exclude> <!-- Exclude target directory and its contents -->
                                    <exclude>**/src/**/**</exclude> <!-- Exclude src directory and its contents -->
                                    <exclude>**/classpath/**</exclude> <!-- Exclude classpath directory and its contents -->
                                    <exclude>pom.xml</exclude> <!-- Exclude pom.xml file -->
                                    <!-- You can specify multiple exclude elements if needed -->
                                </excludes>
                            </path>
                            <path>
                                <!-- Source directory -->
                                <from>${project.basedir}</from>
                                <!-- Destination directory in the container -->
                                <into>/app</into>
                                <!-- Exclude specific directories -->
                                <excludes>
                                    <exclude>target/**</exclude> <!-- Exclude target directory and its contents -->
                                    <exclude>src/**</exclude> <!-- Exclude src directory and its contents -->
                                    <exclude>pom.xml</exclude> <!-- Exclude pom.xml file -->
                                    <!-- Add more excludes if needed -->
                                </excludes>
                            </path>
                        </paths>
                    </extraDirectories>

Expected behavior:

Container image expected to exclude target and src folders. But, they are appearing in the produced image.

jib-maven-plugin Configuration:

   <extraDirectories>
                        <!-- Copy only directories from the /path/to/your/source-directory into the /app directory in the container -->
                        <paths>
                            <path>
                                <from>/mycontent/init/</from>
                                <into>/app</into>
                                <excludes>
                                    <exclude>**/target/**/**</exclude> <!-- Exclude target directory and its contents -->
                                    <exclude>**/src/**/**</exclude> <!-- Exclude src directory and its contents -->
                                    <exclude>**/classpath/**</exclude> <!-- Exclude classpath directory and its contents -->
                                    <exclude>pom.xml</exclude> <!-- Exclude pom.xml file -->
                                    <!-- You can specify multiple exclude elements if needed -->
                                </excludes>
                            </path>
                            <path>
                                <!-- Source directory -->
                                <from>${project.basedir}</from>
                                <!-- Destination directory in the container -->
                                <into>/app</into>
                                <!-- Exclude specific directories -->
                                <excludes>
                                    <exclude>target/**</exclude> <!-- Exclude target directory and its contents -->
                                    <exclude>src/**</exclude> <!-- Exclude src directory and its contents -->
                                    <exclude>pom.xml</exclude> <!-- Exclude pom.xml file -->
                                    <!-- Add more excludes if needed -->
                                </excludes>
                            </path>
                        </paths>
                    </extraDirectories>
chanseokoh commented 6 months ago

Let me try to understand: no files under the target and src are copied as expected, but you are left with empty target and src directories in the image, right?

skumaravelan commented 6 months ago

I see the content also in it.

skumaravelan commented 6 months ago

Please suggest me , how to exclude those folders as well.

chanseokoh commented 6 months ago

If you can provide a simple reproducible repo that I can clone, I can look into it quickly.

skumaravelan commented 6 months ago

I don’t have the public repo . You need the Pom..cml

skumaravelan commented 6 months ago

Please share any working example of config . I will check that out .

skumaravelan commented 6 months ago
   <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>jib-maven-plugin</artifactId>
                <version>3.4.0</version>

                <configuration>
                    <from>
                        <image>${jib.from.image}</image>

                    </from>
                    <to>
                        <image>${jib.to.image}</image>
                        <auth>
                            <username>${jib.to.auth.username}</username>
                            <password>${jib.to.auth.password}</password>
                        </auth>

                    </to>
                    <containerizingMode>packaged</containerizingMode>
                    <container>
                        <creationTime>USE_CURRENT_TIMESTAMP</creationTime>
                        <!-- Define the working directory inside the container -->
                    <workingDirectory>/app</workingDirectory>
                        <!--
                    <entrypoint>java -jar -Dquarkus.launch.rebuild=true /app/quarkus-app/quarkus-run.jar &amp;&amp; sleep 5m &amp;&amp; java  -Dquarkus.application.name=Stratos-Workflow -Dquarkus.http.port=8081 -Dapp.hawtio.port=20080 -Xms1024m -Xmx1024m -jar /app/quarkus-app/quarkus-run.jar </entrypoint>
                    -->
                        <entrypoint>
                            <shell>bash</shell>
                            <option>-c</option>
                            <argument>chmod +x /app/entrypoint.sh&amp;&amp; /app/entrypoint.sh</argument>
                        </entrypoint>
                        <ports>
                            <port>8080</port>
                        </ports>
                    </container>
                    <extraDirectories>
                        <!-- Copy only directories from the /path/to/your/source-directory into the /app directory in the container -->
                        <paths>
                            <path>
                                <from>/myapp/init/</from>
                                <into>/app</into>
                                <excludes>
                                    <exclude>**/target/**/**</exclude> <!-- Exclude target directory and its contents -->
                                    <exclude>**/src/**/**</exclude> <!-- Exclude src directory and its contents -->
                                    <exclude>**/classpath/**</exclude> <!-- Exclude classpath directory and its contents -->
                                    <exclude>pom.xml</exclude> <!-- Exclude pom.xml file -->
                                    <!-- You can specify multiple exclude elements if needed -->
                                </excludes>
                            </path>
                            <path>
                                <!-- Source directory -->
                                <from>${project.basedir}</from>
                                <!-- Destination directory in the container -->
                                <into>/app</into>
                                <!-- Exclude specific directories -->
                                <excludes>
                                    <exclude>target/**</exclude> <!-- Exclude target directory and its contents -->
                                    <exclude>src/**</exclude> <!-- Exclude src directory and its contents -->
                                    <exclude>pom.xml</exclude> <!-- Exclude pom.xml file -->
                                    <!-- Add more excludes if needed -->
                                </excludes>
                            </path>
                        </paths>
                    </extraDirectories>

                </configuration>
            </plugin>
skumaravelan commented 6 months ago

drwxr-xr-x. 1 root root 6 Jan 1 1970 target drwxr-xr-x. 1 root root 6 Jan 1 1970 src drwxr-xr-x. 1 root root 37 Jan 1 1970 scripts drwxr-xr-x. 1 root root 47 Jan 1 1970 routes drwxr-xr-x. 1 root root 102 Jan 1 1970 quarkus-app drwxr-xr-x. 1 root root 27 Jan 1 1970 original drwxr-xr-x. 2 root root 4096 Jan 1 1970 libs -rw-r--r--. 1 root root 28 Jan 1 1970 jib-main-class-file -rw-r--r--. 1 root root 28 Jan 1 1970 jib-classpath-file drwxr-xr-x. 1 root root 24 Jan 1 1970 hawtio -rwxr-xr-x. 1 root root 609 Jan 1 1970 entrypoint.sh drwxr-xr-x. 2 root root 34 Jan 1 1970 classpath drwxr-xr-x. 1 root root 26 Jan 1 1970 atlasmap drwxr-xr-x. 1 root root 73 Feb 14 05:24 .. drwxr-xr-x. 1 root root 36 Feb 14 05:24 config -rw-r--r--. 1 root root 117 Feb 14 05:25 quarkus-artifact.properties Here are the copy of app directory , source and destination is same.

skumaravelan commented 6 months ago

am not seeing any filtering happening, except the pom.xml not in destination .

chanseokoh commented 6 months ago

Here's a working demo you can try yourself too:

$ git clone https://github.com/chanseokoh/jib.git
$ cd jib/examples/helloworld/
$ git checkout i4186-working-demo 
$ mvn compile jib:dockerBuild
...
$ docker run -it --entrypoint ls image-built-with-jib -RF copied
copied:
build.gradle  gradlew      README.md        src/
gradle/       gradlew.bat  settings.gradle  target/

copied/gradle:
wrapper/

copied/gradle/wrapper:
gradle-wrapper.jar  gradle-wrapper.properties

copied/src:

copied/target:

As you can see, copied/pom.xml, copied/src/**, and copied/target/** are excluded as directed.

My configuration is basically the same as yours. The only difference is that I'm copying into /copied instead of /app to eliminate any potential confusion:

                    <extraDirectories>
                        <!-- Copy only directories from the /path/to/your/source-directory into the /app directory in the container -->
                        <paths>
                            <path>
                                <!-- Source directory -->
                                <from>${project.basedir}</from>
                                <!-- Destination directory in the container -->
                                <into>/copied</into>
                                <!-- Exclude specific directories -->
                                <excludes>
                                    <exclude>target/**</exclude> <!-- Exclude target directory and its contents -->
                                    <exclude>src/**</exclude> <!-- Exclude src directory and its contents -->
                                    <exclude>pom.xml</exclude> <!-- Exclude pom.xml file -->
                                    <!-- Add more excludes if needed -->
                                </excludes>
                            </path>
                        </paths>
                    </extraDirectories>
skumaravelan commented 6 months ago

When I configure /copied directory. i see the files on target and src folders are empty. When I add /app , it is showing up with files. Is there a way to exclude the whole directory on /app directory.

chanseokoh commented 6 months ago

Then I think this is the part where you are misconfiguring. I don't know the structure of /myapp/init, so I cannot help further. The <exclude> is working correctly as you've seen, so it's just a matter of configuring it correctly. I can help you, but it's really up to you.

                                <from>/myapp/init/</from>
                                <into>/app</into>
                                <excludes>
                                    <exclude>**/target/**/**</exclude> <!-- Exclude target directory and its contents -->
                                    <exclude>**/src/**/**</exclude> <!-- Exclude src directory and its contents -->
                                    <exclude>**/classpath/**</exclude> <!-- Exclude classpath directory and its contents -->
                                    <exclude>pom.xml</exclude> <!-- Exclude pom.xml file -->
                                    <!-- You can specify multiple exclude elements if needed -->
                                </excludes>
skumaravelan commented 6 months ago

Here is the content of my my app/init drwxr-xr-x. 1 root root 6 Jan 1 1970 target drwxr-xr-x. 1 root root 6 Jan 1 1970 src drwxr-xr-x. 1 root root 37 Jan 1 1970 scripts drwxr-xr-x. 1 root root 47 Jan 1 1970 routes drwxr-xr-x. 1 root root 102 Jan 1 1970 quarkus-app drwxr-xr-x. 1 root root 27 Jan 1 1970 original drwxr-xr-x. 2 root root 4096 Jan 1 1970 libs -rw-r--r--. 1 root root 28 Jan 1 1970 jib-main-class-file -rw-r--r--. 1 root root 28 Jan 1 1970 jib-classpath-file drwxr-xr-x. 1 root root 24 Jan 1 1970 hawtio -rwxr-xr-x. 1 root root 609 Jan 1 1970 entrypoint.sh drwxr-xr-x. 2 root root 34 Jan 1 1970 classpath drwxr-xr-x. 1 root root 26 Jan 1 1970 atlasmap drwxr-xr-x. 1 root root 73 Feb 14 05:24 .. drwxr-xr-x. 1 root root 36 Feb 14 05:24 config -rw-r--r--. 1 root root 117 Feb 14 05:25 quarkus-artifact.properties

Files are getting excluded but not the directory for some reason.

chanseokoh commented 6 months ago

If you want to exclude target and src as well, specify them:

                                     <exclude>target/**</exclude>
+                                    <exclude>target</exclude>
                                     <exclude>src/**</exclude>
+                                    <exclude>src</exclude>
skumaravelan commented 6 months ago

Thanks you so much for helping me out in here. This solution worked !