SpringBoot打包成Docker镜像

发布时间:2024年01月23日

SpringBoot打包成Docker镜像

1、第一种方式

1.1 编写一个springboot项目并且打包成jar包

package com.example.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(){
        return "Hello World!!!";
    }
}
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {

		SpringApplication.run(DemoApplication.class, args);
	}

}

1.2 安装docker插件

Docker

1.3 项目打包

mvn package

1.4 在target目录下编写Dockerfile文件

FROM openjdk:8
COPY *.jar /app.jar
CMD ["--server.port=8080"]
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]

在这里插入图片描述

1.5 构建镜像

$ docker build -t demo:v1 .
[root@zsx target]# docker build -t demo:v1 .
Sending build context to Docker daemon  17.37MB
Step 1/5 : FROM openjdk:8
 ---> b273004037cc
Step 2/5 : COPY *.jar /app.jar
 ---> 71538f2a4725
Step 3/5 : CMD ["--server.port=8080"]
 ---> Running in 9a08516ce3b2
Removing intermediate container 9a08516ce3b2
 ---> 58ad7de95482
Step 4/5 : EXPOSE 8080
 ---> Running in 47ae06299ac5
Removing intermediate container 47ae06299ac5
 ---> 50c705487e20
Step 5/5 : ENTRYPOINT ["java","-jar","/app.jar"]
 ---> Running in 2252f518b4cb
Removing intermediate container 2252f518b4cb
 ---> 3da5156a3350
Successfully built 3da5156a3350
Successfully tagged demo:v1
$ docker images

在这里插入图片描述

1.5 运行

$ docker run -d -P --name spring-boot-demo demo:v1
$ curl http://192.168.136.195:49153/hello

在这里插入图片描述

2、第二种方式

2.1 SpringBoot项目

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.0.RELEASE</version>
	</parent>

	<groupId>com.example</groupId>
	<artifactId>spring-boot-docker</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-boot-docker</name>
	<description>spring-boot-docker</description>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
		<docker.image.prefix>springboot</docker.image.prefix>
	</properties>

	<dependencies>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<!-- Docker maven plugin start-->
			<plugin>
				<groupId>com.spotify</groupId>
				<artifactId>docker-maven-plugin</artifactId>
				<version>1.0.0</version>
				<configuration>
					<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
					<dockerDirectory>src/main/docker</dockerDirectory>
					<resources>
						<resource>
							<targetPath>/</targetPath>
							<directory>${project.build.directory}</directory>
							<include>${project.build.finalName}.jar</include>
						</resource>
					</resources>
				</configuration>
			</plugin>
			<!-- Docker maven plugin start-->
		</plugins>
	</build>

</project>
package com.example.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DockerController {

    @RequestMapping("/")
    public String index() {
        return "Hello Docker!";
    }
}
FROM openjdk:8
VOLUME /tmp
ADD spring-boot-docker-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/urandom","-jar","/app.jar"]

这个 Dockerfile 文件很简单,构建 Jdk 基础环境,添加 Spring Boot Jar 到镜像中,简单解释一下:

  • FROM:表示使用 JDK8环境为基础镜像,如果镜像不是本地的会从DockerHub进行下载。

  • VOLUME:VOLUME 指向了一个/tmp的目录,由于 Spring Boot 使用内置的Tomcat容器,Tomcat 默认使

    /tmp作为工作目录。这个命令的效果是:在宿主机的/var/lib/docker目录下创建一个临时文件并把它链

    接到容器中的/tmp目录。

  • ADD:拷贝文件并且重命名。

  • ENTRYPOINT:为了缩短 Tomcat 的启动时间,添加java.security.egd的系统属性指向/dev/urandom

    为 ENTRYPOINT。

2.2 使用Docker部署Spring Boot项目

将项目 spring-boot-docker 拷贝服务器中,进入项目路径下进行打包测试。

# 打包
$ mvn package
# 启动
$ java -jar target/spring-boot-docker-0.0.1-SNAPSHOT.jar

看到 Spring Boot 的启动日志后表明环境配置没有问题,接下来我们使用 DockerFile 构建镜像。

$ mvn package docker:build

第一次构建可能有点慢,当看到以下内容的时候表明构建成功:

[root@zsx spring-boot-docker]# mvn package docker:build
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building spring-boot-docker 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ spring-boot-docker ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ spring-boot-docker ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:testResources (default-testResources) @ spring-boot-docker ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/temp/spring-boot-docker/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ spring-boot-docker ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.20.1:test (default-test) @ spring-boot-docker ---
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ spring-boot-docker ---
[INFO] Building jar: /home/temp/spring-boot-docker/target/spring-boot-docker-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.0.0.RELEASE:repackage (default) @ spring-boot-docker ---
[INFO]
[INFO] --- docker-maven-plugin:1.0.0:build (default-cli) @ spring-boot-docker ---
[INFO] Using authentication suppliers: [ConfigFileRegistryAuthSupplier]
[INFO] Copying /home/temp/spring-boot-docker/target/spring-boot-docker-0.0.1-SNAPSHOT.jar -> /home/temp/spring-boot-docker/target/docker/spring-boot-docker-0.0.1-SNAPSHOT.jar
[INFO] Copying src/main/docker/Dockerfile -> /home/temp/spring-boot-docker/target/docker/Dockerfile
[INFO] Building image springboot/spring-boot-docker
Step 1/4 : FROM openjdk:8

 ---> b273004037cc
Step 2/4 : VOLUME /tmp

 ---> Using cache
 ---> 1ddce8ad30a4
Step 3/4 : ADD spring-boot-docker-0.0.1-SNAPSHOT.jar app.jar

 ---> 731e8e91b593
Step 4/4 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/urandom","-jar","/app.jar"]

 ---> Running in 504bf74d0084
Removing intermediate container 504bf74d0084
 ---> b3bb84d4545a
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built b3bb84d4545a
Successfully tagged springboot/spring-boot-docker:latest
[INFO] Built springboot/spring-boot-docker
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.890 s
[INFO] Finished at: 2024-01-23T11:50:16+08:00
[INFO] Final Memory: 30M/97M
[INFO] ------------------------------------------------------------------------

使用docker images命令查看构建好的镜像:

$ [root@zsx spring-boot-docker]# docker images
REPOSITORY                                       TAG       IMAGE ID       CREATED              SIZE
springboot/spring-boot-docker                    latest    b3bb84d4545a   About a minute ago   542MB

springboot/spring-boot-docker 就是我们构建好的镜像,下一步就是运行该镜像

$ [root@zsx spring-boot-docker]# docker run -d -p 8888:8080 -t springboot/spring-boot-docker
af51b2369945a77a8b96098618b22191fb2b8a1616897c97688ed8183a7da759

启动完成之后我们使用docker ps查看正在运行的镜像:

[root@zsx spring-boot-docker]# docker ps
CONTAINER ID   IMAGE                           COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
af51b2369945   springboot/spring-boot-docker   "java -Djava.securit…"   48 seconds ago   Up 46 seconds   0.0.0.0:8888->8080/tcp, :::8888->8080/tcp              youthful_margulis

可以看到我们构建的容器正在在运行,访问浏览器:http://192.168.136.195:8888/,返回

Hello Docker!

说明使用 Docker 部署 Spring Boot 项目成功!

文章来源:https://blog.csdn.net/qq_30614345/article/details/135768794
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。