From a8c4f95a6e0cd56c7b21ed3a072b78db0273a84a Mon Sep 17 00:00:00 2001 From: South Drifted Date: Fri, 6 Oct 2023 00:29:08 +0000 Subject: [PATCH 1/2] docs: add Docker deployment guide fix: some Shell Command bugs --- .github/workflows/deploy-oss.yml | 1 + Dockerfile | 1 + README.md | 57 ++++++++++++++++++++++++++++++++ start-docker.sh | 4 +-- 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/.github/workflows/deploy-oss.yml b/.github/workflows/deploy-oss.yml index 02fa95e..cbfd406 100644 --- a/.github/workflows/deploy-oss.yml +++ b/.github/workflows/deploy-oss.yml @@ -59,5 +59,6 @@ jobs: command: | cd /tmp mv docker-compose.yml .env start-docker.sh ~/ + cd ~/ chmod +x ~/start-docker.sh sudo ~/start-docker.sh ${{ env.ARTIFACT_PATH }} diff --git a/Dockerfile b/Dockerfile index af72359..b8a07c0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,6 +11,7 @@ COPY package.json package-lock.json file-content-injector.js /home/node/app/ RUN npm i COPY . /home/node/app +RUN npm run build RUN npm prune --production || true \ npm cache clean -f diff --git a/README.md b/README.md index d017a38..89b3172 100644 --- a/README.md +++ b/README.md @@ -362,6 +362,63 @@ These two services are very much alike. In fact, `Polyfiller` depends on the lib The server is built with support for both HTTP2 and HTTP. The environment variable `HTTP2=[true|false]` decides whether a HTTP2 server will be hosted or not. If you use a load balancer and something like `nginx` in a reverse proxy setup, please know that `nginx` doesn't support HTTP2 via its proxy module, so you have to use HTTP1.1 there. Thankfully, it is as easy as setting `HTTP2=false` before launching the server and setting `HTTPS=false`. +#### Docker + +> This guide has been tested in the deployment process of China mirror: https://polyfiller.kaiyuanshe.cn + +##### Simple container + +Run shown commands in the Project Root folder: + +```shell +docker build -t polyfiller/api-service . +docker run --name polyfiller -e NODE_ENV=production -p 3000:3000 polyfiller/api-service +``` + +##### Composed services with Object Storage + +Install Docker plugins in Cloud Server at first: + +```shell +sudo apt install docker-compose +sudo docker plugin install juicedata/juicefs +``` + +###### 1. Manual deployment + +1. Write [JuiceFS environment variables](https://juicefs.com/docs/community/juicefs_on_docker/#using-docker-compose) into `.env` file in the Project Root folder: + +```ini +STORAGE_TYPE = +BUCKET = +ACCESS_KEY = +SECRET_KEY = +``` + +2. Run shown commands in the Project Root folder: + +```shell +docker-compose up -d +``` + +###### 2. Automatic deployment + +1. Set GitHub Repository secrets: + +| name | value | +| :--------: | :-------------------------------: | +| `ENV_FILE` | `.env` file shown above | +| `HOST` | IP or Domain Name of Cloud Server | +| `USER` | Account Name of Cloud Server | +| `SSH_KEY` | SSH Private Key of Cloud Server | + +2. Push a Git tag: + +```shell +git tag v0.2.3-oss # the version number is the value of "version" field in "package.json" +git push origin --tags +``` + ## Logo All credits go to [Andreas Mehlsen (@andreasbm)](https://github.com/andreasbm/) for the awesome logo design. diff --git a/start-docker.sh b/start-docker.sh index f5051b9..7133b8b 100644 --- a/start-docker.sh +++ b/start-docker.sh @@ -3,7 +3,7 @@ docker load < $1 docker image ls -a -docker compose down -docker compose up -d +docker-compose down --volumes +docker-compose up -d docker image prune -a -f From b188437773e14871bdfcf264268de908e5e24415 Mon Sep 17 00:00:00 2001 From: South Drifted Date: Thu, 23 Nov 2023 01:05:57 +0000 Subject: [PATCH 2/2] [add] Health Check service in Docker Compose [optimize] reduce Docker image size --- Dockerfile | 24 ++++++++--------- docker-compose.yml | 66 +++++++++++++++++++++++++++++----------------- 2 files changed, 54 insertions(+), 36 deletions(-) diff --git a/Dockerfile b/Dockerfile index b8a07c0..a6f5a80 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,20 +1,20 @@ -FROM node:18-slim +# Reference: https://pnpm.io/docker#example-1-build-a-bundle-in-a-docker-container -USER root +FROM node:18-slim AS base +RUN apt-get update && \ + apt-get install curl -y --no-install-recommends +COPY . /app +WORKDIR /app -RUN npm rm yarn -g +FROM base AS prod-deps +RUN npm i --prod -RUN mkdir /home/node/app -WORKDIR /home/node/app - -COPY package.json package-lock.json file-content-injector.js /home/node/app/ +FROM base AS build RUN npm i - -COPY . /home/node/app RUN npm run build -RUN npm prune --production || true \ - npm cache clean -f - +FROM base +COPY --from=prod-deps /app/node_modules /app/node_modules +COPY --from=build /app/dist /app/dist EXPOSE 3000 CMD ["npm", "start"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 19192c0..fdb9a9e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,4 @@ -version: '3' +version: "3" volumes: polyfill-cache: @@ -12,28 +12,46 @@ volumes: secret-key: ${SECRET_KEY} networks: - polyfiller: + polyfiller: services: - api-service: - image: polyfiller/api-service - environment: - - NODE_ENV=production - ports: - - 3000:3000 - volumes: - - polyfill-cache:/tmp/@wessberg/polyfiller - networks: - - polyfiller - restart: always - caddy: - depends_on: - - api-service - image: caddy - ports: - - 80:80 - - 443:443 - networks: - - polyfiller - restart: always - command: caddy reverse-proxy --from polyfiller.app --to api-service:3000 + api-service: + image: polyfiller/api-service + environment: + - NODE_ENV=production + ports: + - 3000:3000 + volumes: + - polyfill-cache:/tmp/@wessberg/polyfiller + networks: + - polyfiller + healthcheck: + test: ["CMD-SHELL", "curl -f http://localhost:3000/ || exit 1"] + interval: 3s + retries: 5 + start_period: 30s + labels: + - autoheal=true + restart: always + logging: + driver: json-file + options: + max-size: 10m + + autoheal: + image: willfarrell/autoheal:1.2.0 + volumes: + - /var/run/docker.sock:/var/run/docker.sock + restart: always + + caddy: + depends_on: + - api-service + image: caddy + ports: + - 80:80 + - 443:443 + networks: + - polyfiller + restart: always + command: caddy reverse-proxy --from polyfiller.app --to api-service:3000