1.構建運行mysql容器,添加數據庫user
參考Docker創建運行多個mysql容器,地址 http://www.cnblogs.com/heyangyi/p/9288402.html
添加user數據庫,添加tbusers表

2.創建asp.net core webapi 應用程序
參考Docker 為 ASP.NET Core WebApi 應用程序生成 Docker 映像,創建容器並運行,地址 http://www.cnblogs.com/heyangyi/p/9323407.html
<2.1> 修改 appsettings.json 文件,添加 dbconn 數據庫鏈接配置

<2.2> 新增Config類,用來存儲配置
public class Config
{
public static string dbconn;
}
<2.3> 修改Program 類,讀取配置
public class Program
{
private static IConfigurationRoot Configuration { get; set; }
public static void Main(string[] args)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
Configuration = builder.Build();
Config.dbconn = Configuration.GetValue<string>("dbconn");
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
<2.4> 新增DataContext類
安裝引用:MySql.Data.EntityFrameworkCore

public class DataContext : DbContext
{
public DbSet<tbuser> tbusers { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.UseMySQL(Config.dbconn);
}
public class tbuser
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int id { get; set; }
public string nickName { get; set; }
public string email { get; set; }
}
<2.5> 新增tbuserDataHandle類
public class tbuserDataHandle : DataContext
{
public static int adduser(tbuser user)
{
try
{
using (var context = new DataContext())
{
context.Database.EnsureCreated();
context.Add(user);
context.SaveChanges();
}
return 200;
}
catch (Exception ex)
{
return 300;
}
}
public static List<tbuser> Get()
{
try
{
using (var context = new DataContext())
{
var users = context.tbusers;
List<tbuser> items = new List<tbuser>();
foreach (var item in users)
{
items.Add(item);
}
return items;
}
}
catch (Exception ex)
{
return null;
}
}
public static tbuser Get(int id)
{
try
{
using (var context = new DataContext())
{
var u = context.tbusers.Find(id);
return u;
}
}
catch (Exception ex)
{
return null;
}
}
public static int Delete(int id)
{
try
{
using (var context = new DataContext())
{
var u = context.tbusers.Remove(new tbuser() { id = id });
context.SaveChanges();
return 200;
}
}
catch (Exception ex)
{
return 300;
}
}
public static int Put(int id, tbuser user)
{
try
{
using (var context = new DataContext())
{
var u = context.tbusers.Update(user);
context.SaveChanges();
return 200;
}
}
catch (Exception ex)
{
return 300;
}
}
}
<2.6> 新增userController Api
[Produces("application/json")]
[Route("api/user")]
public class userController : Controller
{
// POST api/user
[HttpPost]
public int Post(tbuser user)
{
return tbuserDataHandle.adduser(user);
}
// GET api/user
[HttpGet]
public List<tbuser> Get()
{
return tbuserDataHandle.Get();
}
// GET api/user/5
[HttpGet("{id}")]
public tbuser Get(int id)
{
return tbuserDataHandle.Get(id);
}
// DELETE api/user/5
[HttpDelete("{id}")]
public int Delete(int id)
{
return tbuserDataHandle.Delete(id);
}
// PUT api/user/5
[HttpPut("{id}")]
public int Put(int id, tbuser user)
{
return tbuserDataHandle.Put(id, user);
}
}
3.生成項目,構建docker鏡像並創作容器運行
修改 docker-compose.yml ,docker-compose.override.yml 的 version 為:
version: '2.0'
配置 appsettings.json 的 dbconn 為:
"dbconn": "server=192.168.99.100;user id=root;password=123456;persistsecurityinfo=True;port=3307;database=user;SslMode=none"
進入到 E:\web\ilinkcore (這個目錄為解決方案的根目錄)
docker-compose up

- up 自動完成包括構建鏡像,創建服務,啟動服務並關聯服務容器的一系列操作
執行成功后創建一個 ilinkcore 的鏡像,並且運行了一個 ilinkcore_ilinkcore_1的容器,將本機的32783端口映射到容器的80端口


4.測試訪問接口
添加反向代理,修改nginx 配置
server{
listen 84;
server_name localhost;
location / {
proxy_pass http://192.168.99.100:32783;
index index.html index.htm;
}
}
重新運行nginx,使用Postman進行api接口測試

<4.1> 測試接口進行添加user數據

Headers 中添加項:
Content-Type:application/json
<4.2>查看所有用戶數據

<4.3> 檢索某個用戶數據

<4.4> 更新某個用戶數據

Headers 中添加項:
Content-Type:application/json

<4.5> 刪除單個用戶


