问题描述
# openjdk:11-jre include 'wget' and 'curl',while openjdk:11-jre-slim does not
FROM openjdk:11-jre
ENV VERSION=2.28.1
ENV DIR=/home/wiremock
ENV JAR=wiremock-jre8-standalone-$VERSION.jar
ENV FULLPATH=$DIR/$JAR
# grab wiremock standalone jar
RUN mkdir -p $DIR
RUN useradd --no-log-init -r wiremock
RUN wget https://repo1.maven.org/maven2/com/github/tomakehurst/wiremock-jre8-standalone/$VERSION/$JAR \
-O $FULLPATH
workdir $DIR
# copy stub files to container
copY --chown=wiremock:root src/main/resources/stubs/__files __files/
copY --chown=wiremock:root src/main/resources/stubs/mappings mappings/
USER wiremock
EXPOSE 8080 8443
# healthcheck(wait 10s to start,then wait 10s interval and check,if wait more than 1s,is fail. Retry 3 times
# with same interval before marking as unhealthy)
HEALTHCHECK --start-period=10s --interval=10s --timeout=1s --retries=3 \
CMD curl http://localhost:8080/__admin && curl -k https://localhost:8443/__admin || exit 1
CMD java -jar $FULLPATH --https-port 8443 --verbose
现在,我找不到使用 maven-jib-plugin 的正确方法。
依赖项将作为依赖项复制,而不是通过 wget
下载:
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8-standalone</artifactId>
<version>${wiremock.standalone.version}</version>
<scope>runtime</scope>
</dependency>
但是我不确定是否所有的事情都可以在 jib 中完成,比如创建用户等等。
现在,我有了这个 pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>foo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>app</artifactId>
<properties>
<docker.registry>xxx</docker.registry>
<docker.base.image>${docker.registry}/base/openjdk11:latest</docker.base.image>
<docker.image>app</docker.image>
<https.port>8443</https.port>
<work.dir>/home/wiremock</work.dir>
<jar>wiremock-jre8-standalone-${wiremock.standalone.version}.jar</jar>
<full.path>${work.dir}/${jar}</full.path>
</properties>
<dependencies>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8-standalone</artifactId>
<version>${wiremock.standalone.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/libs</outputDirectory>
<includeArtifactIds>wiremock-jre8-standalone</includeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<from>
<image>${docker.base.image}</image>
</from>
<to>
<image>${docker.image}</image>
<tags>
<tag>latest</tag>
</tags>
</to>
<exTradirectories>
<paths>
<path>
<from>libs/</from>
<into>${work.dir}</into>
</path>
<path>
<from>src/main/resources/stubs/__files</from>
<into>${work.dir}/__files</into>
</path>
<path>
<from>src/main/resources/stubs/mappings</from>
<into>${work.dir}/mappings</into>
</path>
</paths>
<permissions>
<permission>
<file>${work.dir}/**/*.*</file>
<mode>744</mode>
</permission>
</permissions>
</exTradirectories>
<container>
<user>wiremock:root</user>
<appRoot>${work.dir}</appRoot>
<workingDirectory>${work.dir}</workingDirectory>
<ports>
<port>8080</port>
<port>${https.port}</port>
</ports>
<args> <!-- dockerfile CMD part-->
<arg>-jar</arg>
<arg>${full.path}</arg>
<arg>--https-port</arg>
<arg>${https.port}</arg>
<arg>--verbose</arg>
</args>
<mainClass>com.github.tomakehurst.wiremock.standalone.wiremockServerRunner</mainClass>
</container>
<!-- <containerizingMode>packaged</containerizingMode> -->
</configuration>
</plugin>
</plugins>
</build>
</project>
当我运行 mvn jib:dockerBuild
时,镜像构建成功(好消息!),我在日志中看到依赖 jar 被复制到 libs
,然后复制到容器中。
但是当我运行它时,我看到错误:
docker: Error response from daemon: unable to find user wiremock: no matching entries in passwd file.
另外,我认为 HEALTHCHECK 也没有完成。
我可以跳过新用户创建和健康检查,因为现在不推荐也不支持它们(https://github.com/GoogleContainerTools/jib/issues/676,https://github.com/GoogleContainerTools/jib/issues/1029),但是当我想像在 dockerfile 中那样运行 jar 时,我看到另一个错误:
$ docker run -p 8080:8080 -p 8443:8443 app
Exception in thread "main" joptsimple.UnrecognizedOptionException: j is not a recognized option
at joptsimple.OptionException.unrecognizedOption(OptionException.java:108)
at joptsimple.OptionParser.validateOptionCharacters(OptionParser.java:633)
at joptsimple.OptionParser.handleShortOptionCluster(OptionParser.java:528)
at joptsimple.OptionParser.handleShortOptionToken(OptionParser.java:523)
at joptsimple.OptionParserState$2.handleArgument(OptionParserState.java:59)
at joptsimple.OptionParser.parse(OptionParser.java:396)
at com.github.tomakehurst.wiremock.standalone.CommandLineOptions.<init>(CommandLineOptions.java:179)
at com.github.tomakehurst.wiremock.standalone.wiremockServerRunner.run(wiremockServerRunner.java:52)
at com.github.tomakehurst.wiremock.standalone.wiremockServerRunner.main(wiremockServerRunner.java:134)
我看到图像是这样构建的:
[INFO] Container entrypoint set to [java,-cp,@/home/wiremock/jib-classpath-file,com.github.tomakehurst.wiremock.standalone.wiremockServerRunner]
[INFO] Container program arguments set to [-jar,/home/wiremock/wiremock-jre8-standalone-2.28.1.jar,--https-port,8443,--verbose]
如何正确地将此 dockerfile 转换为 XML 中的 jib 配置?
解决方法
好吧,我终于找到了:
我应该设置 <container><entrypoint>
,而不是 <args>
。不需要 <mainClass>
。
<container>
...
<entrypoint>java,-jar,${full.path},--https-port,${https.port},--verbose</entrypoint>
</container>
<!-- <containerizingMode>packaged</containerizingMode>-->
然后入口点是这样的:
[INFO] Container entrypoint set to [java,/home/wiremock/wiremock-jre8-standalone-2.28.1.jar,8443,--verbose]
Wiremock jar 运行正常。