Louis Lam 1 年之前
父节点
当前提交
bf2d4c95f9
共有 4 个文件被更改,包括 93 次插入2 次删除
  1. 10 0
      docker/BuildHealthCheck.Dockerfile
  2. 8 2
      docker/Dockerfile
  3. 74 0
      extra/healthcheck.go
  4. 1 0
      package.json

+ 10 - 0
docker/BuildHealthCheck.Dockerfile

@@ -0,0 +1,10 @@
+############################################
+# Build in Golang
+############################################
+FROM golang:1.21.4-bookworm
+WORKDIR /app
+ARG TARGETPLATFORM
+COPY ./extra/healthcheck.go ./extra/healthcheck.go
+
+# Compile healthcheck.go
+RUN go build -x -o ./extra/healthcheck ./extra/healthcheck.go

+ 8 - 2
docker/Dockerfile

@@ -1,3 +1,8 @@
+############################################
+# Healthcheck Binary
+############################################
+FROM louislam/dockge:build-healthcheck AS build_healthcheck
+
 ############################################
 # Build
 ############################################
@@ -12,16 +17,17 @@ RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-l
 ############################################
 FROM louislam/dockge:base AS release
 WORKDIR /app
-COPY --chown=node:node  . .
+COPY --chown=node:node --from=build_healthcheck /app/extra/healthcheck /app/extra/healthcheck
 COPY --from=build /app/node_modules /app/node_modules
+COPY --chown=node:node  . .
 RUN mkdir ./data
 
 VOLUME /app/data
 EXPOSE 5001
+HEALTHCHECK --interval=60s --timeout=30s --start-period=60s --retries=5 CMD extra/healthcheck
 ENTRYPOINT ["/usr/bin/dumb-init", "--"]
 CMD ["tsx", "./backend/index.ts"]
 
-
 ############################################
 # Mark as Nightly
 ############################################

+ 74 - 0
extra/healthcheck.go

@@ -0,0 +1,74 @@
+/*
+ * If changed, have to run `npm run build-docker-builder-go`.
+ * This script should be run after a period of time (180s), because the server may need some time to prepare.
+ */
+package main
+
+import (
+	"crypto/tls"
+	"io/ioutil"
+	"log"
+	"net/http"
+	"os"
+	"strings"
+	"time"
+)
+
+func main() {
+	// Is K8S + "dockge" as the container name
+	// See https://github.com/louislam/uptime-kuma/pull/2083
+	isK8s := strings.HasPrefix(os.Getenv("DOCKGE_PORT"), "tcp://")
+
+	// process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
+	http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{
+		InsecureSkipVerify: true,
+	}
+
+	client := http.Client{
+		Timeout: 28 * time.Second,
+	}
+
+	sslKey := os.Getenv("DOCKGE_SSL_KEY")
+	sslCert := os.Getenv("DOCKGE_SSL_CERT")
+
+	hostname := os.Getenv("DOCKGE_HOST")
+	if len(hostname) == 0 {
+		hostname = "127.0.0.1"
+	}
+
+	port := ""
+	// DOCKGE_PORT is override by K8S unexpectedly,
+	if !isK8s {
+		port = os.Getenv("DOCKGE_PORT")
+	}
+	if len(port) == 0 {
+		port = "5001"
+	}
+
+	protocol := ""
+	if len(sslKey) != 0 && len(sslCert) != 0 {
+		protocol = "https"
+	} else {
+		protocol = "http"
+	}
+
+	url := protocol + "://" + hostname + ":" + port
+
+	log.Println("Checking " + url)
+	resp, err := client.Get(url)
+
+	if err != nil {
+		log.Fatalln(err)
+	}
+
+	defer resp.Body.Close()
+
+	_, err = ioutil.ReadAll(resp.Body)
+
+	if err != nil {
+		log.Fatalln(err)
+	}
+
+	log.Printf("Health Check OK [Res Code: %d]\n", resp.StatusCode)
+
+}

+ 1 - 0
package.json

@@ -17,6 +17,7 @@
         "build:docker-base": "docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/dockge:base -f ./docker/Base.Dockerfile . --push",
         "build:docker": "node ./extra/env2arg.js docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/dockge:latest -t louislam/dockge:1 -t louislam/dockge:$VERSION --target release -f ./docker/Dockerfile . --push",
         "build:docker-nightly": "pnpm run build:frontend && docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/dockge:nightly --target nightly -f ./docker/Dockerfile . --push",
+        "build:healthcheck": "docker buildx build -f docker/BuildHealthCheck.Dockerfile --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/dockge:build-healthcheck . --push",
         "start-docker": "docker run --rm -p 5001:5001 --name dockge louislam/dockge:latest",
         "mark-as-nightly": "tsx ./extra/mark-as-nightly.ts",
         "reformat-changelog": "tsx ./extra/reformat-changelog.ts"