【转】SpringBoot使用Docker来部署

<div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-1a85854398.css">
                <div id="content_views" class="markdown_views prism-atom-one-dark">
                    <svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
                        <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
                    </svg>
                    <h3><a name="t0"></a><a id="SpringBootDocker_0"></a>SpringBoot使用Docker来部署</h3> 
<blockquote> 
 <p><a href="https://so.csdn.net/so/search?from=pc_blog_highlight&amp;q=linux" target="_blank" class="hl hl-1">linux</a>:center 7<br> docker version: 19.03.1<br> docker client version: 1.40<br> idea:2018</p> 
</blockquote> 
<h3><a name="t1"></a><a id="_7"></a>安装步骤</h3> 
<ol><li><a href="https://blog.csdn.net/a656678879/article/details/89636402#docker">安装步骤</a></li></ol> 
<pre class="prettyprint"><code class="has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;">https://blog.csdn.net/a656678879/article/details/89636402#docker
<div class="hljs-button {2}" data-title="复制(为作者贡献原力分)" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.4259&quot;}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li></ul></pre> 
<ol start="2"><li>配置docker远程连接端口</li></ol> 
<pre class="prettyprint"><code class="has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"> vi /usr/lib/systemd/system/docker.service
找到 ExecStart,在最后面添加 -H tcp://0.0.0.0:2375,如下图所示

由安全组的开放安全组的端口开放
<div class="hljs-button {2}" data-title="复制(为作者贡献原力分)" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.4259&quot;}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li></ul></pre> 
<ol start="3"><li>重启docker</li></ol> 
<pre class="prettyprint"><code class="has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;">systemctl daemon-reload
systemctl start docker
<div class="hljs-button {2}" data-title="复制(为作者贡献原力分)" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.4259&quot;}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li></ul></pre> 
<ol start="4"><li>Idea安装插件,重启<br> <img src="https://img-blog.csdnimg.cn/20190827174714990.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2E2NTY2Nzg4Nzk=,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></li><li>配置docker的端口,进行连接(前面已经2375开放端口)<br> <img src="https://img-blog.csdnimg.cn/20190827175107838.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2E2NTY2Nzg4Nzk=,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></li></ol> 
<h3><a name="t2"></a><a id="SpringClouddocker_32"></a>SpringCloud使用docker的发布</h3> 
<ol><li>项目基本结构<br> <img src="https://img-blog.csdnimg.cn/20190827175441869.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2E2NTY2Nzg4Nzk=,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></li><li>pom文件增加相应的插件以及复制等操作</li></ol> 
<pre class="prettyprint"><code class="has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;">&lt;build&gt;
    &lt;plugins&gt;
      &lt;!--SpringCloud的打成可执行的jar包插件--&gt;
      &lt;plugin&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
        &lt;executions&gt;
          &lt;execution&gt;
            &lt;goals&gt;
              &lt;goal&gt;repackage&lt;/goal&gt;
            &lt;/goals&gt;
          &lt;/execution&gt;
        &lt;/executions&gt;
      &lt;/plugin&gt;
      &lt;!--docker的操作--&gt;
      &lt;plugin&gt;
        &lt;groupId&gt;com.spotify&lt;/groupId&gt;
        &lt;artifactId&gt;docker-maven-plugin&lt;/artifactId&gt;
        &lt;version&gt;1.0.0&lt;/version&gt;
        &lt;configuration&gt;
          &lt;dockerDirectory&gt;src/main/docker&lt;/dockerDirectory&gt;
          &lt;resources&gt;
            &lt;resource&gt;
              &lt;targetPath&gt;/&lt;/targetPath&gt;
              &lt;directory&gt;${project.build.directory}&lt;/directory&gt;
              &lt;include&gt;${project.build.finalName}.jar&lt;/include&gt;
            &lt;/resource&gt;
          &lt;/resources&gt;
        &lt;/configuration&gt;
      &lt;/plugin&gt;
      &lt;!--docker需要的jar的复制操作--&gt;
      &lt;plugin&gt;
        &lt;artifactId&gt;maven-antrun-plugin&lt;/artifactId&gt;
        &lt;executions&gt;
          &lt;execution&gt;
            &lt;phase&gt;package&lt;/phase&gt;
            &lt;configuration&gt;
              &lt;tasks&gt;
                &lt;copy todir = "src/main/docker"
                  file = "target/${project.artifactId}-${project.version}.${project.packaging}"&gt;&lt;/copy&gt;
              &lt;/tasks&gt;
            &lt;/configuration&gt;
            &lt;goals&gt;
              &lt;goal&gt;run&lt;/goal&gt;
            &lt;/goals&gt;
          &lt;/execution&gt;
        &lt;/executions&gt;
      &lt;/plugin&gt;
    &lt;/plugins&gt;
  &lt;/build&gt;
<div class="hljs-button {2}" data-title="复制(为作者贡献原力分)" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.4259&quot;}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li><li style="color: rgb(153, 153, 153);">21</li><li style="color: rgb(153, 153, 153);">22</li><li style="color: rgb(153, 153, 153);">23</li><li style="color: rgb(153, 153, 153);">24</li><li style="color: rgb(153, 153, 153);">25</li><li style="color: rgb(153, 153, 153);">26</li><li style="color: rgb(153, 153, 153);">27</li><li style="color: rgb(153, 153, 153);">28</li><li style="color: rgb(153, 153, 153);">29</li><li style="color: rgb(153, 153, 153);">30</li><li style="color: rgb(153, 153, 153);">31</li><li style="color: rgb(153, 153, 153);">32</li><li style="color: rgb(153, 153, 153);">33</li><li style="color: rgb(153, 153, 153);">34</li><li style="color: rgb(153, 153, 153);">35</li><li style="color: rgb(153, 153, 153);">36</li><li style="color: rgb(153, 153, 153);">37</li><li style="color: rgb(153, 153, 153);">38</li><li style="color: rgb(153, 153, 153);">39</li><li style="color: rgb(153, 153, 153);">40</li><li style="color: rgb(153, 153, 153);">41</li><li style="color: rgb(153, 153, 153);">42</li><li style="color: rgb(153, 153, 153);">43</li><li style="color: rgb(153, 153, 153);">44</li><li style="color: rgb(153, 153, 153);">45</li><li style="color: rgb(153, 153, 153);">46</li><li style="color: rgb(153, 153, 153);">47</li><li style="color: rgb(153, 153, 153);">48</li><li style="color: rgb(153, 153, 153);">49</li><li style="color: rgb(153, 153, 153);">50</li></ul></pre> 
<ol start="3"><li>在src/main目录下创建docker目录,并创建Dockerfile文件,见上图</li></ol> 
<pre class="prettyprint"><code class="has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;">#基础镜像,如果本地没有,会从远程仓库拉取。
FROM java:8

#镜像的制作人
#MAINTAINER kunzai/qq@qq.com

#工作目录
#WORKDIR /opt/docker/penguin-log-web/

##这里的 /tmp 目录就会在运行时自动挂载为匿名卷,任何向 /tmp 中写入的信息都不会记录进容器存储层,可以多个
#VOLUME ["/tmp"]

#声明了容器应该打开的端口并没有实际上将它打开
#EXPOSE 8080

#定义参数
#ARG JAR_FILE

# 将jar包添加到容器中并更名为app.jar
ADD *.jar app.jar

#拷贝本地文件到镜像中#复制上下文目录下的target/demo-1.0.0.jar 到容器里
#COPY ${JAR_FILE} penguin-log-web.jar

#指定容器启动时要执行的命令,但如果存在CMD指令,CMD中的参数会被附加到ENTRYPOINT指令的后面
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
<div class="hljs-button {2}" data-title="复制(为作者贡献原力分)" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.4259&quot;}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li><li style="color: rgb(153, 153, 153);">21</li><li style="color: rgb(153, 153, 153);">22</li><li style="color: rgb(153, 153, 153);">23</li><li style="color: rgb(153, 153, 153);">24</li><li style="color: rgb(153, 153, 153);">25</li><li style="color: rgb(153, 153, 153);">26</li></ul></pre> 
<ol start="4"><li>在配置文件[application.properties]增加日志的配置</li></ol> 
<pre class="prettyprint"><code class="has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;">logging.path=/var/log/docker/penguin-applet/penguin-alert-web
<div class="hljs-button {2}" data-title="复制(为作者贡献原力分)" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.4259&quot;}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li></ul></pre> 
<ol start="5"><li>配置镜像文件的配置<br> <img src="https://img-blog.csdnimg.cn/20190827180810144.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2E2NTY2Nzg4Nzk=,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></li><li>创建镜像,创建容器</li></ol> 
<pre class="prettyprint"><code class="has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;">- 执行命令:mvn clean install -DskipTests 也就会形成步骤1的jar包的复制操作
<div class="hljs-button {2}" data-title="复制(为作者贡献原力分)" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.4259&quot;}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li></ul></pre> 
<ol start="7"><li>镜像的基本操作<br> <img src="https://img-blog.csdnimg.cn/20190827181417247.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2E2NTY2Nzg4Nzk=,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></li><li>访问项目即可</li></ol>
                </div><div><div></div></div>
                <link href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/markdown_views-d7a94ec6ab.css" rel="stylesheet">
                <link href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/style-49037e4d27.css" rel="stylesheet">
        </div>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值