春风唱 發表於 2021-8-2 13:48:00

windows环境30分钟从0开始快速搭建第一个docker项目(带数据库交互)

<h1><span style="font-size: 16px">前言</span></h1>
<p><span style="font-size: 15px">小白直接上手&nbsp;docker&nbsp; 构建我们的第一个项目,简单粗暴,后续各种概念边写边了解,各种概念性的内容就不展开,没了解过的点击&nbsp;</span>Docker 教程&nbsp;进行初步了解。</p>
<blockquote>
<p><span style="font-size: 15px">Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的&nbsp;Linux或Windows&nbsp;机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。</span></p>
</blockquote>
<p>&nbsp;</p>
<hr>
<h1><span style="font-size: 16px">1、安装docker</span></h1>
<p><span style="font-size: 15px">由于我们是windows环境,所以安装的是&nbsp;</span><span style="background-color: rgba(221, 221, 221, 1); color: rgba(255, 0, 0, 1)"><strong>Docker Desktop for Windows</strong>&nbsp;</span></p>
<p>&nbsp;<strong>系统要求:</strong></p>
<ul>
<li><strong><span style="color: rgba(255, 0, 0, 1)">Windows 10 64位</span></strong>:专业版,企业版或教育版(Build 15063或更高版本)。</li>
<li><span style="color: rgba(255, 0, 0, 1)"><strong>在BIOS中启用虚拟化</strong></span>(各个主板的BIOS的操作面板不同,可咨询主板商)。通常,默认情况下启用虚拟化。</li>
<li>具有CPU SLAT功能。</li>
<li>至少4GB的运行内存。</li>
<li><strong><span style="color: rgba(255, 0, 0, 1)">启用Hyper-V</span></strong></li>
</ul>
<p><span style="font-size: 15px">这里需要重点注意,<strong>Windows 10 64位、<strong>在BIOS中启用虚拟化、启<strong>用Hyper-V这三个条件比较重要。</strong></strong></strong></span></p>
<p><span style="font-size: 15px">可以点击下面的连接进行详细安装指导</span></p>
<p><span style="font-size: 15px">详细安装步骤:Docker Desktop for Windows 安装要求已经安装步骤:https://www.jc2182.com/docker/docker-windows-install.html</span></p>
<p><span style="font-size: 15px">PS:不同的主板可以百度查询一下如:HP工作站如何在BIOS下开启关闭虚拟化技术:https://jingyan.baidu.com/article/ce436649184b393773afd309.html</span></p>
<p>&nbsp;</p>
<hr>
<h1><span style="font-size: 16px">2、创建docker项目</span></h1>
<h2><span style="font-size: 15px">1.创建项目</span></h2>
<p><span style="font-size: 15px">安装完docker后,我们创建第一个docker项目。</span></p>
<p><span style="font-size: 15px">新建一个<span style="color: rgba(255, 0, 0, 1); background-color: rgba(221, 221, 221, 1)">ASP.NET Core Web(模型-视图-控制器)</span>项目,命名为<span style="color: rgba(255, 0, 0, 1); background-color: rgba(221, 221, 221, 1)"><strong>&nbsp;AspNetCoreWeb&nbsp;</strong></span>(你也可以自己取一个名称),</span></p>
<p><span style="font-size: 15px">选择<span style="color: rgba(255, 0, 0, 1); background-color: rgba(221, 221, 221, 1)">&nbsp;.NET Core 3.1(长期支持)</span>&nbsp; ,勾选 <span style="background-color: rgba(221, 221, 221, 1); color: rgba(255, 0, 0, 1)"><strong>启用Docker&nbsp;&nbsp;</strong></span>,Docker OS 下拉选择 <span style="background-color: rgba(221, 221, 221, 1); color: rgba(255, 0, 0, 1)"><strong>Linux</strong></span>&nbsp;,如下展示:<br></span></p>
<p><span style="font-size: 15px"><img src="https://img2020.cnblogs.com/blog/1093832/202107/1093832-20210730154948888-809490276.png" alt="" loading="lazy"></span></p>
<p><span style="font-size: 15px">到这里默认代码就创建完成了,不需要的可以跳过,需要查看数据库交互的可以继续往下看。</span></p>
<p><span style="font-size: 15px">我们之前介绍过:10分钟系列:NetCore3.1+EFCore三步快速完成数据库交互。现在就基于这个进行数据库交互,下面介绍的就是这里面的做法。</span></p>
<p>&nbsp;</p>
<h2><strong><span style="font-size: 15px">2.引入NuGet包并创建上下文对象</span></strong></h2>
<p><span style="font-size: 15px">这里只需要两个包,一个是EFCore的引用包,一个是数据库连接的引用包。</span></p>
<p><span style="font-size: 15px">在NuGet分别引入下面两个包,</span></p>
<blockquote>
<p>Microsoft.EntityFrameworkCore</p>
<p>Microsoft.EntityFrameworkCore.SqlServer</p>






</blockquote>
<p>&nbsp;</p>
<p><span style="font-size: 15px">在项目里面创建一个<span style="color: rgba(255, 0, 0, 1); background-color: rgba(221, 221, 221, 1)">&nbsp;EntityDbContext</span>&nbsp;文件夹,然后在文件夹里面创建一个&nbsp;<span style="color: rgba(255, 0, 0, 1); background-color: rgba(221, 221, 221, 1)">DbContext_first&nbsp;</span>类,并继承 EFCore框架中的 <span style="color: rgba(255, 0, 0, 1); background-color: rgba(221, 221, 221, 1)">DbContext</span>,</span></p>
<p><span style="font-size: 15px">在&nbsp;<span style="color: rgba(255, 0, 0, 1); background-color: rgba(221, 221, 221, 1)">&nbsp;EntityDbContext&nbsp;</span>文件夹下创建&nbsp;<span style="background-color: rgba(221, 221, 221, 1); color: rgba(255, 0, 0, 1)">Entity</span>&nbsp;文件夹,创建&nbsp;<span style="color: rgba(255, 0, 0, 1); background-color: rgba(221, 221, 221, 1)">StudentTable</span>&nbsp;实体映射。如下展示</span></p>
<p><span style="font-size: 15px">PS:(注意,这里默认是数据库存在StudentTable表的,如果没有请先创建,EFCore支持实体映射表到数据库的,这里就不体现了,有需要了解的自行百度或私信小编)</span></p>
<p><span style="font-size: 15px">上下文里面的内容如下(注意:在Docker里面连接的数据库需要是外网可以访问的):</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> AspNetCoreWeb.EntityDbContext.Entity;
</span><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> Microsoft.EntityFrameworkCore;
</span><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> System;
</span><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> System.Collections.Generic;
</span><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> System.Linq;
</span><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> System.Threading.Tasks;

</span><span style="color: rgba(0, 0, 255, 1)">namespace</span><span style="color: rgba(0, 0, 0, 1)"> AspNetCoreWeb.EntityDbContext
{
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> DbContext_first:DbContext
    {
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 在这里重写OnConfiguring的方法来配置数据库的连接字符串
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="optionsBuilder"&gt;&lt;/param&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">override</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> OnConfiguring(DbContextOptionsBuilder optionsBuilder)
      {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">SQL Server/Azure SQL 数据库、SQLite、Azure Cosmos DB、MySQL、PostgreSQL数据库连接
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">注意,这个链接是外网可以访问的,本地链接不可以,Docker里面读取不到</span>
            optionsBuilder.UseSqlServer(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Data Source=47.101.72.203; Initial Catalog=Demo;User Id=sa;Password=Ai562723XueEr@</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
      }

      </span><span style="color: rgba(0, 0, 255, 1)">public</span> DbSet&lt;StudentTable&gt; StudentTable { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span>; }<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">需要操作的数据库对应的表</span>
<span style="color: rgba(0, 0, 0, 1)">    }
}</span></pre>
</div>
<p>&nbsp;</p>
<h2><span style="font-size: 15px">3.编写一个查询语句并返回数据</span></h2>
<p><span style="font-size: 15px">在自动生成的Home控制器里面添加如下查询代码</span></p>
<div class="cnblogs_code">
<pre>       <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">查询</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> JsonResult GetStudentList()
      {
            List</span>&lt;StudentTable&gt; studeltList = <span style="color: rgba(0, 0, 255, 1)">new</span> List&lt;StudentTable&gt;<span style="color: rgba(0, 0, 0, 1)">();
            ResultInfo result </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ResultInfo();
            </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">
            {
                </span><span style="color: rgba(0, 0, 255, 1)">using</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> ctx = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> DbContext_first())
                {
                  </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">
                  {
                        studeltList </span>=<span style="color: rgba(0, 0, 0, 1)"> ctx.StudentTable.ToList();
                  }
                  </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception ex)
                  {
                        result.code </span>= -<span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">;
                        result.message </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">数据库查询失败:</span><span style="color: rgba(128, 0, 0, 1)">"</span>+<span style="color: rgba(0, 0, 0, 1)">ex.Message;
                        </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> Json(result);
                  }
                  result.code </span>= <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;
                  result.message </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">成功</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
                  result.info </span>=<span style="color: rgba(0, 0, 0, 1)"> studeltList;
                }
            }
            </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception ex)
            {
                result.code </span>= -<span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">;
                result.message </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">数据库连接失败:</span><span style="color: rgba(128, 0, 0, 1)">"</span> +<span style="color: rgba(0, 0, 0, 1)"> ex.Message;
            }
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> Json(result);
      }

      </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ResultInfo
      {
            </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span> code { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span><span style="color: rgba(0, 0, 0, 1)">; }
            </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">string</span> message { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span><span style="color: rgba(0, 0, 0, 1)">; }
            </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">object</span> info { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span><span style="color: rgba(0, 0, 0, 1)">; }
      }</span></pre>
</div>
<p>&nbsp;</p>
<h2><span style="font-size: 15px">4.配置相关数据</span></h2>
<p><strong><span style="font-size: 15px">配置1:避免中文变成Unicode</span></strong></p>
<p>在<span style="color: rgba(255, 0, 0, 1); background-color: rgba(221, 221, 221, 1)"><strong> Startup.cs</strong></span> 类的<strong><span style="color: rgba(255, 0, 0, 1); background-color: rgba(221, 221, 221, 1)"> ConfigureServices</span></strong> 方法添加返回的格式定义为JSON,否则默认返回的是被编译的Unicode,不是中文。</p>
<blockquote>
<p>由于一些 JS 组件要求 JSON 格式是 PascalCase ,新版本 ASP.NET Core 3.0 中默认移除了 Newtonsoft.Json ,使用了微软自己实现的 System.Text.Json 来代替,可以修改继续使用 Newtonsoft.Json 来设置 PascalCase 格式属性名。</p>
</blockquote>
<div class="cnblogs_code">
<pre> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> This method gets called by the runtime. Use this method to add services to the container.</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> ConfigureServices(IServiceCollection services)
      {
            services.AddControllersWithViews();
            services.AddControllersWithViews().AddJsonOptions(options </span>=&gt;<span style="color: rgba(0, 0, 0, 1)">
            {
                options.JsonSerializerOptions.Encoder </span>=<span style="color: rgba(0, 0, 0, 1)"> JavaScriptEncoder.Create(UnicodeRanges.All);
            });
      }</span></pre>
</div>
<p><strong><span style="font-size: 15px">配置2:避免数据库版本过低和Docker版本不一致</span></strong></p>
<p><span style="font-size: 15px">在Dockerfile的最后添加一行代码,添加内容如下:</span></p>
<blockquote>
<p><span style="color: rgba(255, 0, 0, 1)">RUN sed -i 's/TLSv1.2/TLSv1.0/g' /etc/ssl/openssl.cnf</span></p>
</blockquote>
<p><span style="text-decoration: line-through"><span style="font-size: 15px">下面是详细解释,可以不看。</span></span></p>
<p><span style="font-size: 15px">我是用SqlServer2012进行操作,发现返回下面下面的35错误:</span></p>
<p><span style="font-size: 15px">A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: TCP Provider, error: 35 - An internal exception was caught</span></p>
<p><span style="font-size: 15px">出现问题的 asp.net core 程序是跑在容器中的,容器镜像用的是 mcr.microsoft.com/dotnet/core/aspnet:3.0</span></p>
<p><span style="font-size: 15px">运行容器内的 openssl 命令发现 openssl 的版本比较高</span></p>
<p><strong><span style="font-size: 15px">1.进入容器</span></strong></p>
<blockquote>
<p>docker exec -it [容器ID] bash</p>
</blockquote>
<p><strong><span style="font-size: 15px">2.查看容器openssl版本</span></strong></p>
<blockquote>
<p>openssl version</p>
<p>OpenSSL 1.1.1d 10 Sep 2019</p>
</blockquote>
<p><strong><span style="font-size: 15px">3.查看 openssl.cnf 配置文件</span></strong></p>
<blockquote>
<p>cat /etc/ssl/openssl.cnf</p>
<p> MinProtocol = TLSv1.1 CipherString = DEFAULT@SECLEVEL=2</p>
</blockquote>
<p><span style="font-size: 15px"> 发现允许的 ssl 最低版本是 TLSv1.2 ,而程序所使用的 SQL Server 数据库版本比较低不支持 TLSv1.2 ,修改为 TLSv1.0 后问题解决</span></p>
<p><strong><span style="font-size: 15px">4.修改方法:在 Dockerfile 中添加下面的指令</span></strong></p>
<blockquote>
<p>RUN sed -i 's/TLSv1.2/TLSv1.0/g' /etc/ssl/openssl.cnf</p>
</blockquote>
<hr>
<h1>&nbsp;<span style="font-size: 16px">3、生成Docker镜像并在浏览器运行</span></h1>
<p><span style="font-size: 15px">运行 <strong>Docker Desktop</strong>,也就是刚刚安装的docker,然后出现面板,直接点下面的按钮跳过,进入主面板。</span></p>
<p><img src="https://img2020.cnblogs.com/blog/1093832/202107/1093832-20210730163445576-900513004.png" alt="" loading="lazy"></p>
<p>&nbsp;</p>
<p><span style="font-size: 15px">在项目里面点击<span style="color: rgba(255, 0, 0, 1); background-color: rgba(221, 221, 221, 1)"><strong>Dockerfile</strong></span>右键,点击 <span style="color: rgba(255, 0, 0, 1); background-color: rgba(221, 221, 221, 1)">生成 Docker 映像</span></span><span style="font-size: 15px">,等待打包生成,生成后点查看Docker Desktop,在Images里面就有一个镜像包。如下</span></p>
<p><img src="https://img2020.cnblogs.com/blog/1093832/202107/1093832-20210730163835418-1500713779.png" alt="" loading="lazy"></p>
<p>&nbsp;</p>
<p><span style="font-size: 15px">&nbsp;点击镜像文件后面的RUN运行镜像,然后在弹出的狂内下拉打开,数据端口号<strong>8088</strong>,你也可以输入其他端口号,然后点击<strong>Run。</strong><br></span></p>
<p><span style="font-size: 15px"><strong><img src="https://img2020.cnblogs.com/blog/1093832/202107/1093832-20210730164306981-1663708697.png" alt="" loading="lazy"></strong></span></p>
<p>&nbsp;</p>
<p><span style="font-size: 15px">这个镜像文件就运行成功了,然后点击OPEN IN BROWSER(在浏览器打开)就可以看到效果了。</span></p>
<p><span style="font-size: 15px"><img src="https://img2020.cnblogs.com/blog/1093832/202107/1093832-20210730164604398-23878579.png" alt="" loading="lazy"></span></p>
<p><img src="https://img2020.cnblogs.com/blog/1093832/202107/1093832-20210730164653829-2081277478.png" alt="" loading="lazy"></p>
<p>&nbsp;</p>
<p><span style="font-size: 15px">然后在浏览器手动输入<strong>http://localhost:8088/Home/GetStudentList</strong>访问我们刚刚在Home控制器写的查询方法进行数据库交互验证,如下成功交互。</span></p>
<p><span style="font-size: 15px"><img src="https://img2020.cnblogs.com/blog/1093832/202107/1093832-20210730165004736-785631276.png" alt="" loading="lazy"></span></p>
<hr>
<h1>&nbsp;<span style="font-size: 16px">参考文献</span></h1>
<ul>
<li>HP工作站如何在BIOS下开启关闭虚拟化技术:https://jingyan.baidu.com/article/ce436649184b393773afd309.html</li>
<li>Docker 教程 | 菜鸟教程:https://www.runoob.com/docker/docker-tutorial.html</li>
<li>Docker——从入门到实践:https://www.kancloud.cn/docker_practice/docker_practice/469767</li>
<li>Docker 教程:https://www.jc2182.com/docker/docker-windows-install.html</li>
<li>Docker 百度百科:https://baike.baidu.com/item/Docker/13344470?fr=aladdin&nbsp;</li>






</ul>
<p>&nbsp;</p>
<table style="height: 149px; background-color: rgba(110, 151, 104, 1); width: 100%" border="2" align="left">
<tbody>
<tr>
<td>
<div style="float: left">&nbsp;</div>
<div style="float: left; margin: 0 0 0 10px; color: rgba(255, 255, 255, 1)"><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 14px"> <strong>欢迎关注订阅微信公众号【熊泽有话说】,更多好玩易学知识等你来取</strong></span><br><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 14px">
                              <strong>作者:熊泽-学习中的苦与乐</strong></span><br><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 14px"><strong>公众号:熊泽有话说</strong></span><br><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 14px">
                              <strong>
                                    出处:&nbsp;https://www.cnblogs.com/xiongze520/p/15069441.html</strong></span><br><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 14px">
                              <strong>
                                    创作不易,任何人或团体、机构全部转载或者部分转载、摘录,请在文章明显位置注明作者和原文链接。
                              </strong> &nbsp;

                            </span></div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 14px">&nbsp;</span></p>
















































            </td>
















































      </tr>
















































</tbody>















































</table>
<p>&nbsp;</p>
<p><img src="https://img2020.cnblogs.com/blog/1093832/202105/1093832-20210531090859260-762296935.png" alt="" width="522" height="243" loading="lazy" style="float: left"></p>
<p>&nbsp;</p>

</div>
<div id="MySignature" role="contentinfo">
    <p>本文来自博客园,作者:熊泽-学习中的苦与乐,转载请注明原文链接:https://www.cnblogs.com/xiongze520/p/15069441.html</p><br><br>
来源:https://www.cnblogs.com/xiongze520/p/15069441.html
頁: [1]
查看完整版本: windows环境30分钟从0开始快速搭建第一个docker项目(带数据库交互)