步骤:
项目准备
1.下载一个干净的springboot项目,没有多余的依赖
https://github.com/wangzixi-diablo/mySpringBoot
2.进入项目文件夹,运行mvn spring-boot:run查看运行结果,端口可在application.properties中修改
2021-04-27 11:54:55.346 INFO 128114 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8000 (http)
浏览器访问http://localhost:8000/product,看到显示页
3.将项目打成jar包
mvn clean package
因为项目的pom.xml中的finalName标签指定了打包后的包名,一般情况下(不指定)时为mySringBoot-0.0.1-SNAPSHOT.jar,其中0.0.1为项目pom.xml标签中指定的。
项目需要有server容器,如
org.springframework.boot spring-boot-starter-web其中的依赖中包含tomat server容器
org.springframework.boot spring-boot-starter-tomcat使用Dockerfile docker run单个部署运行docker
1.编写Dockerfile文件,docker镜像就是基于Dockerfile文件进行制作
# 指定了我们这个镜像基于openjdk这个镜像制作
FROM openjdk:8-jdk-alpine
# 定义了一个持久化存储,指向容器中的tmp文件夹。SpringBoot应用为内置的Tomcat服务器实例创建的默认工作目录为tmp,通过该命令,可以在运行Docker的[宿主机]目录/var/lib/docker创建一个临时的目录,挂接到容器内部的tmp
VOLUME /tmp
# 声明运行时容器提供服务端口,这只是一个声明,在运行时并不会因为这个声明应用就会开启这个端口的服务。命令行的expose可以起作用
EXPOSE 8080
# 把本地目录下target文件夹里打好的jar文件添加到容器里,重命名为helloapp.jar.
ADD target/prolikeService.jar helloapp.jar
# ENV命令的作用是设置环境变量。在复杂的使用场景中,我们可能需要使用各种参数启动JVM,这些参数通过ENV命令设置的环境变量传入Java命令。
ENV JAVA_OPTS="-Dserver.port=8080"
# 容器镜像运行的起始点
ENTRYPOINT exec java $JAVA_OPTS -jar /app.jar
2.生成镜像
docker build -t helloapp:v0 .
上面命令行最后的".“并不是表示结束的标点符号,而是Linux系统里的”." ,代表当前目录。
查看镜像
root@ubuntu:/home/ubuntu/workspace/mySpringBoot# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
helloapp v0 713ca3edde75 29 seconds ago 136MB
3.运行镜像
docker run 容器名 -d 主机端口:容器内部的端口
-d 后台运行 ,主机端口:容器内部的端口 一定需要
docker run helloapp:v0 -d -p 8080:8080
4.测试
root@ubuntu:/home/ubuntu/workspace/openvidu/openvidu-server/deployments/ce/docker-compose# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f4469950da09 helloapp:v0 "/bin/sh -c 'exec ja…" About a minute ago Up About a minute 8080/tcp awesome_brown
root@ubuntu:/home/ubuntu/workspace/openvidu/openvidu-server/deployments/ce/docker-compose# docker exec -it f4469950da09 /bin/sh
/ #
成功进入容器中
使用docker-compose批量(一次多个镜像)部署运行docker镜像
1.查看docker版本
root@ubuntu:/home/ubuntu/workspace/openvidu/openvidu-server/deployments/ce/docker-compose# docker --version
Docker version 20.10.6, build 370c289
我的docker版本为20.10.6,使用docker-compose3.8的规范
下表显示了哪些Compose文件版本支持特定的Docker版本。
Compose 文件规范 | Docker Engine版本 |
---|---|
Compose规格 | 19.03.0+ |
3.8 | 19.03.0+ |
3.7 | 18.06.0+ |
3.6 | 18.02.0+ |
3.5 | 17.12.0+ |
3.4 | 17.09.0+ |
3.3 | 17.06.0+ |
3.2 | 17.04.0+ |
3.1 | 1.13.1+ |
3.0 | 1.13.0+ |
2.4 | 17.12.0+ |
2.3 | 17.06.0+ |
2.2 | 1.13.0+ |
2.1 | 1.12.0+ |
2.0 | 1.10.0+ |
1.0 | 1.9.1。+ |
https://docs.docker.com/compose/compose-file/#compose-and-docker-compatibility-matrix
2.编写docker-compose.yml文件
version: "3.8"
services:
hahademo:
build: .
image: hahademo:v0
ports:
- "8080:8080"
-
version:指定 docker-compose.yml 文件的写法格式
-
services:多个容器集合
-
build:配置构建时,Compose 会利用它自动构建镜像,该值可以是一个路径,也可以是一个对象,用于指定 Dockerfile 参数
-
image:指定服务所使用的镜像
-
environment:环境变量配置,可以用数组或字典两种方式
-
env_file:从文件中获取环境变量,可以指定一个文件路径或路径列表,其优先级低于 environment 指定的环境变量
-
ports:对外暴露的端口定义,和 expose 对应 - “宿主机端口:容器暴露端口”
-
context:包含Dockerfile文件的目录路径,或者是git仓库的URL。
当提供的值是相对路径时,它被解释为相对于当前compose文件的位置。 该目录也是发送到Docker守护程序构建镜像的上下文。 -
例如:
version: "3.3" services: appdemo: build: dockerfile: Dockerfile-java context: . image: myapp:v1 ports: - "8080:8080" depends_on: - db - redis redis: image: redis db: image: postgres
3.构建新镜像
docker-compose的工作流程就是:当你改变本地代码之后,先执行docker-compose build构建新的镜像,然后执行docker-compose up -d取代运行中的容器
root@ubuntu:/home/ubuntu/workspace/mySpringBoot# docker-compose build
Building hahademo
Sending build context to Docker daemon 31.24MB
Step 1/6 : FROM openjdk:8-jdk-alpine
---> a3562aa0b991
Step 2/6 : VOLUME /tmp
---> Using cache
---> d91d8d4e471c
Step 3/6 : EXPOSE 8080
---> Using cache
---> 81f16a49e077
Step 4/6 : ADD target/prolikeService.jar helloapp.jar
---> Using cache
---> 211c53bc141b
Step 5/6 : ENV JAVA_OPTS="-Dserver.port=8080"
---> Using cache
---> dd54a526601c
Step 6/6 : ENTRYPOINT exec java $JAVA_OPTS -jar /helloapp.jar
---> Using cache
---> 104a17e73ca9
Successfully built 104a17e73ca9
Successfully tagged hahademo:v0
4.运行新镜像
root@ubuntu:/home/ubuntu/workspace/mySpringBoot# docker-compose up
Creating network "myspringboot_default" with the default driver
Creating myspringboot_hahademo_1 ... done
Attaching to myspringboot_hahademo_1
hahademo_1 | Jerry Arguments:[]
hahademo_1 |
hahademo_1 | . ____ _ __ _ _
hahademo_1 | /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
hahademo_1 | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
hahademo_1 | \\/ ___)| |_)| | | | | || (_| | ) ) ) )
hahademo_1 | ' |____| .__|_| |_|_| |_\__, | / / / /
hahademo_1 | =========|_|==============|___/=/_/_/_/
hahademo_1 | :: Spring Boot :: (v1.5.9.RELEASE)
hahademo_1 |
hahademo_1 | 2021-04-27 10:37:05.840 INFO 1 --- [ main] c.s.prolikeService.PromoLikeApplication : Starting PromoLikeApplication v0.0.1-SNAPSHOT on d1516e2cf9a9 with PID 1 (/helloapp.jar started by root in /)
hahademo_1 | 2021-04-27 10:37:05.853 INFO 1 --- [ main] c.s.prolikeService.PromoLikeApplication : No active profile set, falling back to default profiles: default
hahademo_1 | 2021-04-27 10:37:06.014 INFO 1 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6e5e91e4: startup date [Tue Apr 27 10:37:05 GMT 2021]; root of context hierarchy
hahademo_1 | 2021-04-27 10:37:08.226 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
hahademo_1 | 2021-04-27 10:37:09.796 INFO 1 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
hahademo_1 | 2021-04-27 10:37:09.826 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
hahademo_1 | 2021-04-27 10:37:09.840 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.23
hahademo_1 | 2021-04-27 10:37:10.066 INFO 1 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
hahademo_1 | 2021-04-27 10:37:10.067 INFO 1 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 4071 ms
hahademo_1 | 2021-04-27 10:37:10.379 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
hahademo_1 | 2021-04-27 10:37:10.388 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
hahademo_1 | 2021-04-27 10:37:10.391 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
hahademo_1 | 2021-04-27 10:37:10.392 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
hahademo_1 | 2021-04-27 10:37:10.392 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
hahademo_1 | 2021-04-27 10:37:11.375 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/product],methods=[GET]}" onto public java.lang.String com.sap.prolikeService.api.CommerceController.getProductDetail()
hahademo_1 | 2021-04-27 10:37:11.383 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/swagger-resources/configuration/ui]}" onto public org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.UiConfiguration> springfox.documentation.swagger.web.ApiResourceController.uiConfiguration()
hahademo_1 | 2021-04-27 10:37:11.385 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/swagger-resources]}" onto public org.springframework.http.ResponseEntity<java.util.List<springfox.documentation.swagger.web.SwaggerResource>> springfox.documentation.swagger.web.ApiResourceController.swaggerResources()
hahademo_1 | 2021-04-27 10:37:11.386 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/swagger-resources/configuration/security]}" onto public org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.SecurityConfiguration> springfox.documentation.swagger.web.ApiResourceController.securityConfiguration()
hahademo_1 | 2021-04-27 10:37:11.391 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
hahademo_1 | 2021-04-27 10:37:11.392 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
hahademo_1 | 2021-04-27 10:37:11.671 INFO 1 --- [ main] pertySourcedRequestMappingHandlerMapping : Mapped URL path [/v2/api-docs] onto method [public org.springframework.http.ResponseEntity<springfox.documentation.spring.web.json.Json> springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest)]
hahademo_1 | 2021-04-27 10:37:13.987 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6e5e91e4: startup date [Tue Apr 27 10:37:05 GMT 2021]; root of context hierarchy
hahademo_1 | 2021-04-27 10:37:14.264 INFO 1 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
hahademo_1 | 2021-04-27 10:37:14.266 INFO 1 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
hahademo_1 | 2021-04-27 10:37:14.790 INFO 1 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
hahademo_1 | 2021-04-27 10:37:15.693 INFO 1 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
hahademo_1 | 2021-04-27 10:37:15.710 INFO 1 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 2147483647
hahademo_1 | 2021-04-27 10:37:15.711 INFO 1 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Context refreshed
hahademo_1 | 2021-04-27 10:37:15.773 INFO 1 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Found 1 custom documentation plugin(s)
hahademo_1 | 2021-04-27 10:37:15.850 INFO 1 --- [ main] s.d.s.w.s.ApiListingReferenceScanner : Scanning for api listing references
hahademo_1 | 2021-04-27 10:37:16.501 INFO 1 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
hahademo_1 | 2021-04-27 10:37:16.512 INFO 1 --- [ main] c.s.prolikeService.PromoLikeApplication : Started PromoLikeApplication in 11.391 seconds (JVM running for 12.36)
5.测试
root@ubuntu:/home/ubuntu/workspace/openvidu/openvidu-server/deployments/ce/docker-compose# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d1516e2cf9a9 hahademo:v0 "/bin/sh -c 'exec ja…" 52 seconds ago Up 48 seconds 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp myspringboot_hahademo_1
root@ubuntu:/home/ubuntu/workspace/openvidu/openvidu-server/deployments/ce/docker-compose# docker exec -it d1516e2cf9a9 bash
OCI runtime exec failed: exec failed: container_linux.go:367: starting container process caused: exec: "bash": executable file not found in $PATH: unknown
root@ubuntu:/home/ubuntu/workspace/openvidu/openvidu-server/deployments/ce/docker-compose# docker exec -it d1516e2cf9a9 /bin/sh
/ # ls
bin etc home log mnt proc run srv tmp var
dev helloapp.jar lib media opt root sbin sys usr
/ # cd /tmp
/tmp # ls
hsperfdata_root tomcat-docbase.8685759694091729018.8080 tomcat.7252619699602794711.8080
/tmp #
完整的例子包含:Dockerfile和docker-compose.yml可下载:https://download.csdn.net/download/level_Tiller/18275295