- 定义docker compose
version: '3' services: composedb: image: mysql/mysql-server container_name: composedb command: mysqld --character-set-server=utf8 --collation-server=utf8_general_ci restart: always ports: - "3306:3306" environment: MYSQL_ROOT_PASSWORD: pwd123456 MYSQL_USER: test MYSQL_PASSWORD: pwd123456 volumes: - /docker/network/beta/mysql-init:/docker-entrypoint-initdb.d web: build: . container_name: aspnetcore ports: - "8004:80" #entrypoint: "sh ./wait-for-it.sh composedb:3306 -- sh ./aspnetcore.sh" #command: ["./wait-for-it.sh", "composedb:3306", "--", "./aspnetcore.sh"] depends_on: - composedb
- 修改初始化实力代码当数据库镜像初始化完毕后执行数据初始化
public static async Task SeedAsync(IApplicationBuilder ApplicationBuilder, ILoggerFactory LoggerFactory, int? retry = 0) { var retryForAvaibility = retry.Value; using (var scope = ApplicationBuilder.ApplicationServices.CreateScope())//数据库未初始化失败会catch { try { var context = (UserContext)scope.ServiceProvider.GetService(typeof(UserContext)); var logger = (ILogger<UserContextSeed>)scope.ServiceProvider.GetService(typeof(ILogger<UserContextSeed>)); logger.LogDebug("Begin UserContextSeed SeedAsyc"); context.Database.Migrate(); if (!context.Users.Any()) { context.Add(new Model.UserInfo { Name = "cyao", Company = "nocompany", Title = "软件工程师" }); context.SaveChanges(); } } catch (System.Exception ex) { retryForAvaibility++; if (retryForAvaibility > 10) { var logger = LoggerFactory.CreateLogger(typeof(UserContextSeed)); logger.LogError(ex.Message); await SeedAsync(ApplicationBuilder, LoggerFactory, retryForAvaibility); } } } }
- 其他的一些问题
1.高版本的MySQL 加密方法使用caching_sha2_password 2.低版本的Mysql创建的用户会有权限问题 使用root用户进入到bash 然后执行: GRANT ALL PRIVILEGES ON *.* TO 'test'@'%' WITH GRANT OPTION;
- liunx 跑到对应的docker-compose 文件路径后执行build 然后将docker-compose up起来
- docker-compose build 构建dockers-compose镜像
- docker-compose up
- docker-compose down