构建镜像
首先创建dockerfile 文件夹
然后创建dockerfile文件
[root@localhost Dockerfile]# mkdir Dockerfile2
[root@localhost Dockerfile]# cd Dockerfile2
[root@localhost Dockerfile2]# vi Dockerfile
# 指定基础镜像,本地没有会从dockerHub pull下来
FROM java:8
# 把可执行jar包复制到基础镜像的根目录下
ADD demo.jar /demo.jar
# 镜像要暴露的端口,如要使用端口,在执行docker run命令时使用-p生效
EXPOSE 8081
# 在镜像运行为容器后执行的命令
ENTRYPOINT ["java","-jar","/demo.jar"]
# 构建镜像
[root@localhost Dockerfile2]# docker build -t demo .
运行自己的
SpringBoot
镜像
[root@localhost Dockerfile2] docker run --name demo -d -p 8081:8081 demo
执行命令后可以看到我们熟悉的springboot开机启动界面了
[root@localhost Dockerfile2]# docker logs demo
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.5.RELEASE)
2020-03-25 03:38:57.321 INFO 1 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication v0.0.1-SNAPSHOT on 75e52477616b with PID 1 (/demo.jar started by root in /)
2020-03-25 03:38:57.330 INFO 1 --- [ main] com.example.demo.DemoApplication : The following profiles are active: dev
2020-03-25 03:38:59.872 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2020-03-25 03:38:59.878 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2020-03-25 03:38:59.978 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 35ms. Found 0 Redis repository interfaces.
2020-03-25 03:39:02.664 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8081 (http)
2020-03-25 03:39:02.738 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-03-25 03:39:02.739 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.31]
2020-03-25 03:39:03.047 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-03-25 03:39:03.047 INFO 1 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 5495 ms
在网页是执行demo接口效果如下: