很久之前就用过,一直没有好好写篇博客,今天就总结一下
创建一个SpringBoot项目并打成jar包,结构如图
FROM java:8
VOLUME /tmp
ADD springboot-1.0-SNAPSHOT.jar boot.jar
EXPOSE 8080
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/boot.jar"]
FROM
:表示基础镜像,即运行环境
VOLUME
: /tmp创建/tmp目录并持久化到Docker数据文件夹,因为Spring Boot使用的内嵌Tomcat容器默认使用/tmp作为工作目录
ADD
:拷贝文件并且重命名(ADD springboot-1.0-SNAPSHOT.jar boot.jar 将应用jar包复制到/boot.jar)
EXPOSE
:并不是真正的发布端口,这个只是容器部署人员与建立image的人员之间的交流,即建立image的人员告诉容器布署人员容器应该映射哪个端口给外界
ENTRYPOINT
:容器启动时运行的命令,相当于我们在命令行中输入java -jar xxxx.jar,为了缩短 Tomcat 的启动时间,添加java.security.egd的系统属性指向/dev/urandom作为 ENTRYPOINT
我们将 Dockerfile 和 我们的项目 jar包 上传到 Linux 服务器上去 ,如何执行下面命令
docker build -t springboot .
docker build
: 就是 我们编译 当前目录下的 Dockerfile文件
-t
: 我的个人理解就是 在进程中运行 ,而那个 点 代码当前目录
[root@spiritmark]# docker build -t springboot .
Sending build context to Docker daemon 14.43 MB
Step 1/5 : FROM java:8
Trying to pull repository docker.io/library/java ...
8: Pulling from docker.io/library/java
5040bd298390: Pull complete
fce5728aad85: Pull complete
76610ec20bf5: Pull complete
60170fec2151: Pull complete
e98f73de8f0d: Pull complete
11f7af24ed9c: Pull complete
49e2d6393f32: Pull complete
bb9cdec9c7f3: Pull complete
Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
Status: Downloaded newer image for docker.io/java:8
---> d23bdf5b1b1b
Step 2/5 : VOLUME /tmp
---> Running in 0aec2dc2f98c
---> a52e844f25d4
Removing intermediate container 0aec2dc2f98c
Step 3/5 : ADD springboot-1.0-SNAPSHOT.jar boot .jar
---> 3ba2f4fdddda
Removing intermediate container 860a0f748a23
Step 4/5 : EXPOSE 8080
---> Running in 1d3331cc2be6
---> e9ac33d26ce0
Removing intermediate container 1d3331cc2be6
Step 5/5 : ENTRYPOINT java -Djava.security.egd=file:/dev/./urandom -jar /boot.jar
---> Running in d354f8ee2af5
---> 8937e1ade6c7
Removing intermediate container d354f8ee2af5
Successfully built 8937e1ade6c7
docker run --rm -d --name 容器名称 -p 8080:8080 镜像名称
–name
给容器取一个容易记住的名字方便日后管理。--rm
在Docker容器退出时,默认容器内部的文件系统仍然被保留,以方便调试并保留用户数据docker logs -f --tail=100 容器名称
[root@spiritmark] # docker logs -f --tail=100 04d6b2c34795
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.4.RELEASE)
2019-12-29 07:42:58.982 INFO 1 --- [ main] c.b.ElkExampleSpringBootApplication : Starting ElkExampleSpringBootApplication v1.0-SNAPSHOT on 04d6b2c34795 with PID 1 (/elk.jar started by root in /)
2019-12-29 07:42:58.999 INFO 1 --- [ main] c.b.ElkExampleSpringBootApplication : No active profile set, falling back to default profiles: default
2019-12-29 07:42:59.243 INFO 1 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5a2e4553: startup date [Sun Dec 29 07:42:59 UTC 2019]; root of context hierarchy
2019-12-29 07:43:03.652 INFO 1 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2019-12-29 07:43:03.699 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-12-29 07:43:03.714 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.15
2019-12-29 07:43:04.012 INFO 1 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-12-29 07:43:04.012 INFO 1 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 4772 ms
2019-12-29 07:43:04.449 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2019-12-29 07:43:04.470 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2019-12-29 07:43:04.470 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2019-12-29 07:43:04.471 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2019-12-29 07:43:04.471 INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2019-12-29 07:43:05.534 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5a2e4553: startup date [Sun Dec 29 07:42:59 UTC 2019]; root of context hierarchy
2019-12-29 07:43:05.765 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/exception]}" onto public java.lang.String com.bruceliu.controller.ELKController.exception()
2019-12-29 07:43:05.766 INFO 1 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/elkdemo]}" onto public java.lang.String com.bruceliu.controller.ELKController.helloWorld()
2019-12-29 07:43:05.772 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)
2019-12-29 07:43:05.780 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)
2019-12-29 07:43:05.869 INFO 1 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-12-29 07:43:05.869 INFO 1 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-12-29 07:43:05.984 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]
2019-12-29 07:43:06.387 INFO 1 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2019-12-29 07:43:06.537 INFO 1 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2019-12-29 07:43:06.562 INFO 1 --- [ main] c.b.ElkExampleSpringBootApplication : Started ElkExampleSpringBootApplication in 8.771 seconds (JVM running for 9.832)
这样我们项目 已经部署在 阿里云服务器上了
使用 idea去连接docker ,就必须开放端口,默认 docker是不支持 idea远程 连接 docker的,所以我们要开放,
首先编辑我们服务器上的docker文件,进入修改docker的地方
#修改该Docker服务文件
vi /usr/lib/systemd/system/docker.service
修改以ExecStart开头的行(centos 7):添加
#修改ExecStart这行
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
将文件内的 ExecStart注释。 新增如上行。
#ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375-H unix:///var/run/docker.sock
修改后保存文件,然后重启docker
systemctl daemon-reload
service docker restart
重启之后测试远程连接是否正常,这里的2375是之前配置的端口
curl http://服务ip:2375/version
然后开启端口,或者关闭防火墙,二者选其一即可
firewall-cmd --zone=public --add-port=2375/tcp --permanent
chkconfig iptables off
然后打开浏览器测试将之前的localhost修改为你的ip
首先下载docker插件,idea2019自带了docker插件。如果没有插件可以选择安装docker插件
打开Idea,从File->Settings->Plugins->Install JetBrains plugin进入插件安装界面,
在搜索框中输入docker,可以看到Docker integration,点击右边的Install按钮进行安装。
安装后重启Idea。
配置docker,连接到远程docker服务。
从File->Settings->Build,Execution,Deployment->Docker打开配置界面
配置完成链接之后,出现了框中的内容即可.
配置阿里云镜像加速器:
传统过程中,打包、部署、等。
而在持续集成过程中,项目工程一般使用 Maven 编译打包,然后生成镜像,通过镜像上线,能够大大提供上线效
率,同时能够快速动态扩容,快速回滚,着实很方便。docker-maven-plugin 插件就是为了帮助我们在Maven工程
中,通过简单的配置,自动生成镜像并推送到仓库中。
<build>
<finalName>${project.artifactId}finalName>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<version>1.5.3.RELEASEversion>
plugin>
<plugin>
<groupId>com.spotifygroupId>
<artifactId>docker-maven-pluginartifactId>
<version>1.0.0version>
<configuration>
<imageName>${docker.image.prefix}/templateimageName>
<imageTags>
<imageTag>latestimageTag>
imageTags>
<baseImage>javabaseImage>
<maintainer>[email protected]maintainer>
<workdir>/ROOTworkdir>
<cmd>["java", "-version"]cmd>
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]entryPoint>
<dockerHost>http://39.97.241.130:2375dockerHost>
<resources>
<resource>
<targetPath>/ROOTtargetPath>
<directory>${project.build.directory}directory>
<include>${project.build.finalName}.jarinclude>
resource>
resources>
configuration>
<executions>
<execution>
<id>build-imageid>
<phase>packagephase>
<goals>
<goal>buildgoal>
goals>
execution>
executions>
plugin>
plugins>
build>
上面的配置 就和 下面的 效果一样 ,只不过变成了自动化
FROM java:8
VOLUME /tmp
ADD springboot-1.0-SNAPSHOT.jar boot.jar
EXPOSE 8080
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/boot.jar"]
mvn clean package docker:build