目录
一、准备基础docker环境
springBoot项目启动需要依赖jdk/java环境,需要将jdk/java作为基础镜像在Dockerfile中进行配置;使用jdk哪个版本需要根据实际项目中的需要,这里使用java 8版本;
1、安装jdk 1.8镜像
docker pull java:8
2、查看拉取镜像
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
java 8 d23bdf5b1b1b 5 years ago 643MB
3、运行java 镜像
docker run -d -it --name java8 java:8
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b6de11e86092 java:8 "/bin/bash" 12 seconds ago Up 12 seconds java8
4、进入镜像验证jdk版本
[root@docker ~]# docker exec -it java8 /bin/bash
root@b6de11e86092:/# java -version
openjdk version "1.8.0_111"
OpenJDK Runtime Environment (build 1.8.0_111-8u111-b14-2~bpo8+1-b14)
OpenJDK 64-Bit Server VM (build 25.111-b14, mixed mode)
root@b6de11e86092:/#
二、将可执行springBoot项目打成jar包
1、主要springBoot项目类
package com.hs.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@org.springframework.stereotype.Controller
public class Controller {
@ResponseBody
@RequestMapping("test")
public String test(){
return "test dockfile-springBoot";
}
}
2、打成jar包
demo-0.0.1-SNAPSHOT.jar
三、制作docker镜像、运行容器
1、存放位置
将demo-0.0.1-SNAPSHOT.jar放在/home/springBoot-docker目录下;
在/home/springBoot-docker目录下放入Dockerfile文件内容如下:
From java:8
COPY *.jar /app/web.jar
#随意输出一句
CMD ["echo","success---build"]
#容器启动时执行什么命令
ENTRYPOINT ["java","-jar","/app/web.jar"]
注意:COPY后的第一个路径是相对于Dockerfile的路径,第二个路径是copy到docker中的路径并进行了重命名;
2、制作镜像
[root@docker springBoot-docker]# docker build -t web-test .
Sending build context to Docker daemon 17.61MB
Step 1/4 : From java:8
---> d23bdf5b1b1b
Step 2/4 : COPY *.jar /app/web.jar
---> 842a89197b40
Step 3/4 : CMD ["echo","success---build"]
---> Running in 44a3ff5d4325
Removing intermediate container 44a3ff5d4325
---> c9f19fd96561
Step 4/4 : ENTRYPOINT ["java","-jar","/app/web.jar"]
---> Running in 1b5bece20c7f
Removing intermediate container 1b5bece20c7f
---> 024d6444c366
Successfully built 024d6444c366
Successfully tagged web-test:latest
- 因为我们的dockerfile文件名为Dockerfile,所以不用 -f 指定dockerfile文件名,系统会自动识别当前目录下名为Dockerfile 的文件。
- -t:镜像名 web-test
- .:指镜像构建时打包上传到Docker引擎中的文件的目录,当构建的时候,由用户指定构建镜像的上下文路径,而 docker build 会将这个路径下所有的文件都打包上传给 Docker 引擎,引擎内将这些内容展开后,就能获取到所有指定上下文中的文件了。
注意:上下文路径下不要放无用的文件,因为会一起打包发送给 docker 引擎,如果文件过多会造成过程缓慢。
[root@docker springBoot-docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
web-test latest 024d6444c366 4 seconds ago 661MB
3、创建并运行容器
docker run -d -p 8082:8080 --name my-web web-test
4、查看容器
[root@docker springBoot-docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b02c7171dec3 web-test "java -jar /app/web.…" 9 seconds ago Up 7 seconds 0.0.0.0:8082->8080/tcp, :::8082->8080/tcp my-web
5、查看日志
[root@docker springBoot-docker]# docker logs -f -t --tail=100 b02c7171dec3
2022-06-07T05:38:01.349493267Z
2022-06-07T05:38:01.349518928Z . ____ _ __ _ _
2022-06-07T05:38:01.349521996Z /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
2022-06-07T05:38:01.349524399Z ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
2022-06-07T05:38:01.349526691Z \\/ ___)| |_)| | | | | || (_| | ) ) ) )
2022-06-07T05:38:01.349528570Z ' |____| .__|_| |_|_| |_\__, | / / / /
2022-06-07T05:38:01.349530513Z =========|_|==============|___/=/_/_/_/
2022-06-07T05:38:01.371587429Z :: Spring Boot :: (v2.7.0)
2022-06-07T05:38:01.371605701Z
2022-06-07T05:38:01.505972358Z 2022-06-07 05:38:01.485 INFO 1 --- [ main] com.hs.demo.DemoApplication : Starting DemoApplication v0.0.1-SNAPSHOT using Java 1.8.0_111 on b02c7171dec3 with PID 1 (/app/web.jar started by root in /)
2022-06-07T05:38:01.505991012Z 2022-06-07 05:38:01.497 INFO 1 --- [ main] com.hs.demo.DemoApplication : No active profile set, falling back to 1 default profile: "default"
2022-06-07T05:38:03.972765275Z 2022-06-07 05:38:03.966 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-06-07T05:38:04.013642655Z 2022-06-07 05:38:04.010 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-06-07T05:38:04.013656901Z 2022-06-07 05:38:04.010 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.63]
2022-06-07T05:38:04.225952597Z 2022-06-07 05:38:04.225 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-06-07T05:38:04.225972304Z 2022-06-07 05:38:04.225 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2549 ms
2022-06-07T05:38:05.579765297Z 2022-06-07 05:38:05.578 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-06-07T05:38:05.615985011Z 2022-06-07 05:38:05.614 INFO 1 --- [ main] com.hs.demo.DemoApplication : Started DemoApplication in 5.084 seconds (JVM running for 6.188)
2022-06-07T05:38:33.160258395Z 2022-06-07 05:38:33.149 INFO 1 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-06-07T05:38:33.160280406Z 2022-06-07 05:38:33.149 INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2022-06-07T05:38:33.160283313Z 2022-06-07 05:38:33.150 INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms