前言
准备好SpringBoot项目
构建一个SpringBoot项目,写一个路由hello
,访问返回hello world, docker!
。
@Slf4j
@RestController
@SpringBootApplication
public class DockerWebApplication implements CommandLineRunner {
@Value("${spring.application.name}")
private String applicationName;
public static void main(String[] args) {
SpringApplication.run(DockerWebApplication.class, args);
}
@GetMapping(value = "/hello")
public String hello() {
log.info("[hello] hello world, docker!");
return "hello world, docker!";
}
@Override
public void run(String... args) throws Exception {
log.info("{} 启动成功...", applicationName);
}
}
项目打包
在项目根目录运行mvn clean package
将项目打包成jar
包,完成之后会在target
目录下生成一个可执行的jar
文件。
<build>
<plugins>
<!--打包-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.ronan.docker.DockerWebApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
编写Dockerfile
在项目根目录编写Dockerfile
文件
# 指定基础镜像
FROM java:8
# 指向了一个`/tmp`的目录,由于Spring Boot使用内置的Tomcat容器,Tomcat默认使用`/tmp`作为工作目录。
VOLUME /tmp
# 项目的`ronan-docker-web.jar`作为`app.jar`添加到容器
ADD ronan-docker-web.jar app.jar
# 指定与外界交互的端口9090
EXPOSE 9090
# 执行项目app.jar
ENTRYPOINT ["java","-jar","app.jar"]
上传服务器
用工具将jar
文件和Dockerfile
文件上传到服务器的/workspace
工作目录
# 制作镜像
[root@izwz9gvghuefob3ifhv3ppz docker-web]# docker build -t ronan-docker-web .
Sending build context to Docker daemon 19.52MB
Step 1/5 : FROM java:8
---> d23bdf5b1b1b
Step 2/5 : VOLUME /tmp
---> Running in 76e275045769
Removing intermediate container 76e275045769
---> 4493e9815453
Step 3/5 : ADD ronan-docker-web.jar app.jar
---> 4de7c3e65497
Step 4/5 : EXPOSE 9090
---> Running in efae4a77e05b
Removing intermediate container efae4a77e05b
---> cba2a305f4e2
Step 5/5 : ENTRYPOINT ["java","-jar","app.jar"]
---> Running in 45641296e12e
Removing intermediate container 45641296e12e
---> 1a8a3e60bd47
Successfully built 1a8a3e60bd47
Successfully tagged ronan-docker-web:latest
# 查看镜像
[root@izwz9gvghuefob3ifhv3ppz docker-web]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ronan-docker-web latest 1a8a3e60bd47 19 seconds ago 663MB
java 8 d23bdf5b1b1b 5 years ago 643MB
# 创建容器
[root@izwz9gvghuefob3ifhv3ppz docker-web]# docker run -itd --name ronan-docker-web-1 -p 9090:9090 ronan-docker-web:latest
0d0ce82668e3f01f8022e013383018db4ca54195af00de9af74385e7342a4324
# 查看容器
[root@izwz9gvghuefob3ifhv3ppz docker-web]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0d0ce82668e3 ronan-docker-web:latest "java -jar app.jar" 6 seconds ago Up 5 seconds 0.0.0.0:9090->9090/tcp ronan-docker-web-1
# 进入容器
[root@izwz9gvghuefob3ifhv3ppz docker-web]# docker exec -it 0d0ce82668e3 bash
root@0d0ce82668e3:/# ls -l
total 19124
-rw-r--r-- 1 root root 19515332 May 9 16:27 app.jar
drwxr-xr-x 1 root root 4096 Jan 17 2017 bin
drwxr-xr-x 2 root root 4096 Dec 28 2016 boot
drwxr-xr-x 5 root root 360 May 10 15:25 dev
drwxr-xr-x 1 root root 4096 May 10 15:25 etc
drwxr-xr-x 2 root root 4096 Dec 28 2016 home
drwxr-xr-x 1 root root 4096 Jan 17 2017 lib
drwxr-xr-x 2 root root 4096 Jan 16 2017 lib64
drwxr-xr-x 2 root root 4096 Jan 16 2017 media
drwxr-xr-x 2 root root 4096 Jan 16 2017 mnt
drwxr-xr-x 2 root root 4096 Jan 16 2017 opt
dr-xr-xr-x 92 root root 0 May 10 15:25 proc
drwx------ 2 root root 4096 Jan 16 2017 root
drwxr-xr-x 3 root root 4096 Jan 16 2017 run
drwxr-xr-x 2 root root 4096 Jan 16 2017 sbin
drwxr-xr-x 2 root root 4096 Jan 16 2017 srv
dr-xr-xr-x 13 root root 0 Jun 29 2021 sys
drwxrwxrwt 5 root root 4096 May 10 15:25 tmp
drwxr-xr-x 1 root root 4096 Jan 17 2017 usr
drwxr-xr-x 1 root root 4096 Jan 17 2017 var
测试
# 请求
ronan@luonandeMacBook-Pro ~ % curl http://120.77.35.233:9090/hello
hello world, docker!
总结
本文是一个Docker部署SpringBoot项目的示例,万变不离其宗。