maven 打包 spring boot 生成docker 鏡像


1.所使用材料 ,spring boot 項目 基於maven ,maven 工具, docker工具

ps:為啥使用 docker 公司微服務需要啟動太多,有兩個優點吧! 1.方便管理,2.減少服務占用內存量

2.上手

a.新建Dockerfile文件如下目錄

file

b.Dockerfile文件內容
				FROM openjdk:8-jdk-alpine
				VOLUME /tmp
				ARG JAR_FILE
				COPY ${JAR_FILE} app.jar
				ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
				ps: 
						FROM: 第一條指令必須是FROM指令 (環境依賴 jdk 版本等)
						VOLUME: 作用是創建在本地主機或其他容器可以掛載的數據卷,用來存放數據。
						ARG: 定義一個變量
						JAR_FILE: 為pom文件中項目定義的路徑地址 
						COPY: 復制本地主機src目錄或文件到容器的desc目錄,desc不存在時會自動創建。
						ENTRYPOINT: 每個Dockerfile中只能有一個ENTRYPOINT,當有多個時最后一個生效。
c.pom文件修改

file

			 <properties>
						<docker.image.prefix>springio</docker.image.prefix>
				</properties>
				
				<plugin>
            <groupId>com.spotify</groupId>
            <artifactId>dockerfile-maven-plugin</artifactId>
            <version>1.3.6</version>
            <configuration>
                <repository>${docker.image.prefix}/${project.artifactId}</repository>
                <buildArgs>
                    <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                </buildArgs>
            </configuration>
        </plugin>
    </plugins>
這個時候 項目中文件修改告一段落
d.需要在maven中添加配置(不加入可能會導致 docker:build 不識別) 否則會報 No plugin found for prefix 'docker' in the current project and in the plugin groups...

file

file

e.docker 配置需要更改(勾選)否則會報 localhost:2375 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect

file

file

3.最后命令

			a.mvn package dockerfile:build(項目路徑下)
			b.docker 查看鏡像 docker images
			c.運行項目 docker run -d -p 8099:8099 springio/xlw_demo

4.總結

			a.問題 如果代碼更改,docker鏡像中還是老代碼,需要手動刪除很不方便
			b.啟動的時候需要命令號窗口啟動不方便
			c.打開軟件時能否跟隨docker一起啟動
![blockchain](https://maziye.oss-cn-beijing.aliyuncs.com/1569635074%281%29.jpg "公眾號")
公眾號即可獲得"Spring Cloud"教程


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM