实战:windows下使用docker部署springboot web demo项目

一: 新建一个SpringBoot-web 项目

  • 在pom.xml 引入Docker maven plugin

  • <build>
        <plugins>
            ...
            <!-- Docker maven plugin -->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.4.10</version>
                <configuration>
                    <tag>${project.build.finalName}-demo</tag>
                    <buildArgs>
                        <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
                    </buildArgs>
                </configuration>
            </plugin>
            <!-- Docker maven plugin -->
        </plugins>
    </build>
    

二: 安装Docker java 8 镜像

docker  pull  java:8

三: 添加Dockerfile

在pom.xml 平级目录新建Dockerfile文件

FROM java:latest
VOLUME /tmp
ARG JAR_FILE
ADD target/${JAR_FILE} /app.jar
#暴露端口
EXPOSE 8088
ENV JVM_OPTS="-Xmx1344M -Xms1344M -Xmn448M -Xloggc:/data/log/demo-jvm.log -XX:+UseConcMarkSweepGC -Dfile.encoding=utf-8 -Duser.timezone=Asia/Shanghai -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70  -XX:+CMSClassUnloadingEnabled -XX:+ParallelRefProcEnabled -XX:+CMSScavengeBeforeRemark -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError -XX:+PrintClassHistogramAfterFullGC -XX:+PrintCommandLineFlags -XX:+PrintGCApplicationConcurrentTime -XX:+PrintGCApplicationStoppedTime -XX:+PrintTenuringDistribution -XX:+PrintHeapAtGC -XX:HeapDumpPath=/tmp/demo-heapdump.hprof -XX:MaxMetaspaceSize=256M -XX:MetaspaceSize=256M"

ENTRYPOINT java ${JVM_OPTS} -Djava.security.egd=file:/dev/./urandom -jar /app.jar

四:使用 DockerFile 构建镜像
执行命令:
mvn clean package dockerfile:build

查看镜像结果:
docker images

五: 运行docker镜像
执行命令:
docker run -d -p 8088:8080 --name docker-demo admol/docker-demo:docker-demo

六:查看运行结果

  • 浏览器验证
    • 访问 http://localhost:8088/hello
  • docker 验证
    • 运行docker ps 命令查看运行中的容器
  • docker 容器日志验证
    • docker logs containerid -f



七:进入容器
windows 环境下:
winpty docker exec -it containerid sh
查看进程ID
jps
跟踪GC执行情况
jstat -gccause pid 3s
退出容器命令
exit

遇到的问题
问题1:打包出现错误:

[ERROR] No plugin found for prefix 'docker' in the current project and in the plugin groups...

解决办法: 修改 maven 的配置文件 settings.xml

<pluginGroups>
	<pluginGroup>com.spotify</pluginGroup>
</pluginGroups>

问题2:打包出现Connection refused 错误:

[ERROR] Failed to execute goal com.spotify:dockerfile-maven-plugin:1.4.10:build (default-cli) on project docker-demo: Could not build image: java.util.concurrent.ExecutionException: com
.spotify.docker.client.shaded.javax.ws.rs.ProcessingException: com.spotify.docker.client.shaded.org.apache.http.conn.HttpHostConnectException: Connect to localhost:2375 [localhost/127.0
.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect -> [Help 1]

解决办法: 右键运行中的docker,Settings, 勾选 Expose daemon on tcp://localhost:2375 without TLS; 然后重启docker

posted @ 2020-04-29 10:55  Admol  阅读(1902)  评论(1编辑  收藏  举报