Docker部署Halo1.x

Docker部署Halo1.x

软件版本

  • Halo1.6.1,1.x最终版
  • Java11
  • MySQL8.0.32
  • Redis7.0.10
  • Nginx1.23.3

准备镜像

  1. 制作Halo镜像,也可官网下载,此处基于ubuntu自制镜像。

    FROM ubuntu
      
    MAINTAINER windcf<[email protected]>
    
    ENV MYPATH /usr/local
    
    WORKDIR $MYPATH
    
    ADD jdk11 $MYPATH/jdk11
    
    ENV JAVA_HOME $MYPATH/jdk11
    
    
    ADD halo-1.6.1.jar $MYPATH
    
    ENV PATH $PATH:$JAVA_HOME/bin
    
    EXPOSE 8090
    
    CMD ["java", "-jar", "./halo-1.6.1.jar"]
    

    并没有使用开源的Java11镜像,而是自己配置了Java环境。

  2. 拉取Redis、MySQL、Nginx镜像

    [root@VM-20-11-centos ~]# docker image list
    	REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
    	halo-docker   1.6.1     720bedb56b56   3 hours ago   476MB
    	nginx         latest    ac232364af84   5 days ago    142MB
    	redis         latest    31f08b90668e   5 days ago    117MB
    	mysql         latest    483a8bc460a9   6 days ago    530MB
    

编辑配置文件

  1. /root/.halo/application.yml
    server:
      port: 8090
    
      # Response data gzip.
      compression:
        enabled: false
    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://mysql:3306/halodb?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
        username: root
        password: 'password'
      redis:
        host: redis
        port: 6379
        database: 0
    halo:
      admin-path: admin
      cache: redis
    
  2. Nginx配置
    upstream halo {
    	  server halo:8090;
    	}
    	
    	server {
    	        listen 80;
    	        server_name windcf.com;
    	        client_max_body_size 1024m;
    	        # access_log /home/halo/.halo/logs/nginx_access_log.log;
    	        # error_log /home/halo/.halo/logs/nginx_error_log.log error;
    	        location / {
    	                proxy_pass http://halo;
    	                proxy_set_header HOST $host:$server_port;
    	                proxy_set_header X-Forwarded-Proto $scheme;
    	                proxy_set_header X-Real-IP $remote_addr;
    	                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    	        }
    	        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$ {
    	                expires 30d;
    	                root /root/.halo;
    	        }
    	        location ~ .*\.(js|css)$ {
    	                root /root/.halo/templates;
    	                expires 15d;
    	        }
    	
    	}
    
  3. MySQL字符集配置
  4. docker-compose.yml
    version: "3.8"
    
    services:
      halo:
        image: halo-docker:1.6.1
        container_name: halo
        #ports: 
        #- "8090:8090"
        volumes:
          - /data/halo:/root/.halo
        networks:
          - halo
        depends_on:
          - mysql
          - redis
      mysql:
        image: mysql
        container_name: mysql
        command: 
          - --character-set-server=utf8mb4
          - --collation-server=utf8mb4_general_ci
        environment:
          MYSQL_ROOT_PASSWORD: 'password'
          MYSQL_ALLOW_EMPTY_PASSWORD: 'no'
          MYSQL_DATABASE: 'database'
          MYSQL_PASSWORD: 'password'
        ports:
          - "3306:3306"
        volumes:
          - /data/mysql:/var/lib/mysql
          - /conf/mysql:/etc/mysql
          - /var/log/mysql:/var/log/mysql
        networks: 
          - halo
      nginx: 
        image: nginx
        container_name: nginx
        volumes: 
          - /conf/nginx:/etc/nginx
          - /var/log/nginx:/var/log/nginx
          - /data/halo:/root/.halo
        ports: 
          - "80:80"
        networks: 
          - halo
        depends_on: 
          - halo
      redis: 
        image: redis
        container_name: redis
        volumes: 
          - /conf/redis:/etc/redis
          - /data/redis:/data
            #ports: 
            #- "6379:6379"
        networks: 
          - halo
    networks:
      halo:
    

启动所有容器

[root@VM-20-11-centos halo-docker]# docker compose up -d
[+] Running 4/0
 ⠿ Container mysql  Running                                                                                                                                                           0.0s
 ⠿ Container redis  Running                                                                                                                                                           0.0s
 ⠿ Container halo   Running                                                                                                                                                           0.0s
 ⠿ Container nginx  Running              

遇到的问题

  1. Nginx配置无法解析halo服务名
    容器都配置在Docker网络halo下,检查后发现Halo服务依赖于Nginx,所以先启动Nginx,此时Halo还未加入网络,所以无法解析,配置Nginx依赖Halo解决问题。
  2. Nginx报错403
    容器中都是root用户操作,Nginx默认启动用户为nginx,读取本地文件有权限问题,修改/etc/nginx/nginx.conf启动用户为root。

你可能感兴趣的:(docker,nginx,java)