.netcoreapi docker-compose the container and the container mysql

Preface:

Based on a .NetCoreApi container and container MySql Internet , where the use of docker-compose to quickly configure and start the mysql container .NetCoreApi container.

note:

Write indent docker-compose.yml Do not use tab, to be direct with spaces (fucked me for a long time ...)

First, write docker-compose.yml file

version: "3"
services:
  
  #mysql容器配置
  db:
    image: mysql/mysql-server  #使用镜像
    command: mysqld --character-set-server=utf8 --collation-server=utf8_general_ci  #相当于dockerfile中my.cnf中的配置,要以mysqld开头,否则不会生效
    container_name: db  #mysql容器名称 此处很关键,要与Web应用中连接字符串server保持一致(上面的db是只是services的名称)             
    restart: always
    ports:
      - "3306:3306"
    environment:
      - MYSQL_PASSWORD=password
      - MYSQL_ROOT_PASSWORD=password
      - MYSQL_USER=lzl
    volumes:  #数据库初始化sql脚本目录映射  本机目录:容器目录(按照官方的来) mysql启动的时候执行该脚本
      - C:/Users/Luo/source/repos/Api/Api/MySql-InitScript:/docker-entrypoint-initdb.d/
      
  #web应用配置
  web:
    build: .  #利用web应用的dockerfile来构建容器。 .为dockerfile所在的目录 
    container_name: "aspnetcoreapi"  #容器名
    ports:
      - "8004:3827"
    depends_on:  #web应用依赖于mysql容器,此处设置让mysql容器先启动,再让web容器再启动。(注意:并不会等待mysql容器完全启动)
      - db

Second, the initialization sql script (C: / Users / Luo / source / repos / Api / Api / MySql-InitScript)

#给用户授权(用于创建数据库以及数据表)
GRANT ALL PRIVILEGES ON *.* TO 'lzl'@'%' WITH GRANT OPTION;

#改变密码的加密方式(旧版本的navicat客户端不支持新版本mysql的密码加密方式,因此切换为原来的加密方式,否则连接不上mysql)
alter user 'lzl'@'%' identified with mysql_native_password by 'password';

note:

When mapping initialization sql script, start when you are prompted to drive is not shared.image-20200111200053441

Solution:

Right docker for windows-> settings-> Shared Drives can check the corresponding letter.

Third, modifying netcoreapi SeedData codes (since the container mysql slow start, rapid start API, thus not being given connection mysql)

By adding error retry until successful start mysql container.

public void InitialDataBase(IApplicationBuilder app,int? retry=0)
        {
            var retryTimes = retry.Value;
            using (var scope = app.ApplicationServices.CreateScope())
            {
                try
                {
                    var context = scope.ServiceProvider.GetRequiredService<UserContext>();
                    context.Database.Migrate();
                    if (!context.Users.Any())
                    {
                        context.Users.Add(new User()
                        {
                            Company = "kingdee",
                            Name = "LZL",
                            Title = "2020",
                            Id = 1
                        });
                        context.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    retryTimes ++;
                    if(retryTimes<10)
                    {
                        InitialDataBase(app, retryTimes);
                    }

                }
            }
        }

Fourth, start the container arrangement

In order to start executing the following command:

1, constructed docker-compose the image
docker-compose build

View build two mirrors

docker ps 

image-20200111204937419

2, start docker-compose
docker-compose up
3. Check the operation of the vessel
docker-compose ps

image-20200111201029864

4、访问http://localhost:8004/User/Get

image-20200111204553897

PS: after starting the mysql container is not connected, then try restarting the web application (docker restart aspnetcoreapi). If the restart can access, you can increase the number of fault-tolerant SeedData.

Guess you like

Origin www.cnblogs.com/roluodev/p/12181180.html