***
```bash
FROM eclipse-temurin:17 AS builder
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} web-scrapping-kata-0.0.1.jar
RUN java -Djarmode=layertools -jar web-scrapping-kata-0.0.1.jar extract
FROM eclipse-temurin:17-focal
LABEL authors="github.com/ehayik"
# Install necessary packages
RUN apt-get update && \
apt-get install -y wget curl unzip firefox
# Download and install geckodriver
RUN GECKODRIVER_VERSION=`curl -Ls -o /dev/null -w %{url_effective} https://github.com/mozilla/geckodriver/releases/latest | sed -n -r 's/.*\/v(.*)/\1/p' | tr -d '\n'` && \
wget --no-verbose -O /tmp/geckodriver.tar.gz https://github.com/mozilla/geckodriver/releases/download/v"$GECKODRIVER_VERSION"/geckodriver-v"$GECKODRIVER_VERSION"-linux64.tar.gz && \
tar -C /opt -zxf /tmp/geckodriver.tar.gz && \
rm /tmp/geckodriver.tar.gz && \
mv /opt/geckodriver /opt/geckodriver-"$GECKODRIVER_VERSION" && \
chmod 755 /opt/geckodriver-"$GECKODRIVER_VERSION" && \
ln -fs /opt/geckodriver-"$GECKODRIVER_VERSION"/usr/bin/geckodriver
## Clean up
RUN apt autoremove -y && apt autoclean -y && \
rm -rf /var/lib/apt/lists/*
# # Set up the xvfb (X Virtual Framebuffer)
ENV DISPLAY=:99
COPY --from=builder dependencies/ ./
COPY --from=builder snapshot-dependencies/ ./
COPY --from=builder spring-boot-loader/ ./
COPY --from=builder application/ ./
ENTRYPOINT ["java", "org.springframework.boot.loader.launch.JarLauncher"]
```
**First Stage - builder:**
- FROM eclipse-temurin:17 AS builder - This line is pulling the base Docker image that the new Docker image will be based on. The eclipse-temurin:17 is an image which provides the Temurin JDK 17. This image is aliased as builder to be referenced later.
- ARG JAR_FILE=target/*.jar - This is setting a build-time variable JAR_FILE.
- COPY ${JAR_FILE} web-scrapping-kata-0.0.1.jar - Copies the jar file indicated by the JAR_FILE argument into the Docker image with the name web-scrapping-kata-0.0.1.jar.
- RUN java -Djarmode=layertools -jar web-scrapping-kata-0.0.1.jar extract - This command is using the Spring Boot’s layered jar file feature to extract the jar into separate layers for dependencies, snapshot-dependencies, spring-boot-loader, and application.
**Second Stage - Application Setup:**
- FROM eclipse-temurin:17-focal - Similar to first line but this time the focal tag indicating it should use Ubuntu 20.04 LTS (Focal Fossa) as the base OS.
- Afterward, it's installing necessary libraries and softwares, such as wget, curl, unzip, firefox, and more importantly, it's installing geckodriver, which is a proxy for using W3C WebDriver-compatible clients to interact with Gecko-based browsers, meaning in this case, Firefox.
- Then it's cleaning up the system to make the image smaller.
- ENV DISPLAY=:99 - This step sets the DISPLAY environment variable to :99, which is a requirement for the headless browser.
- In the next block, it's copying the previously extracted layers from the builder image.
- ENTRYPOINT ["java", "org.springframework.boot.loader.launch.JarLauncher"] - Defining the main command to run when the Docker container starts up. In this case, it is the Spring Boot’s JarLauncher.
> [!NOTE]
> The org.springframework.boot.loader.JarLauncher is a special class provided by Spring Boot which will setup the classpath and then run the primary class. The 'JarLauncher' is used when the classes (*.class files) are packaged in a JAR file.
***
References:
- [Creating Docker Images with Spring Boot](https://www.baeldung.com/spring-boot-docker-images)
- [Dockerizing a Spring Boot Application](https://www.baeldung.com/dockerizing-spring-boot-application)