From a1645ca0f12f092c2b4e9e8b1cb67a6f11941b73 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Wed, 29 Nov 2023 18:03:47 +0800 Subject: [PATCH] WIP --- docker/BuildHealthCheck.Dockerfile | 10 ++++ docker/Dockerfile | 10 +++- extra/healthcheck.go | 74 ++++++++++++++++++++++++++++++ package.json | 1 + 4 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 docker/BuildHealthCheck.Dockerfile create mode 100644 extra/healthcheck.go diff --git a/docker/BuildHealthCheck.Dockerfile b/docker/BuildHealthCheck.Dockerfile new file mode 100644 index 0000000..764b9b6 --- /dev/null +++ b/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 diff --git a/docker/Dockerfile b/docker/Dockerfile index fa70174..ce83059 100644 --- a/docker/Dockerfile +++ b/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 ############################################ diff --git a/extra/healthcheck.go b/extra/healthcheck.go new file mode 100644 index 0000000..4e03fbf --- /dev/null +++ b/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) + +} diff --git a/package.json b/package.json index 96d76fb..d65a862 100644 --- a/package.json +++ b/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"