• .NET Core+MySql+Nginx 容器化部署


    1. 引言

    上两节我们通过简单的demo学习了docker的基本操作。这一节我们来一个进阶学习,完成ASP.NET Core + MySql + Nginx的容器化部署。

    本文是基于CentOS 7.4环境进行演示,示例项目可以访问Docker.NetCore.MySql进行下载。

    2. Hello MySQL

    同样我们还是以循序渐进的方式来展开。首先来基于Docker来试玩一下MySQL。

    2.1. 创建MySql实例

    注:如果linux系统之前已经安装过mysql了,这里的 -p 3306:3306就不要用3306端口了,不然本机是能通,但远程连的时候就出岔子了,搞了半天报错:

    Access denied for user 'root'@'

    就是连不上,后来发现之前系统上面已经安装过mysql,停止服务时,用service mysqld stop都不行,原来安装的是mariadb,用下面命令禁止启动:

    systemctl stop mariadb.service #停止MariaDB
    systemctl disable mariadb.service #设置开机启动

    一定要加上参数-p 3306:3306,这样端口是0.0.0.0:3306->3306/tcp就表示绑定成功,不用做多余设置,远程直接可以连接mysql

    [root@localhost ~]# docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                            NAMES
    18858ef84b82        mysql:5.6           "docker-entrypoint..."   9 seconds ago       Up 8 seconds        0.0.0.0:3306->3306/tcp           hello.mysql
    e19e2dc74ffa        mycore              "dotnet MyApi.dll"       2 hours ago         Up 6 minutes        8080/tcp, 0.0.0.0:5002->80/tcp   hello.netcore.mysql
    //拉取mysql镜像
     docker pull mysql
    $ docker images$
    REPOSITORY                           TAG                 IMAGE ID            CREATED             SIZE
    docker.io/mysql                      latest              7d83a47ab2d2        13 days ago         408.2 MB
    //创建一个mysql实例
    $ docker run --name hello.mysql -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql 
    $ docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    e21bbd84e0b5 mysql
    "docker-entrypoint.sh" 3 minutes ago Up 3 minutes 3306/tcp hello.mysql

    下面我们直接在容器中连接到我们刚刚创建的mysql数据库:

    $ docker exec -it hello.mysql 
    > mysql -uroot -p123456
    mysql: [Warning] Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor.  Commands end with ; or g.
    Your MySQL connection id is 8
    Server version: 5.7.20 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
    
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    +--------------------+
    4 rows in set (0.00 sec)

    2.2. 挂载数据卷

    上面创建的mysql实例其数据都在容器内部存储,这样就暴露了一个问题,如果容器销毁,那么对应的数据库数据就会丢失。那如何持久化存储容器内数据呢?我们可以通过挂载数据卷的方式来解决这一问题。

    //创建数据卷
    $ docker volume create --name hello.db
    hello.db
    //查看数据卷信息
    $ docker volume inspect hello.db
    [
        {
            "Name": "hello.db",
            "Driver": "local",
            "Mountpoint": "/var/lib/docker/volumes/hello.db/_data",
            "Labels": {},
            "Scope": "local"
        }
    ]
    // 挂载数据卷启动MySql实例
    $ docker run --name hello.mysql 
    > -v hello.db:/var/lib/mysql 
    > -e MYSQL_ROOT_PASSWORD=123456 -d mysql

    上面是使用使用了docker volume create命令创建了一个数据卷,当然我们也可以自行挂载某个目录作为数据卷。

    3. 准备.NET Core+EFCore+MySql项目

    4. 基于示例项目进行实操演练

    这里略过,查看之前的随笔

    4.3. 启动镜像并连接到指定数据库

    docker提供了--link参数用于在容器之间建立连接。下面我们实例化创建的镜像docker.netcore.mysql并命名容器名为hello.netcore.mysql,并使用--link参数与我们文章开头建立的hello.mysql容器建立连接。

    docker run --name hello.netcore.mysql --link hello.mysql:db -d -p 5002:80 mycore
    这里需要特别注意一下--link=hello.mysql:db,这个参数就是告诉Docker容器需要使用hello.mysql容器,并将其别名命名为db,这样在hello.netcore.mysql这个容器中就可以使用db来作为提供mysql数据库服务的服务器。这也就是为什么我们.NET Core项目中连接字符串设置为server=db;的原因。
    "ConnectionStrings": { "MySql": "server=db;database=MySqlDbContext;uid=root;pwd=123456;" }

    [root@localhost tmp]# docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                            NAMES
    e19e2dc74ffa        mycore              "dotnet MyApi.dll"       20 minutes ago      Up 20 minutes       8080/tcp, 0.0.0.0:5002->80/tcp   hello.netcore.mysql
    1c00e198b2e6        mycore              "dotnet MyApi.dll"       23 minutes ago      Up 22 minutes       8080/tcp, 0.0.0.0:5001->80/tcp   mycore
    fc19cb375752        mysql:5.6           "docker-entrypoint..."   42 minutes ago      Up 42 minutes       3306/tcp                         hello.mysql
    [root@localhost tmp]# curl http://192.168.1.120:5002/api/user
    [{"id":1,"nickName":"ss","email":"ddd"}][root@localhost tmp]#

    如上,访问http://192.168.1.120:5002/api/user可以看到mysql数据库连接成功,并且自动创建好了库和表

     


  • 相关阅读:
    20080408 VS2003 中 Jscript 文件中文乱码问题
    20080330 single process memory on Windows and Windows virtual memory
    20080331 Corillian's product is a Component Container Name at least 3 component containers that ship now with the Windows Server Family
    20080330 the difference between an EXE and a DLL
    20080408 Javascript中的字符串替换replaceG
    20080501 修复Windows Update 自动更新
    20080331 How many processes can listen on a single TCPIP port
    20080329 What is a Windows Service and how does its lifecycle differ from a standard EXE
    20080407 Fire in the hole
    20080328 Adobe Launches Webbased Photoshop Express
  • 原文地址:https://www.cnblogs.com/xtjatswc/p/10334784.html
Copyright © 2020-2023  润新知