How to create docker file with Chrome browser and chrome driver dependencies for a Java based Selenium application
This is related to a maven 3.9.6-based and java 17-based selenium automation project used to log and download files from a remote site, it works fine as a nonotorized application.
This application uses the Chrome browser in headless mode. Currently, the Chrome drivers and relevant Maven versions are bundled with it in the application. The Chrome drivers have to match the Chrome browser for the Selenium operations to work without error. The Chrome browser version in the current server is 123.0.6312.86 and the drivers in drivers/chromedriver-linux64 are used. The Chrome drivers need to be updated if the Chrome browser is updated on the server. I have placed the report-automation.jar at the root level of the application during the build.
Currently, the Chrome browser should be installed on a server and is not bundled into the application in any way. The related Chrome drivers are only bundled in the application. how can this application be dockerized? should the Chrome browser and Chrome driver installation be handled in the docker file?
Given below is a sample docker file I created for this scenario. But the Chrome browser and Chrome driver-related sections need to be updated Could the docker file be optimized further to support containerization optimally?
Furthermore, the outcome of this application is to download a CSV report Downloaded-Report folder in the application. How can this operation be handled if the application is dockerized?
[`OS Version/build - Ubuntu 18.04 LTS
Docker Version - 24.0.2, build cb74dfc
Maven Version - 3.9.6
Java Version - 17.04
Chrome browser version - 123.0.6312.86`]()
The currently used docker file is given below:
[`# Use the official OpenJDK 17 image as a base image
FROM maven:3.9.6-eclipse-temurin-17-alpine AS builder
# Set the working directory inside the container
WORKDIR /app
RUN chmod -R 777 /app
# Install tools.
RUN apk update && apk add --no-cache wget unzip
[#######################
# Install Chrome.
# Adding Google Chrome repository
RUN apk add --no-cache --repository=http://dl-cdn.alpinelinux.org/alpine/edge/community google-chrome-stable
#######################
# Download the Chrome Driver
RUN wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/$(wget -qO- https://chromedriver.storage.googleapis.com/LATEST_RELEASE)/chromedriver_linux64.zip \
&& unzip /tmp/chromedriver.zip -d /usr/local/bin/ \
&& rm /tmp/chromedriver.zip
ENV PATH="/usr/local/bin:${PATH}"
#######################
# Copy the entire project (assuming Dockerfile is in the project root)
COPY . .
# Build the application using Maven
RUN mvn package -DskipTests
# Use the official OpenJDK 17 image as the final image
FROM eclipse-temurin:17.0.6_10-jdk@sha256:13817c2faa739c0351f97efabed0582a26e9e6745a6fb9c47d17f4365e56327d
# Set the working directory inside the container
WORKDIR /app
# Copy the JAR file and other necessary files to the container
COPY --from=builder /app/report-automation.jar /app/report-automation.jar
COPY --from=builder /app/src/main/resources/META-INF/MANIFEST.MF /app/META-INF/MANIFEST.MF
COPY --from=builder /app/src/main/resources/config.properties /app/config.properties
COPY --from=builder /app/pom.xml /app/pom.xml
COPY --from=builder /app/testng.xml /app/testng.xml
COPY --from=builder /app/application.properties /app/application.properties
COPY --from=builder /app/Configuration.xlsm /app/Configuration.xlsm
COPY --from=builder /app/apache-maven-3.9.6/ /app/apache-maven-3.9.6/
COPY --from=builder /app/Downloads /app/Downloads
COPY --from=builder /app/Downloaded-Report /app/Downloaded-Report
COPY --from=builder /app/Logs /app/Logs
COPY --from=builder /app/Reports /app/Reports
COPY --from=builder /app/drivers /app/drivers
# Expose the port (if your application listens on a specific port)
EXPOSE 8080
# Set the entry point for the container
ENTRYPOINT ["java", "-Xmx512m", "-Xms256m", "-jar", "report-automation.jar"]`]]()
The application's hierarchical structure is mentioned below.
If that does not work, cut that long chained command where you install the chrome drivers in to smaller steps. I think that it where it fails?
If that does not work, remove the code from the place where it starts to fail, build the image and start the image interactive so you are in its command line. From there run the troubled code. It might give you some more clues.