12
09

一、要点

  • 目标:通过域名访问博客
  • 前提准备:ECS(必须)、证书(非必须)、域名(必须)、docker/docker-compose/git安装

二、Docker安装

具体请参考 CentOS7下安装Docker

三、Docker-compose安装

docker-compose是一个用于定义和运行多个docker容器应用的工具,可以用YAML文件来配置应用服务,然后一个命令,可以实现部署配置的所以服务。

下载Docker-compose:

[root@localhost ~]# curl -L https://get.daocloud.io/docker/compose/releases/download/1.24.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   423  100   423    0     0    199      0  0:00:02  0:00:02 --:--:--   199
100 15.4M  100 15.4M    0     0  5381k      0  0:00:02  0:00:02 --:--:-- 22.6M

修改文件的权限为可执行:

[root@localhost ~]# chmod +x /usr/local/bin/docker-compose 

检验是否安装成功:

[root@localhost ~]# docker-compose -v
docker-compose version 1.24.0, build 0aa59064

四、Git clone博客部署文件

Github地址:solo-for-docker

目录结构:

[root@localhost https]# tree
.
├── docker-compose.yml
├── mysql
│   ├── data
│   └── init
│       └── init.sql
└── nginx
    ├── conf
    │   ├── nginx.conf
    │   └── www.taotao-java.com.log
    └── www
        └── index.html

6 directories, 5 files
[root@localhost https]# 

五、nginx配置

[root@localhost conf]# cat nginx.conf 
gzip on;
gzip_min_length  1k;
gzip_buffers     4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
gzip_vary on;
gzip_proxied   expired no-cache no-store private auth;
gzip_disable   "MSIE [1-6]\.";

server_tokens off;
access_log off;

server {
  server_name www.taotao-java.com taotao-java.com;
  listen 80;
  location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $http_host;
    proxy_pass http://solo:8080;
  }
  access_log  /etc/nginx/conf.d/www.taotao-java.com.log;
} 

六、初始化init.sql

[root@localhost init]# cat init.sql 
CREATE DATABASE  `solo` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
grant all PRIVILEGES on *.* to root@'%' identified by 'adminadmin';
flush privileges;
[root@localhost init]# 

七、docker-compose.yml文件

version: "2"

services:
  mysql:
    container_name: mysql
    image: mysql:5.7
    restart: always
    volumes:
      - ./mysql/data:/var/lib/mysql  //mysql的数据文件存放地址
      - ./mysql/init:/docker-entrypoint-initdb.d //初始化sql脚本
      - /etc/localtime:/etc/localtime:ro
    ports:
      - "6603:3306"   //6603宿主机端口,3306容器端口
    environment:
      MYSQL_ROOT_PASSWORD: "adminadmin" //mysql的root密码
      TZ: "Asia/Shanghai"
    command: --max_allowed_packet=32505856
  solo:
    container_name: solo
    image: b3log/solo:latest  //使用最新solo镜像
    restart: always
    ports:
      - "8080:8080"
    environment:
      RUNTIME_DB: "MYSQL"
      JDBC_USERNAME: "root"
      JDBC_PASSWORD: "adminadmin"
      JDBC_DRIVER: "com.mysql.jdbc.Driver"
      JDBC_URL: "jdbc:mysql://mysql:3306/solo?useUnicode=yes&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC"
    command: --listen_port=8080 --server_port=80 --server_scheme=http --server_host=taotao-java.com
  nginx:
     container_name: nginx
     image: nginx:latest
     restart: always
     ports:
       - "80:80"
     volumes:
       - "./nginx/conf:/etc/nginx/conf.d" //映射nginx目录到docker容器中
       - "./nginx/www/:/var/www/" //映射www目录到docker容器中,部署静态站点

注:仅当 DB的数据目录为空(数据库服务的第一次运行)时,才会加载此./mysql/init文件夹中的SQL文件。 (即您的情况)./mysql/data文件夹应为空,docker-compose后面Docker系列文章会详细讲解,关注公众号:taotao-java

八、部署

  • 克隆项目到自己的服务器上
  • 替换docker-compose.yml,nginx配置的域名
  • 命令启动
[root@localhost https]# ll
total 4
-rw-r--r--. 1 root root 1079 Dec  6 22:28 docker-compose.yml
drwxr-xr-x. 4 root root   30 Dec  5 03:45 mysql
drwxr-xr-x. 4 root root   29 Dec  5 03:26 nginx
[root@localhost https]# docker-compose -f docker-compose.yml up -d
Starting nginx ... done
Starting mysql ... done
Starting solo  ... done
[root@localhost https]# 

九、验证并访问博客

  • 本次搭建在本地环境,需要配置本地hosts
  • 域名访问博客

标题:Docker系列基于solo十分钟搭建个人博客
作者:chenjuntao
地址:http://taotaojava.com/articles/2020/12/09/1607513117785.html

标签:   浏览数: 0