Docker,  技术

How to generate docker image

Fabric8 maven plugin

The first way to do is using fabric8 maven plugin (https://dmp.fabric8.io/) . We have something like this to define the image in pom.xml:

        <profile>
            <id>docker-image</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>io.fabric8</groupId>
                        <artifactId>docker-maven-plugin</artifactId>
                        <configuration>
                            <images>
                                <image>
                                    <name>z/daco</name>
                                    <build>
                                        <cleanup>none</cleanup>
                                        <from>XXX/centos7-openjdk8:5</from>
                                        <maintainer>XX</maintainer>
                                        <tags>
                                            <tag>latest</tag>
                                            <tag>${project.version}</tag>
                                            <tag>${project.version}-${git.commit.id.abbrev}</tag>
                                        </tags>
                                        <runCmds>
                                            <run>curl -v -u deployer:Jf8f907Q https://nexus.XX.io/nexus/service/local/repositories/security/content/kafka/client/certs/kafka.client.truststore.jks -o /maven/kafka.client.truststore.jks</run>
                                        </runCmds>
                                        <assembly>
                                            <descriptor>${basedir}/src/main/docker/fabric8/assembly.xml</descriptor>
                                        </assembly>
                                        <ports>
                                            <port>${dataconnect.port.service}</port>
                                            <port>${dataconnect.port.admin}</port>
                                        </ports>
                                        <cmd>
                                            <shell>java $JAVA_OPTS -jar /maven/${project.artifactId}-${project.version}-shaded.jar $DROPWIZARD_COMMAND /maven/XX-application.yml</shell>
                                        </cmd>
                                    </build>
                                </image>
                            </images>
                        </configuration>
                        <executions>
                            <execution>
                                <id>build-docker-image</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>build</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

And assembly file:

<assembly xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>${project.artifactId}</id>
    <files>
        <file>
            <source>target/${project.artifactId}-${project.version}-shaded.jar</source>
            <outputDirectory>/</outputDirectory>
        </file>
        <file>
            <source>XX-application.yml</source>
            <outputDirectory>/</outputDirectory>
        </file>
        <file>
            <source>src/main/resources/liquibase/db.changelog-0.0.1.xml</source>
            <outputDirectory>/liquibase</outputDirectory>
        </file>
        <file>
            <source>src/main/resources/liquibase/migrations.xml</source>
            <outputDirectory>/liquibase</outputDirectory>
        </file>
    </files>
    <fileSets>
        <fileSet>
            <directory>src/main/resources/dedicatedresources</directory>
            <outputDirectory>/dedicatedresources</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

Framework build-in target

If you are using Sprint boot, it has the maven docker image target mvn spring-boot:build-image. Based on the doc here it has some benefit like using exploded folder other than fat jar; but I guess it may not flexible enough.

Write your own dockerfile

This is more direct and I guess it has most flexibility. Here is one example:

FROM openjdk:8-jdk-alpine
EXPOSE 8080
ARG JAR_FILE=target/my-application.jar
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

Leave a Reply

Your email address will not be published. Required fields are marked *