docker pull mysql/mysql-servermysql
docker run -d -p 3306:3306 -e MYSQL_USER="lzl" -e MYSQL_PASSWORD="password" -e MYSQL_ROOT_PASSWORD="password" --name mysql01 mysql/mysql-server --character-set-server=utf8 --collation-server=utf8_general_cigit
①docker exec -it mysql01 bashsql
②mysql -uroot -pdocker
③GRANT ALL PRIVILEGES ON . TO 'LZL'@'%' WITH GRANT OPTION;json
MySql.Data.EntityFrameworkCore 8.0.18鏈接.netCore3.0 有Bug ,作Db遷移的時候失敗.所以採用Pomelo.EntityFrameworkCore.MySql驅動。c#
Microsoft.EntityFrameworkCore.tools。api
①新建文件夾Entityes、新建User瀏覽器
namespace Api.Entities { public class User { public int Id { get; set; } public string Name { get; set; } public string Company { get; set; } public string Title { get; set; } } }
②新建文件夾Data、新建UserContext.csbash
namespace Api.Data { public class UserContext:DbContext { public UserContext(DbContextOptions<UserContext> options):base(options) { } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<User>() .ToTable("t_ApiUser"); } public DbSet<User> Users { get; set; } } }
public void ConfigureServices(IServiceCollection services) { services.AddDbContext<UserContext>(options => { options.UseMySql(Configuration.GetConnectionString("MySqlConString")); }); services.AddControllers(); }
"ConnectionStrings": { "MySqlConString": "Server=mysql01;database=db_appuser;userid=lzl;password=password;" }
注意:此處Server=mysql01爲第一步Mysql容器的名稱。在本地開發的時候能夠設置爲對應的Db服務器地址。服務器
依次執行 Add-Migration IntialDb、Update-Database
StartUp.cs添加如下代碼初始化數據。
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); InitialDataBase(app); } public void InitialDataBase(IApplicationBuilder app) { using (var scope = app.ApplicationServices.CreateScope()) { var context = scope.ServiceProvider.GetRequiredService<UserContext>(); if (!context.Users.Any()) { context.Users.Add(new User() { Company = "kingdee", Name = "LZL", Title = "2020", Id = 1 }); context.SaveChanges(); } } }
namespace Api.Controllers { [ApiController] [Route("[controller]/[action]")] public class UserController : ControllerBase { private ILogger<UserController> _logger; private UserContext _userContext; public UserController(ILogger<UserController> logger,UserContext userContext) { _logger = logger; _userContext = userContext; } [HttpGet] public async Task<IActionResult> Get() { var users = await _userContext.Users.ToListAsync(); return new JsonResult(users); } } }
# 1.指定編譯和發佈應用的鏡像 FROM mcr.microsoft.com/dotnet/core/sdk:3.0 AS build-env # 2. 指定(編譯和發佈)工做目錄 WORKDIR /app # 3. 拷貝.csproj到工做目錄/app,而後執行dotnet restore恢復全部安裝的NuGet包 COPY *.csproj ./ RUN dotnet restore # 4. 拷貝當前項目目錄下全部文件到工做目錄(/app),而後執行dotnet publish命令將應用發佈到/app/out目錄下 COPY . ./ RUN dotnet publish -c Release -o out # 5. 編譯生成Docker鏡像 # 5.1.設置基礎鏡像 FROM mcr.microsoft.com/dotnet/core/aspnet:3.0 AS runtime # 5.2. 設置(運行)工做目錄,並將發佈文件拷貝到out子目錄下 WORKDIR /app COPY --from=build-env /app/out . # 5.3. 利用環境變量設置ASP.NET Core應用的監聽地址 ENV ASPNETCORE_URLS http://0.0.0.0:3827 # 5.4. 執行dotnet命令啓動ASP.NET Core應用 ENTRYPOINT ["dotnet", "Api.dll"]
docker build -t userapi:prod .
-t 鏡像名稱 dockerfile所在的目錄
docker network create -d bridge my-network
-d
參數指定 Docker 網絡類型,有 bridge
overlay
。其中 overlay
網絡類型用於 Swarm mode
docker run -d -p 8084:3827 --network my-net --name myuserapi userapi:prod
本機端口8084,容器監聽端口3827
docker network connect my-net mysql01 #添加進網絡 docker network disconnect my-net mysql01 #從網絡中移除
docker inspect myuserapi
docker inspect mysql01