I have a dockerfile which is like:
# 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
Now, I cannot find a proper way to do it with maven-jib-plugin.
The dependency will be copied as a dependency, not downloaded with wget
:
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8-standalone</artifactId>
<version>${wiremock.standalone.version}</version>
<scope>runtime</scope>
</dependency>
But I am not sure if all things can be done in jib, like creating user and so.
Now, I have this 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>
When I run mvn jib:dockerBuild
, the image is built successfully(good news!), I see in log the dependency jar is copied to libs
, and then copied into container.
But then when I run it, I see error:
docker: Error response from daemon: unable to find user wiremock: no matching entries in passwd file.
So jib did not create the user for me.
Also, I think HEALTHCHECK is not done, neither.
I can skip new user creation and healthcheck, as they are not recommended and not supported now(https://github.com/GoogleContainerTools/jib/issues/676, https://github.com/GoogleContainerTools/jib/issues/1029), but when I want to run the jar like I do in dockerfile, I see another error:
$ 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)
I see the image is built like this:
[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]
How can I properly translate this dockerfile into a jib configuration in XML?
Well, at last I found it:
I should set <container><entrypoint>
, and not <args>
. <mainClass>
is not needed.
<container>
...
<entrypoint>java,-jar,${full.path},--https-port,${https.port},--verbose</entrypoint>
</container>
<!-- <containerizingMode>packaged</containerizingMode>-->
Then the entrypoint is like:
[INFO] Container entrypoint set to [java, -jar, /home/wiremock/wiremock-jre8-standalone-2.28.1.jar, --https-port, 8443, --verbose]
And the wiremock jar is running correctly.