Docker安装halo

本文详细介绍了如何在Docker环境下创建自定义网络,安装MySQL并设置权限,然后部署Halo博客系统,包括配置文件的修改和容器的启动。最后,讨论了通过Nginx进行域名绑定和端口配置,确保服务的稳定访问。
摘要由CSDN通过智能技术生成

Docker安装halo

自定义Docker网络

  1. 查看当前Docker网络

    [root@10-7-176-211 ~]# docker network ls
    NETWORK ID     NAME              DRIVER    SCOPE
    e7660459253e   bridge            bridge    local
    4ca31df30e45   example_default   bridge    local
    63d1d72302da   host              host      local
    f1ef8995ba94   none              null      local
    [root@10-7-176-211 ~]# docker network inspect bridge
    [
        {
            "Name": "bridge",
            "Id": "e7660459253e3941f01ab914cb8d444276f1c41b799fdba63e29a1b88650250d"                                                                                                             ,
            "Created": "2021-12-12T11:09:38.822166888+08:00",
            "Scope": "local",
            "Driver": "bridge",
            "EnableIPv6": false,
            "IPAM": {
                "Driver": "default",
                "Options": null,
                "Config": [
                    {
                        "Subnet": "172.17.0.0/16"
                    }
                ]
            },
            "Internal": false,
            "Attachable": false,
            "Ingress": false,
            "ConfigFrom": {
                "Network": ""
            },
            "ConfigOnly": false,
            "Containers": {
                "7ee06ea38f5985bf9b4cc917ffd44ea851982dbea8e02174d56507133ad1a95f":                                                                                                              {
                    "Name": "redis-standalone-dev",
                    "EndpointID": "a81bf88204b7cb422faadf58388ea758a0aa0c9da3c0b047c                                                                                                             83646c1400b1183",
                    "MacAddress": "02:42:ac:11:00:02",
                    "IPv4Address": "172.17.0.2/16",
                    "IPv6Address": ""
                },
                "98301fef949c07d4cece8612c407ba835969f29cbe32982fa75c1e6eb45ab953":                                                                                                              {
                    "Name": "pgsql-dev",
                    "EndpointID": "7a00e6e7246a6962e650c290b9523e9d895ca54024367fb60                                                                                                             47d3d9b77553efc",
                    "MacAddress": "02:42:ac:11:00:03",
                    "IPv4Address": "172.17.0.3/16",
                    "IPv6Address": ""
                }
            },
            "Options": {
                "com.docker.network.bridge.default_bridge": "true",
                "com.docker.network.bridge.enable_icc": "true",
                "com.docker.network.bridge.enable_ip_masquerade": "true",
                "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
                "com.docker.network.bridge.name": "docker0",
                "com.docker.network.driver.mtu": "1500"
            },
            "Labels": {}
        }
    ]
    
    

    重点信息: "Subnet": "172.17.0.0/16""IPv4Address": "172.17.0.3/16"

  2. 创建自定义网络并指定网段

    [root@10-7-176-211 ~]# docker network create -d bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 halo-net
    4257e42bbf3bb64ec39544a5a6ba49a345df556400f3d8ec36099c59f09539eb
    [root@10-7-176-211 ~]# docker network ls
    NETWORK ID     NAME              DRIVER    SCOPE
    e7660459253e   bridge            bridge    local
    4ca31df30e45   example_default   bridge    local
    4257e42bbf3b   halo-net          bridge    local
    63d1d72302da   host              host      local
    f1ef8995ba94   none              null      local
    

    -d: 指定驱动,网络类型

    –subnet: 指定子网网段

    –gateway: 指定子网网关

    halo-net: 网络名称

  3. 查看新建的网络详情

    [root@10-7-176-211 ~]# docker network inspect halo-net
    [
        {
            "Name": "halo-net",
            "Id": "4257e42bbf3bb64ec39544a5a6ba49a345df556400f3d8ec36099c59f09539eb",
            "Created": "2022-02-20T19:49:49.295905019+08:00",
            "Scope": "local",
            "Driver": "bridge",
            "EnableIPv6": false,
            "IPAM": {
                "Driver": "default",
                "Options": {},
                "Config": [
                    {
                        "Subnet": "192.168.0.0/16",
                        "Gateway": "192.168.0.1"
                    }
                ]
            },
            "Internal": false,
            "Attachable": false,
            "Ingress": false,
            "ConfigFrom": {
                "Network": ""
            },
            "ConfigOnly": false,
            "Containers": {},
            "Options": {},
            "Labels": {}
        }
    ]
    

    重点: "Subnet": "192.168.0.0/16""Gateway": "192.168.0.1"以及容器情况,现在没有任何容器链接到该网络,所以是空的: "Containers": {}

Docker安装MySql

  1. 创建mysql宿主目录

    [root@10-7-176-211 opt]# mkdir -p /opt/halo/mysql
    
  2. 运行Docker命令创建Mysql容器

    [root@10-7-176-211 opt]# docker run --name some-mysql -v /opt/halo/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw --net halo-net --restart=unless-stopped -d mysql/mysql-server
    [root@10-7-176-211 opt]# docker ps -a
    CONTAINER ID   IMAGE                    COMMAND                  CREATED          STATUS                             PORTS    NAMES
    11d199640bb7   mysql/mysql-server       "/entrypoint.sh mysq…"   25 seconds ago   Up 24 seconds (health: starting)   3306/tcp, 33060-33061/tcp      some-mysql
    
    
  3. 进入容器

    [root@10-7-176-211 opt]# docker exec -it some-mysql /bin/bash
    bash-4.4#
    
  4. 在容器中增加用户admin,指定hosthalo容器的IP,并授权admin用户

    mysql> use mysql;
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Database changed
    mysql> create user 'admin'@'192.168.0.3' identified by 'my-secret-pw';
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> grant all on *.* to 'admin'@'192.168.0.3';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> select host, user from user where user='admin';
    +-------------+-------+
    | host        | user  |
    +-------------+-------+
    | 192.168.0.3 | admin |
    +-------------+-------+
    1 row in set (0.00 sec)
    
    
  5. 创建halo数据库

    mysql> create database halodb character set utf8mb4 collate utf8mb4_bin;
    Query OK, 1 row affected (0.01 sec)
    

Docker安装Halo

  1. 下载halo配置样例,并对其进行修改

    [root@10-7-176-211 halo]# wget https://dl.halo.run/config/application-template.yaml -O ./application.yaml--2022-02-20 21:01:44--  https://dl.halo.run/config/application-template.yamlResolving dl.halo.run (dl.halo.run)... 104.21.22.75, 172.67.203.114, 2606:4700:3032::ac43:cb72, ...Connecting to dl.halo.run (dl.halo.run)|104.21.22.75|:443... connected.HTTP request sent, awaiting response... 200 OKLength: 798 [application/octet-stream]Saving to:./application.yaml’./application.yaml                              100%[====================================================================================================>]     798  --.-KB/s    in 0s2022-02-20 21:01:46 (11.4 MB/s) -./application.yaml’ saved [798/798]
    

    修改的内容包括:

    • 注释 H2 database configuration.部分
    • 启用 MySQL database configuration.部分
    • 修改 datasource 下的 url 中的 ip 地址部分为容器名称并修改密码为您设定的 MySQL 密码
    • 修改username为刚刚创建并授权了的admin用户和密码

    修改的内容:

    spring:  datasource:    driver-class-name: com.mysql.cj.jdbc.Driver    url: jdbc:mysql://some-mysql:3306/halodb?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true    username: admin    password: my-secret-pw
    
  2. 创建指定IP地址的halo容器

    [root@10-7-176-211 halo]# docker run -it -d --name halo -p 8091:8090 --ip 192.168.0.3 -v /opt/halo:/root/.halo --net halo-net --restart=unless-stopped halohub/halo
    
  3. 查看halo容器是否正常启动

halo控制台显示后端程序启动成功
halo前台启动成功

出现这两个图就说明服务器部署成功了!

解析域名并关闭开放的临时端口

绑定和解析域名,并把为看到效果而临时开放的端口8091关闭。

配置Nginx

  1. nginx.conf添加如下配置

    server {        listen 80;        server_name www.xxxxx.com;        location / {                proxy_pass http://127.0.0.1:8091;        }   }   server {        listen 80;        server_name xxxxx.com;        location / {                proxy_pass http://127.0.0.1:8091;        }   }
    
  2. 运行如下nginx命令,重新加载配置

    [root@10-7-176-211 nginx]# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@10-7-176-211 nginx]# nginx -s reload
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值