- 首先新增一个webapi的项目
项目核心代码 UserContext using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using User.API.Model; namespace User.API.Data { public class UserContext : DbContext { public UserContext(DbContextOptions<UserContext> options) : base(options) { } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<UserInfo>() .ToTable("Users") .HasKey(u => u.Id); base.OnModelCreating(modelBuilder); } public DbSet<UserInfo> Users { get; set; } } } 核心代码 userinfo namespace User.API.Model { public class UserInfo { public int Id { get; set; } public string Name { get; set; } public string Company { get; set; } public string Title { get; set; } } }
startup核心代码 public void ConfigureServices(IServiceCollection services) { services.AddDbContext<UserContext>(options => { options.UseMySQL(Configuration.GetConnectionString("MysqlUser")); }); services.AddMvc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseMvc(); //InitUserDataBase(app);//初始化数据库脚本再创建数据库之后取消注释 } public void InitUserDataBase(IApplicationBuilder app) { using (var scope=app.ApplicationServices.CreateScope()) { var userContext = scope.ServiceProvider.GetRequiredService<UserContext>(); userContext.Database.Migrate(); if (!userContext.Users.Any()) { userContext.Users.Add(new Model.UserInfo { Name = "cyao", Company = "nocompany", Title = "软件工程师" }); userContext.SaveChanges(); } } }
appsetting
{ "Logging": { "IncludeScopes": false, "Debug": { "LogLevel": { "Default": "Warning" } }, "Console": { "LogLevel": { "Default": "Warning" } } }, "ConnectionStrings": { "MysqlUser": "server=193.112.107.43;port=3306;database=bate_user;userid=test;password=pwd123456" } }#DockerFile核心 FROM microsoft/aspnetcore-build as build-env WORKDIR /code COPY *.csproj ./ RUN dotnet restore COPY . ./ RUN dotnet publish -c Release -o out FROM microsoft/aspnetcore WORKDIR /app COPY --from=build-env /code/out ./ EXPOSE 80 ENTRYPOINT [ "dotnet","User.API.dll" ]
数据库初始化的命令暂时省略
- 然后使用dockerfile进行镜像的build
首先将整个项目文件夹上传到liunx服务器 /NetCoreProJect/User.API/ #下面是命令 cd /NetCoreProJect/User.API/ #开始镜像的build 这里要注意的是文件的路径问题当build失败的时候docker images 的命令是没有 jess/aspnetcore命名的镜像的 docker build -t jess/aspnetcore:prod .
- 运行build的容器并以link的形式进行桥接(渐渐淘汰一般使用network形式)
docker run -d -p 8020:80 --name aspnetcoretest --link mysqldb:db jess/aspnetcore:prod docker ps -l
- 运行build的容器以network的形式进行桥接,并且将本地文件挂载到容器内部文件(容器与容器之间相互通信的端口号是容器内部映射的端口号而不是主机暴露出去的端口号例如这里是80而不是8003)
#新建一个network docker network create -d bridge mybridge #查看所有network docker network ls #运行容器并进行network 和资料卷的挂载首先你本地目录必须又appsettings.json文件 docker run -d -p 8003:80 --net mybridge --name aspnetcorebrige -v /docker/netcore/appsettings.json:/appsettings.json jess/aspnetcore:prod #命名mysqldb为db docker rename mysqldb db #两个容器进行network docker network connect mybridge db
#查看docker的bridge网络的详细情况 docker network inspect mybridge