I'm pretty addicted to it, so I'll summarize it.
docker-maven-plugin is released from the following two.
This time I used docker-maven-plugin of fabric8io.
buid has two modes.
Regarding 1, it is as follows.
<configuration>
<images>
<image>
<alias>service</alias>
<name>fabric8/docker-demo:${project.version}</name>
<build>
<from>java:8</from>
<assembly>
<descriptor>docker-assembly.xml</descriptor>
</assembly>
<cmd>
<shell>java -jar /maven/service.jar</shell>
</cmd>
</build>
</image>
</configuration>
2 is as follows.
<configuration>
<images>
<image>
<name>${project.name}</name>
<build>
<dockerFileDir>${project.basedir}</dockerFileDir>
</build>
</image>
</images>
</configuration>
I chose the method of 2. The reason is
Create a Dockerfile directly under the project and specify the bid reference destination.
project/
┝src
┝Dockerfile
┝pom.xml
Dockerfile
FROM amazoncorretto:11-alpine
COPY maven /maven/jar/
COPY ${flyway.sqldir}/V1__Initialize.sql sql/
COPY ${flyway.confdir}/flyway.conf conf/
CMD java -XX:MaxRAMPercentage=75 -jar \
maven/jar/${project.name}-${project.version}.jar \
-url=jdbc:mysql://host.docker.internal:${db.port}/${db.name} \
-user=${db.username} \
-password=${db.password} \
-configFiles=conf/flyway.conf \
-jarDirs="" \
migrate
(1) Place the created jar in the second layer.
COPY maven /maven/jar/
COPY ${flyway.sqldir}/V1__Initialize.sql sql/
COPY ${flyway.confdir}/flyway.conf conf/
As a mechanism of flyway, when searching for sql and conf files, go to the folder two levels above. Make the directory structure as follows.
/
├sql
│ └V1__Initialize.sql
├conf
│ └flyway.conf
├maven
└jar
└flyway.jar
(2) Use properties of pom.xml when copying sql and conf files.
pom.xml
...
<properties>
<flyway.sqldir>${project.basedir}/src/main/resources/db/migration</flyway.sqldir>
<flyway.confdir>${project.basedir}/src/local/resources</flyway.confdir>
</properties>
...
If you write it directly in Dockerfile, it will refer to an unintended path. Fix the path declared and referenced in properties.
③ Remove famous files when creating flyway jar
pom.xml
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
...
Install the above plugin. Without this, a SecurityException will occur.
mvn package docker:buid
A docker Image is created.
docker run ${project.name}
Is executed.
http://dmp.fabric8.io/#docker:build
https://stackoverflow.com/questions/43525202/how-to-copy-files-from-an-absolute-path-to-docker-image-using-docker-maven-plugi
https://www.tssol.net/blog/2019/10/26/java_lang_securityexception_invalid-signature-file-digest-for-manifest-main-attributes/
https://oboe2uran.hatenablog.com/entry/2015/10/21/120700
Recommended Posts