虚拟机Linux Red Hat 7.9 Docker部署.Net 7 Zr.Admin项目(后端)
0、环境信息
应用部署在虚拟机里的docker,里面的应用访问宿主主机的MySQL
1、开启MySQL远程访问
使用非安装版MySQL参考Windows 使用 非安装版MySQL 8
为了避免出现
Host is not allowed to connect to this MySQL server
使用root用户登录
cmd进入到MySQL的bin目录
执行
.\mysql.exe -u root -p
根据提示输入密码登录
选择mysql库
执行
use mysql;
更新用户权限
未更新权限前:
只允许本机访问
更新为所有地址都能访问
执行
update user set host = '%' where user = 'root';
由于我之前已经更新,所以Changed为0
刷新权限
执行
flush privileges;
为了避免出现
[Warning] [MY-010055] [Server] IP address '192.168.100.155' could not be resolved: 不知道 这样的主机。
my.ini 增加下面两个设置
阻止MySQL对外部连接进行DNS解析
skip-name-resolve
MySQL服务器监听所有可用的网络接口上的所有IP地址
bind-address = 0.0.0.0
修改完my.ini,要重启MySQL服务
完整my.ini
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_bin
init_connect='SET NAMES utf8mb4'
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
basedir = D:\lsp\mysql-8.0.40-winx64
datadir = D:\lsp\mysql-8.0.40-winx64\data
port = 3306
skip-name-resolve
# server_id = .....
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
join_buffer_size = 128M
sort_buffer_size = 16M
read_rnd_buffer_size = 16M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
bind-address = 0.0.0.0
2、Docker挂载MySQL
由于使用的MySQL并非部署在docker里,所以通过挂载MySQL的方式访问外部MySQL
安装Docker参考Linux Red Hat 7.9 Server安装Docker
拉取MySQL镜像
执行
docker pull swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/mysql:8.0
docker挂载MySQL
执行
sudo docker run --privileged=true \
-p 13306:3306 --name mysql \
-v /mydata/mysql/log:/var/log/mysql \
-v /mydata/mysql/data:/var/lib/mysql \
-v /mydata/mysql/conf:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=root \
-d swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/mysql:8.0
如果出现错误
[ERROR] [Entrypoint]: mysqld failed while attempting to check config
command was: mysqld --verbose --help --log-bin-index=/tmp/tmp.XNYZPu9o0r
mysqld: Can't read dir of '/etc/mysql/conf.d/' (OS errno 2 - No such file or directory)
mysqld: [ERROR] Stopped processing the 'includedir' directive in file /etc/my.cnf at line 36.
mysqld: [ERROR] Fatal error in defaults handling. Program aborted!
执行
mkdir -p /etc/mysql/conf.d
然后再重新执行挂载
3、Docker构建代码镜像
3.1 gitlab拉取代码
执行
git clone http://192.168.100.155:8088/lsp/zradmin.git
3.2 修改数据库访问地址
修改appsettings.json数据库连接信息,之前配置的是localhost,要改成宿主主机IP访问
进入配置文件目录:cd /lsp/code/zradmin/ZR.Admin.WebApi
修改保存
此时我们当前所在目录/lsp/code/zradmin/ZR.Admin.WebApi
复制Dockerfile 到上一级目录,即:/lsp/code/zradmin
执行
cp Dockerfile ../
3.3 构建镜像
执行
docker build -f Dockerfile -t zr-admin .
构建完成,查看生成的镜像
执行
docker images
3.4 启动镜像
执行
docker run --net=host -p 8888:80 zr-admin
如果出现下面错误,就是MySQL没开启远程访问
最终成功启动
服务器上访问测试
执行
curl http://localhost:8888
4、宿主主机访问
当回到宿主本地浏览器通过远程IP访问时,却是无法访问
上网查找了很多,有说是防火墙问题、Docker网卡冲突问题,但是我Linux防火墙的永久关闭的,也重新设置网卡地址
还是无效!!!
后面查到,我们的启动的服务是否监听了所有网络地址
查看appsettings.json,发下配置文件的服务监听的是localhost,这就解释了为什么在Linux服务器上通过localhost可以访问
把appsettings.json中这几个地方的localhost改成0.0.0.0
保存后,删除旧的zr-admin镜像,重新执行构建命令
删除镜像
执行
docker rmi $(docker images | grep zr-admin | awk '{print $3}')
如果删除不成功,报错
Error response from daemon: conflict: unable to delete ab8b2857eb11 (cannot be forced) - image is being used by running container cc1c4f6ea67d
则需要先停止容器,再删除容器
停止容器
执行
docker stop $(docker ps -a | grep 'zr-admin' | awk '{print $1}')
删除容器
执行
docker rm $(docker ps -a | grep 'zr-admin' | awk '{print $1}')
删除镜像
重新执行命令:docker rmi $(docker images | grep zr-admin | awk '{print $3}')
重新执行构建命令:docker build -f Dockerfile -t zr-admin .
重新执行运行命令:docker run --net=host -p 8888:80 zr-admin
最终宿主主机浏览器访问