Jib - where to copy webapp folder inside image?

Viewed 2178

I am creating docker image using google's Jib maven plugin, image gets created successfully and backend services are working fine but my webapp folder is not part of that image. Before jib i was creating a zip containing everything (including webapp folder in the root of that zip along with executable jar) which was working fine.

Now the image created by jib has classes, libs, resources in the app root. How and where should i copy webapp folder ?

3 Answers

It worked for me by using external directories provided by maven jib plugin.

<extraDirectories>
    <paths>
      <path>webapp</path>
      <path>
        <from>webapp</from>
        <into>/app/webapp</into>
      </path>
    </paths>
</extraDirectories>

I am currently using running a spring-boot version 2.4.10 and the application is packed as a jar.

My project have JSPs at:

src/main/webapp/WEB-INF/jsp

This is important because it allows me to run the application as an executable jar: java -jar ./target/app.jar --spring.profiles.active=prod

jib-plugin doesn't copy the src/main/webapp directory to the container image by default, so we need to add it manually by including the following configuration.

<extraDirectories>
   <paths>
      <path>
          <from>src/main/webapp/WEB-INF</from>
          <into>/app/resources/META-INF/resources/WEB-INF</into>
      </path>
   </paths>
</extraDirectories>

I provide jib-plugin a custom entrypoint.sh

The entrypoint.sh is located at src/main/jib

#!/bin/sh
echo "The application will start in ${APPLICATION_SLEEP}s..." && sleep ${APPLICATION_SLEEP}
exec java ${JAVA_OPTS} -noverify -XX:+AlwaysPreTouch \
-Djava.security.egd=file:/dev/./urandom -cp /app/resources/:/app/classes/:/app/libs/* \
"com.demo.application.Application"  "$@"

My final jib-plugin configuration is the following:

<plugin>
    <groupId>com.google.cloud.tools</groupId>
    <artifactId>jib-maven-plugin</artifactId>
    <version>${jib-maven-plugin.version}</version>
    <configuration>
        <from>
            <image>adoptopenjdk:11-jre-hotspot</image>
        </from>
        <to>
            <image>myprivateregistry/app/${project.name}</image>
            <tags>
                <tag>latest</tag>
                <tag>${project.version}</tag>
            </tags>
        </to>
        <container>
            <entrypoint>
                <shell>bash</shell>
                <option>-c</option>
                <arg>/entrypoint.sh</arg>
            </entrypoint>
            <ports>
                <port>8080</port>
            </ports>
            <environment>
                <SPRING_OUTPUT_ANSI_ENABLED>ALWAYS</SPRING_OUTPUT_ANSI_ENABLED>
                <APPLICATION_SLEEP>0</APPLICATION_SLEEP>
            </environment>
            <creationTime>USE_CURRENT_TIMESTAMP</creationTime>
        </container>
        <extraDirectories>
            <paths>
                <path>src/main/jib</path>
                <path>
                    <from>src/main/webapp/WEB-INF</from>
                    <into>/app/resources/META-INF/resources/WEB-INF</into>
                </path>
            </paths>
            <permissions>
                <permission>
                    <file>/entrypoint.sh</file>
                    <mode>755</mode>
                </permission>
            </permissions>
        </extraDirectories>
    </configuration>
    <!-- Make JIB plugin run during the packing life cycle -->
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>build</goal>
            </goals>
        </execution>
    </executions>
</plugin>

The above didn't work for me but the below did.

<extraDirectories>
    <paths>
        <path>
            <from>../path/to/frontend/app/dist</from>
            <into>/app/resources/static</into>
        </path>
    </paths>
</extraDirectories>
Related