[JAVA] Create a flyway jar with maven and docker build (migrate) with docker-maven-plugin

I'm pretty addicted to it, so I'll summarize it.

Docker-maven-plugin to use

docker-maven-plugin is released from the following two.

This time I used docker-maven-plugin of fabric8io.

to docker buid

buid has two modes.

  1. Describe from and cmd in pom.xml and buy method
  2. How to refer to another Dockerfile from pom.xml and buy it

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

How to migrate using Dockerfile

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

point

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

run migrate with docker run

mvn package docker:buid

A docker Image is created.

docker run ${project.name}

Is executed.


References

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

Create a flyway jar with maven and docker build (migrate) with docker-maven-plugin
Create a Java (Maven) project with VS Code and develop it on a Docker container
Build a Node-RED environment with Docker to move and understand
Create a Vue3 environment with Docker!
Build a Node.js environment with Docker
Create a Maven project with a command
Use Jenkins to build inside Docker and then create a Docker image.
Create a jar file with the command
Build a PureScript development environment with Docker
Create a MySQL environment with Docker from 0-> 1
Build a Wordpress development environment with Docker
Make a daily build of the TOPPERS kernel with Gitlab and Docker
Distributed transaction with SpringBoot + PostgreSql + mybatis + NarayanaJTA
Supports multi-port with SpringBoot
Change the port with SpringBoot
Try gRPC with Java, Maven
I tried UPSERT with PostgreSQL.
Easy library introduction with Maven!
Hello World with SpringBoot / Gradle
UnitTest with SpringBoot + JUnit + Mockito
Create a flyway jar with maven and docker build (migrate) with docker-maven-plugin
[Java] Create a jar file with both compressed and uncompressed with the jar command
[Copy and paste] Build a Laravel development environment with Docker Compose Part 2
How to create a server executable JAR and WAR with Spring gradle
[Copy and paste] Build a Laravel development environment with Docker Compose Participation
[Memo] Create a CentOS 8 environment easily with Docker
Build a Laravel / Docker environment with VSCode devcontainer
Create jupyter notebook with Docker and run ruby
Prepare a scraping environment with Docker and Java
Create a Spring Boot development environment with docker
Build Apache and Tomcat environment with Docker. By the way, Maven & Java cooperation
Build a development environment to create Ruby on Jets + React apps with Docker
Getting started with Maven (until you create a Java project and combine external libraries into a single executable JAR)
Build Elastic Stack with Docker and analyze IIS logs
Until you build a Nuxt.js development environment with Docker and touch it with VS Code
Create a Jar file with two lines of command
Create a Java (Gradle) project with VS Code and develop it on a Docker container
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
Easily build a Vue.js environment with Docker + Vue CLI
[Note] Build a Python3 environment with Docker in EC2
[Docker] How to update using a container on Heroku and how to deal with Migrate Error
How to quit Docker for Mac and build a Docker development environment with Ubuntu + Vagrant
[Note] Create a java environment from scratch with docker
Build WordPress environment with Docker (Local) and AWS (Production)
Until you build a project described in scala with Maven and execute it with the scala command.
Create a blog with Jekyll and GitHub Pages @ Theme setting
Maybe it works! Create an image with Docker and share it!
[Docker] Build a site on Hugo and publish it on GitHub
Create a Docker image with the Oracle JDK installed (yum
Create a blog with Jekyll and GitHub Pages @ Initial Settings
Build Docker Image lightweight and fast with CodeBuild with Santa Banner
Elasticsearch> Build with docker, get Twitter information and visualize with Kibana
Create Rails5 and postgresql environment with Docker and make pgadmin available
I tried to create a padrino development environment with Docker
Create a playground with Xcode 12
Build docker environment with WSL
Build DynamoDB local with Docker
Build a NAS with DLNA function at the speed of a second with Raspberry Pi and Docker Compose
Build Metabase with Docker on Lightsail and make it https with nginx
Build a SPA for Laravel 6.2 / Vue.js / Nginx / Mysql / Redis with Docker
How to make a jar file with no dependencies in Maven
Create a JVM for app distribution with JDK9 modules and jlink
[Swift] Create a project with Xcode (ver 12.1) and display "Hello, World!"
Create a high-performance enum with fields and methods like Java with JavaScript
Build a development environment for Django + MySQL + nginx with Docker Compose
Create a Docker Image for redoc-cli and register it on Docker Hub
Let's create a parameter polymorphic mechanism with Generic Dao and Hibernate
How to download and run a Jar package directly from the Maven repository with just the command line
Build a web application development environment that uses Java, MySQL, and Redis with Docker CE for Windows