spring boot项目打包为docker image

19.05.20更新:

pom文件更新为:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>dockerfile-maven-plugin</artifactId>
    <version>1.4.10</version>
    <configuration>
        <repository>com.myname.demo/demo1</repository>
        <outputDirectory>${project.build.directory}</outputDirectory>
        <contextDirectory>${project.basedir}</contextDirectory>
        <dockerfile>src/main/resources/docker/Dockerfile</dockerfile>
        <tag>v1</tag>
        <buildArgs>
            <JAR_FILE>${project.build.finalName}-myname.jar</JAR_FILE>
        </buildArgs>
    </configuration>
    <executions>
        <execution>
        <id>default</id>
        <goals>
            <goal>build</goal>
            <goal>push</goal>
        </goals>
        </execution>
    </executions>
</plugin>

建议用最新版1.4.10,加入了指定Dockerfile位置的更新.19.05.20

maven打包命令:

1
2
mvn clean package -DskipTests
mvn dockerfile:build

以下为旧docker-maven-plugin配置

pom文件里加入:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                    <configuration>
                        <classifier>myname</classifier>
                        <mainClass>com.myname.demo.demo1.BootApplication</mainClass>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <version>1.2.0</version>
            <configuration>
                <imageName>com.myname.demo/demo1</imageName>
                <dockerDirectory>src/main/resources/docker</dockerDirectory>
                <resources>
                    <resource>
                        <targetPath>/</targetPath>
                        <directory>${project.build.directory}</directory>
                        <include>${project.build.finalName}-myname.jar</include>
                    </resource>
                </resources>
                <forceTags>true</forceTags>
                <imageTags>
                    <imageTag>v1</imageTag>
                </imageTags>
            </configuration>
        </plugin>

    </plugins>
</build>

创建src/main/resources/docker目录,加入Dockerfile文件:

1
2
3
4
5
FROM frolvlad/alpine-java:jdk8-slim
ADD ./demo1-1.0.0-myname.jar /app.jar
ENV SPRING_PROFILE=""
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS $SPRING_PROFILE -jar /app.jar" ]

maven打包命令:

1
2
mvn clean package -DskipTests
mvn docker:build -DdockerImageTags=v1

创建

运行项目:

1
docker run -d --name demo1 -p 8080:8080 -v /home/user/profiles:/profiles -e SPRING_PROFILE="-Dspring.config.location=file:/profiles/application-dev.yml,file:/profiles/application.yml -Dspring.profiles.active=dev" -e JAVA_OPTS="-Xms2g -Xmx4g" com.myname.demo/demo1:v1

1
2
3
4
5
6
7
8
9
# 查看容器日志
docker logs demo1

# 在容器内部执行命令
docker exec -it kycserver ps -ef
docker exec -it kycserver ls /profiles

# 停止容器
docker stop demo1 && docker rm demo1

导出docker image:
docker save -o /home/user/demo/demo1/target/demo1.img com.myname.demo/demo1:v1

导入docker image:
docker load -i demo1.img

参考:
How to copy Docker images from one host to another without using a repository
Added support for supplying a custom Dockerfile location on build #89

arch linux 安装配置笔记
科学上网
comments powered by Disqus