【问题标题】:Run Maven project inside Docker Container with run time parameters使用运行时参数在 Docker 容器中运行 Maven 项目
【发布时间】:2020-07-15 14:51:39
【问题描述】:

我有一个使用 Maven 构建的 Testng Selenium 项目。我正在使用这样的 Maven Surefire 插件运行这个 maven 项目:

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M3</version>
            <configuration>
                <forkMode>never</forkMode>
                <useFile>true</useFile>
                <testFailureIgnore>true</testFailureIgnore>
                <!-- Suite testng xml file to consider for test execution-->
                <suiteXmlFiles>
                    <suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>

我需要做什么?
我需要在 Docker 容器中运行这个 Selenium 项目。我需要将完整的源代码移动到容器中并从那里运行它。在运行时,我将传递 testng xml 文件的路径,该特定测试应该单独运行。运行后,我需要将结果从 docker 镜像到本地系统(我们可以使用 docker cp ...)。

到目前为止我做了什么?
我用 maven、chrome、chromedriver 创建了一个 docker 映像。在运行时,我传递了 testng XML 文件路径,并且正如预期的那样,单独的特定测试用例正在运行。但.... 程序完成后,docker 容器将关闭。 docker ps 显示没有正在运行的容器。所以,我看不到报告。

我想要什么?
所以,我想要一种方法来避免容器在执行后被关闭,以便我可以进入 docker 容器并查看报告。

我的 Dockerfile:

FROM kshivaprasad/java
RUN apt-get update
RUN apt-get upgrade --fix-missing -y
RUN apt-get install -y curl
RUN apt-get install -y p7zip \
    p7zip-full \
    unace \
    zip \
    unzip

# Install Chrome for Selenium
RUN curl http://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_83.0.4103.116-1_amd64.deb -o /chrome.deb
RUN dpkg -i /chrome.deb || apt-get install -yf
RUN rm /chrome.deb

# Install chromedriver for Selenium
RUN mkdir -p /app/bin
RUN curl https://chromedriver.storage.googleapis.com/83.0.4103.39/chromedriver_linux64.zip -o /tmp/chromedriver.zip \
    && unzip /tmp/chromedriver.zip -d /app/bin/ \
    && rm /tmp/chromedriver.zip

ARG MAVEN_VERSION=3.6.3

# 2- Define a constant with the working directory
ARG USER_HOME_DIR="/root"

# 3- Define the SHA key to validate the maven download
ARG SHA=c35a1803a6e70a126e80b2b3ae33eed961f83ed74d18fcd16909b2d44d7dada3203f1ffe726c17ef8dcca2dcaa9fca676987befeadc9b9f759967a8cb77181c0

# 4- Define the URL where maven can be downloaded from
ARG BASE_URL=http://apachemirror.wuchna.com/maven/maven-3/${MAVEN_VERSION}/binaries

# 5- Create the directories, download maven, validate the download, install it, remove downloaded file and set links
RUN mkdir -p /usr/share/maven /usr/share/maven/ref \
  && echo "Downlaoding maven" \
  && curl -fsSL -o /tmp/apache-maven.tar.gz ${BASE_URL}/apache-maven-${MAVEN_VERSION}-bin.tar.gz \
  \
  && echo "Checking download hash" \
  && echo "${SHA}  /tmp/apache-maven.tar.gz" | sha512sum -c - \
  \
  && echo "Unziping maven" \
  && tar -xzf /tmp/apache-maven.tar.gz -C /usr/share/maven --strip-components=1 \
  \
  && echo "Cleaning and setting links" \
  && rm -f /tmp/apache-maven.tar.gz \
  && ln -s /usr/share/maven/bin/mvn /usr/bin/mvn

# 6- Define environmental variables required by Maven, like Maven_Home directory and where the maven repo is located
ENV MAVEN_HOME /usr/share/maven
ENV MAVEN_CONFIG "$USER_HOME_DIR/.m2"

COPY src /app/src
COPY pom.xml /app
COPY testng /app/testng
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /app/bin/chromedriver
#RUN mvn -f /app/pom.xml clean package
ENTRYPOINT ["/entrypoint.sh"]

entrypoint.sh 文件用于在运行时发送参数。它包括:

#!/bin/sh
mvn -f /app/pom.xml clean install -DsuiteXmlFile=$1

我如何运行它?

docker build -t my_image .
docker run -it my_image module/testng.xml

【问题讨论】:

    标签: java docker selenium maven


    【解决方案1】:

    对于快速上市来说,这个过程会非常耗时。我建议使用 zalenium https://opensource.zalando.com/zalenium/ 在 docker 容器中运行您的 selenium 脚本,它还具有各种功能,例如查看正在进行的执行和检索报告。

    【讨论】:

    • 我需要的一些关键功能是:并行执行,能够跨不同的浏览器版本和屏幕尺寸运行。但是对于 Zalenium,跨不同浏览器版本运行似乎是不可能的 - github.com/zalando/zalenium/issues/180。能否请您以其他方式告诉我?
    【解决方案2】:

    我只需要在 shell 文件 (entrypoint.sh) 中使用 bash 命令。对我来说效果很好。

    #!/bin/sh
    mvn -f /app/pom.xml clean install -DsuiteXmlFile=$1
    /bin/bash
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2020-03-29
      • 1970-01-01
      • 2014-05-04
      相关资源
      最近更新 更多