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 in 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.

/workspace/report-automation$ tree -L 2
.
├── apache-maven-3.9.6 //Bundled maven
│   
├── application.properties
├
├── Configuration.xlsm
├── Dockerfile
├── Downloaded-Report
│   └── SalesReport.csv  //Downloaded report
├── Downloads
├── drivers
│   ├── chromedriver-linux64 //Bundled chrome drivers
│   └── chromedriver-mac-arm64
├── Logs
│   └── TestLogs.log
├── out
│   └── artifacts
├── pom.xml
├── README.md
├── report-automation.jar  // Applications jar file
├── Reports
│   └── ExtentReport.html
├── src
│   └── main
├── target
│   ├── classes
│   ├── generated-sources
│   ├── maven-archiver
│   ├── maven-status
│   ├── surefire-reports
│   
└── testng.xml

This error is also thrown when the docker file is being built.

randunu@randunu:/rezsystem/workspace/report-automation$ sudo docker build -t report-automation .

[+] Building 358.0s (9/27)                                                                                                                                              
 => [internal] load build definition from Dockerfile                                                                                                               0.0s
 => => transferring dockerfile: 2.48kB                                                                                                                             0.0s
 => [internal] load .dockerignore                                                                                                                                  0.0s
 => => transferring context: 2B                                                                                                                                    0.0s
 => [internal] load metadata for docker.io/library/eclipse-temurin:17.0.6_10-jdk@sha256:13817c2faa739c0351f97efabed0582a26e9e6745a6fb9c47d17f4365e56327d           0.0s
 => [internal] load metadata for docker.io/library/maven:3.9.6-eclipse-temurin-17                                                                                  3.0s
 => [internal] load build context                                                                                                                                  0.1s
 => => transferring context: 51.39kB                                                                                                                               0.0s
 => CANCELED [builder 1/4] FROM docker.io/library/maven:3.9.6-eclipse-temurin-17@sha256:29a1658b1f3078e07c2b17f7b519b45eb47f65d9628e887eac45a8c5c8f939d4         354.9s
 => => resolve docker.io/library/maven:3.9.6-eclipse-temurin-17@sha256:29a1658b1f3078e07c2b17f7b519b45eb47f65d9628e887eac45a8c5c8f939d4                            0.0s
 => => sha256:408d89a73e0dc1b14cfbc003f5729720855da4003a266ebf841cf2c298a8d144 2.41kB / 2.41kB                                                                     0.0s
 => => sha256:5e5d1bccc5440d3a24f4a620704b9e687b4163c6c872fcc8e812e200c9bbac58 17.46MB / 17.46MB                                                                 120.0s
 => => sha256:29a1658b1f3078e07c2b17f7b519b45eb47f65d9628e887eac45a8c5c8f939d4 1.21kB / 1.21kB                                                                     0.0s
 => => sha256:b86805e902bf7f1eb3cd9d7f99f0cb391aacca9fe5653ba2c8279d2f0a4e2c23 7.91kB / 7.91kB                                                                     0.0s
 => => sha256:4a023cab5400feb5c1ab725beb8345ddb0e3200314004b56677a5eee2e8c86cf 30.44MB / 30.44MB                                                                 236.3s
 => => sha256:d59fd278c1b4ffe1727be6ccba42125e8f4db57e660e795cce19889d2c776457 61.87MB / 145.10MB                                                                354.9s
 => => sha256:c97285723537ab7921fca4d081c256e501adfdaa8992d04637992075f4cea392 173B / 173B                                                                       122.1s
 => => sha256:a3ba11f7aaaedad962216812fe84aee9061aeabac6932f0274a1d204eb96e8e8 734B / 734B                                                                       122.5s
 => => sha256:67f99c2668af1d0eced6d61dad057d7beffe543a8e8370d5c0d3fe8682e059a3 19.00MB / 19.00MB                                                                 275.5s
 => => sha256:45f480637770dbb740623c0c7f827d44bbacc9204faf3d07b4135d54a2bee043 9.48MB / 9.48MB                                                                   293.0s
 => => extracting sha256:4a023cab5400feb5c1ab725beb8345ddb0e3200314004b56677a5eee2e8c86cf                                                                          0.5s
 => => extracting sha256:5e5d1bccc5440d3a24f4a620704b9e687b4163c6c872fcc8e812e200c9bbac58                                                                          0.4s
 => => sha256:58c3491a14ebc973960cbda9f2eea22537785a3cb07e81487bd1115bad4a8278 852B / 852B                                                                       276.5s
 => => sha256:4712dfa85971124b8c3507fbba4c02441e5754f1517162926e600afd3fc5404b 358B / 358B                                                                       276.9s
 => => sha256:fc06d68d71ba375a9ceed165c0c04b09b1a8cc193146888663b005660fc25013 156B / 156B                                                                       277.4s
 => [stage-1  1/18] FROM docker.io/library/eclipse-temurin:17.0.6_10-jdk@sha256:13817c2faa739c0351f97efabed0582a26e9e6745a6fb9c47d17f4365e56327d                   0.0s
 => CACHED [stage-1  2/18] WORKDIR /app                                                                                                                            0.0s
 => ERROR [stage-1  3/18] RUN apt-get update &&     apt-get install -y wget unzip gnupg &&     wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.p  354.9s
------                                                                                                                                                                  
 > [stage-1  3/18] RUN apt-get update &&     apt-get install -y wget unzip gnupg &&     wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - &&     sh -c 'echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list' &&     apt-get update &&     apt-get install -y google-chrome-stable &&     apt-get clean &&     rm -rf /var/lib/apt/lists/*:                                                                                     
#0 1.293 Get:1 http://archive.ubuntu.com/ubuntu jammy InRelease [270 kB]                                                                                                
#0 3.757 Get:2 http://security.ubuntu.com/ubuntu jammy-security InRelease [129 kB]                                                                                      
#0 6.756 Get:3 http://security.ubuntu.com/ubuntu jammy-security/multiverse amd64 Packages [44.7 kB]
#0 7.154 Get:4 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [128 kB]
#0 7.157 Get:5 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages [2,118 kB]
#0 10.38 Get:6 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [127 kB]
#0 13.30 Get:7 http://archive.ubuntu.com/ubuntu jammy/main amd64 Packages [1,792 kB]
#0 25.08 Get:8 http://security.ubuntu.com/ubuntu jammy-security/universe amd64 Packages [1,131 kB]
#0 34.61 Get:9 http://security.ubuntu.com/ubuntu jammy-security/restricted amd64 Packages [2,787 kB]
#0 53.25 Get:10 http://archive.ubuntu.com/ubuntu jammy/multiverse amd64 Packages [266 kB]
#0 60.66 Get:11 http://archive.ubuntu.com/ubuntu jammy/restricted amd64 Packages [164 kB]
#0 65.24 Get:12 http://archive.ubuntu.com/ubuntu jammy/universe amd64 Packages [17.5 MB]
#0 300.8 Get:13 http://archive.ubuntu.com/ubuntu jammy-updates/multiverse amd64 Packages [51.8 kB]
#0 301.5 Get:14 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [2,396 kB]
#0 323.5 Get:15 http://archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages [1,421 kB]
#0 332.4 Get:16 http://archive.ubuntu.com/ubuntu jammy-updates/restricted amd64 Packages [2,884 kB]
#0 352.6 Get:17 http://archive.ubuntu.com/ubuntu jammy-backports/universe amd64 Packages [33.7 kB]
#0 353.4 Get:18 http://archive.ubuntu.com/ubuntu jammy-backports/main amd64 Packages [81.0 kB]
#0 354.0 Fetched 33.3 MB in 5min 54s (94.2 kB/s)
#0 354.0 Reading package lists...
#0 354.8 E: Problem executing scripts APT::Update::Post-Invoke 'rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true'
#0 354.8 E: Sub-process returned an error code
------
ERROR: failed to solve: executor failed running [/bin/sh -c apt-get update &&     apt-get install -y wget unzip gnupg &&     wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - &&     sh -c 'echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list' &&     apt-get update &&     apt-get install -y google-chrome-stable &&     apt-get clean &&     rm -rf /var/lib/apt/lists/*]: exit code: 100

any suggestions on how this error may be handled?

A good practice is running a container from the base image and executing the commands in the container manually so you can investigate it as a Linux / APT issue.

On the other hand if you search for the error message you can find something like this:

That mentiones updating Docker. You have a Docker version which is not supported anymore and an operating system for which standard support ended as well. That is why it was removed from the list of supported Ubuntu versions, although it was after the release of the Docker version you are using.

It would be also important to know how you installed Docker. There are multiple ways and the version number itself does not show it. If you installed it from Ubuntu’s own repository, that package is maintained by Ubuntu as well as the snap package.

There were also occasions when An APT command didn’t work because it was not compatible with the Docker Engine so some system calls could not be handled by Docker. That is another reason of using uptodate Docker CE

Hello

Based on your question (your post’s title), you can perhaps find my blog post useful : Introduction to Behat | Christophe Avonture

It’s for running behat, a tool similar to selenium.

I give a dockerfile with everything in it i.e. Chrome and Chromedriver.

I hope it’ll give you some inspirations.