From 4254975afd5e0132d530dd750cf9e89863a3f43d Mon Sep 17 00:00:00 2001 From: alimy Date: Tue, 7 Jun 2022 19:03:36 +0800 Subject: [PATCH] add docker-commpose support --- .gitignore | 1 + Dockerfile | 42 ++++++++++++++++++------------- README.md | 29 +++++++++++++++++++++ config.yaml.sample | 18 ++++++------- docker-compose.yaml | 61 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 124 insertions(+), 27 deletions(-) create mode 100644 docker-compose.yaml diff --git a/.gitignore b/.gitignore index e29a3b90..61bdf2ca 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ config.yaml *.log paopao-ce* release +data diff --git a/Dockerfile b/Dockerfile index 109cbea6..39f8608c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,26 +1,32 @@ -# build app -FROM golang AS build-env - -ADD . /paopao-ce +# build frontend +FROM node:18-alpine as frontend +WORKDIR /web +COPY web/ ./ +RUN echo 'VITE_HOST=""'>.env && yarn && yarn build + +# build backend +FROM golang:1.18-alpine AS backend +RUN apk --no-cache --no-progress add --virtual \ + build-deps \ + build-base \ + git WORKDIR /paopao-ce +COPY . . +COPY --from=frontend /web/dist ./web/dist +ENV GOPROXY=https://goproxy.cn +RUN make build TAGS='embed' -RUN CGO_ENABLED=0 go build . - -# safe image -FROM alpine - +FROM alpine:3.16 ENV TZ=Asia/Shanghai - RUN apk update && apk add --no-cache ca-certificates && update-ca-certificates -COPY --from=build-env /paopao-ce/paopao-ce /usr/bin/paopao-ce -COPY --from=build-env /paopao-ce/assets/comic.ttf /assets/comic.ttf -COPY --from=build-env /paopao-ce/configs /configs - -EXPOSE 8000 - -CMD ["paopao-ce"] +WORKDIR /app/paopao-ce +COPY --from=backend /paopao-ce/release/paopao-ce . +COPY assets ./assets +COPY configs ./configs -# HEALTHCHECK +VOLUME ["/app/paopao-ce/assets", "/app/paopao-ce/configs"] +EXPOSE 8008 HEALTHCHECK --interval=5s --timeout=3s --retries=3 CMD ps -ef | grep paopao-ce || exit 1 +ENTRYPOINT ["/app/paopao-ce/paopao-ce"] diff --git a/README.md b/README.md index 7ce7b803..edef78a9 100644 --- a/README.md +++ b/README.md @@ -152,6 +152,35 @@ PaoPao主要由以下优秀的开源项目/工具构建 桌面端是使用[Rust](https://www.rust-lang.org/) + [tauri](https://github.com/tauri-apps/tauri)编写 的,需要安装tauri的依赖,具体参考[https://tauri.studio/v1/guides/getting-started/prerequisites](https://tauri.studio/v1/guides/getting-started/prerequisites). +### docker-compose 运行 +```sh +%> git clone https://github.com/rocboss/paopao-ce.git +%> docker compose up --build +# visit http://localhost:8008 +``` +默认是使用config.yaml.sample的配置,如果需要自定义配置,请拷贝默认配置文件(比如config.yaml),修改后再同步配置到docker-compose.yaml如下: +``` +# file: docker-compose.yaml +... + backend: + build: + context: . + restart: always + depends_on: + - db + - redis + - zinc + # modify below to reflect your custom configure + volumes: + - ./config.yaml:/app/paopao-ce/config.yaml + ports: + - 127.0.0.1:8008:8008 + networks: + - paopao-network +.... +``` + + ### 其他说明 建议后端服务使用 `supervisor` 守护进程,并通过 `nginx` 反向代理后,提供API给前端服务调用。 diff --git a/config.yaml.sample b/config.yaml.sample index 80cea226..eb0a4280 100644 --- a/config.yaml.sample +++ b/config.yaml.sample @@ -12,8 +12,8 @@ Server: # 服务设置 ReadTimeout: 60 WriteTimeout: 60 Features: - Default: ["Sms", "Alipay", "Zinc", "MySQL", "Redis", "AliOSS", "LoggerZinc"] - Develop: ["Zinc", "MySQL", "AliOSS", "LoggerFile"] + Default: ["Alipay", "Zinc", "MySQL", "Redis", "AliOSS", "LoggerZinc"] + Develop: ["Sms", "Zinc", "MySQL", "AliOSS", "LoggerFile"] Slim: ["Zinc", "MySQL", "Redis", "AliOSS", "LoggerFile"] Sms: "SmsJuhe" SmsJuhe: @@ -24,11 +24,11 @@ Alipay: AppID: PrivateKey: LoggerFile: # 使用File写日志 - SavePath: storage/logs + SavePath: data/paopao-ce/logs FileName: app FileExt: .log LoggerZinc: # 使用Zinc写日志 - Host: http://127.0.0.1:4080/es/_bulk + Host: http://zinc:4080/es/_bulk Index: paopao-log User: admin Password: admin @@ -37,7 +37,7 @@ JWT: # 鉴权加密 Issuer: paopao-api Expire: 86400 Zinc: # Zinc搜索配置 - Host: http://127.0.0.1:4080 + Host: http://zinc:4080 Index: paopao-data User: admin Password: admin @@ -48,9 +48,9 @@ AliOSS: # 阿里云OSS存储配置 Bucket: Domain: MySQL: # MySQL数据库 - Username: root # 填写你的数据库账号 - Password: root # 填写你的数据库密码 - Host: 127.0.0.1:3306 + Username: paopao + Password: paopao + Host: db:3306 DBName: paopao TablePrefix: p_ Charset: utf8mb4 @@ -59,6 +59,6 @@ MySQL: # MySQL数据库 MaxIdleConns: 10 MaxOpenConns: 30 Redis: - Host: 127.0.0.1:6379 + Host: redis:6379 Password: DB: \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 00000000..57039243 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,61 @@ +version: '3.1' + +services: + db: + image: mysql:8.0 + restart: always + environment: + MYSQL_DATABASE: paopao + MYSQL_USER: paopao + MYSQL_PASSWORD: paopao + MYSQL_RANDOM_ROOT_PASSWORD: "yes" + volumes: + - ./scripts/paopao.sql:/docker-entrypoint-initdb.d/paopao.sql + - ./data/mysql/data:/var/lib/mysql + ports: + - 127.0.0.1::3306 + networks: + - paopao-network + + redis: + image: redis:7.0-alpine + restart: always + ports: + - 127.0.0.1::6379 + networks: + - paopao-network + + zinc: + image: public.ecr.aws/prabhat/zinc:latest + restart: always + ports: + - 127.0.0.1::4080 + volumes: + - ./data/zinc/data:/data + environment: + ZINC_FIRST_ADMIN_USER: admin + ZINC_FIRST_ADMIN_PASSWORD: admin + DATA_PATH: /data + networks: + - paopao-network + + backend: + build: + context: . + restart: always + depends_on: + - db + - redis + - zinc + # modify below to reflect your custom configure + volumes: + - ./config.yaml.sample:/app/paopao-ce/config.yaml + - ./data/paopao-ce/data:/app/paopao-ce/data + ports: + - 127.0.0.1:8008:8008 + networks: + - paopao-network + +networks: + paopao-network: + driver: bridge