From c7e557d35fbddc4d2253973fe49ecc7a84c8aa8c Mon Sep 17 00:00:00 2001 From: Edward Welch Date: Thu, 1 May 2025 22:37:24 +0000 Subject: [PATCH 1/2] Add a Dockerfile, update Go --- Dockerfile | 48 +++++++++++++++++++++++++++++++++ README.md | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 2 +- 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e754b1e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,48 @@ +# syntax=docker/dockerfile:1 + +# Build stage +FROM --platform=$BUILDPLATFORM golang:1.24.2-alpine AS builder + +WORKDIR /app + +# Install build dependencies +RUN apk add --no-cache git + +# Copy go mod files +COPY go.mod go.sum ./ +RUN go mod download + +# Copy source code +COPY . . + +# Build the application +ARG TARGETPLATFORM +ARG BUILDPLATFORM +RUN GOOS=$(echo $TARGETPLATFORM | cut -d/ -f1) \ + GOARCH=$(echo $TARGETPLATFORM | cut -d/ -f2) \ + go build -o speedtest-go + +# Final stage +FROM alpine:latest + +# Install bash +RUN apk add --no-cache bash + +# Create non-root user +RUN adduser -D -h /home/speedtest speedtest + +WORKDIR /home/speedtest + +# Copy the binary from builder +COPY --from=builder /app/speedtest-go /usr/local/bin/ + +# Switch to non-root user +USER speedtest + +# Set default shell +SHELL ["/bin/bash", "-c"] + +# Set the entrypoint to bash, we do this rather than using the speedtest command directly +# such that you can also use this container in an interactive way to run speedtests. +# see the README for more info and examples. +CMD ["/bin/bash"] \ No newline at end of file diff --git a/README.md b/README.md index eb36ee2..42cb6b2 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,85 @@ $ nix-shell -p speedtest-go Please download the compatible package from [Releases](https://github.com/showwin/speedtest-go/releases). If there are no compatible packages you want, please let me know on [Issue Tracker](https://github.com/showwin/speedtest-go/issues). +#### Docker Build + +To build a multi-architecture Docker image: + +```bash +# Check if you already have a builder instance +docker buildx ls + +# Only create a new builder if you don't have one +# If the above command shows no builders or none are in use, run: +docker buildx create --name mybuilder --use + +# Build and push for multiple platforms +docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t yourusername/speedtest-go:latest --push . +``` + +#### Running the Container + +##### Docker +Run the container with default settings (interactive shell): +```bash +docker run -it yourusername/speedtest-go:latest +``` + +Run a speedtest with specific arguments: +```bash +# Run a basic speedtest +docker run yourusername/speedtest-go:latest speedtest-go + +# Run with specific server +docker run yourusername/speedtest-go:latest speedtest-go --server 6691 + +# Run with multiple servers and JSON output +docker run yourusername/speedtest-go:latest speedtest-go --server 6691 --server 6087 --json + +# Run with custom location +docker run yourusername/speedtest-go:latest speedtest-go --location=60,-110 +``` + +##### Kubernetes +Here's an example Kubernetes pod specification that runs a speedtest: + +```yaml +apiVersion: v1 +kind: Pod +metadata: + name: speedtest +spec: + containers: + - name: speedtest + image: yourusername/speedtest-go:latest + # Base command to run bash + command: ["speedtest-go"] + # Or run with specific arguments + # args: ["--server", "6691", "--json"] + restartPolicy: Never +``` + +For a more complete deployment, you might want to use a CronJob to run periodic speedtests: + +```yaml +apiVersion: batch/v1 +kind: CronJob +metadata: + name: speedtest +spec: + schedule: "0 */6 * * *" # Run every 6 hours + jobTemplate: + spec: + template: + spec: + containers: + - name: speedtest + image: yourusername/speedtest-go:latest + command: ["speedtest-go"] + args: ["--json"] + restartPolicy: OnFailure +``` + ### Usage ```bash diff --git a/go.mod b/go.mod index 6b9548e..5dd5f92 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/showwin/speedtest-go -go 1.19 +go 1.24.2 require ( github.com/chelnak/ysmrr v0.5.0 From 376720e036b21247bb46be9a4c2211cb32a29dad Mon Sep 17 00:00:00 2001 From: Edward Welch Date: Fri, 2 May 2025 16:16:19 +0000 Subject: [PATCH 2/2] don't incrase the required go version --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 5dd5f92..6b9548e 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/showwin/speedtest-go -go 1.24.2 +go 1.19 require ( github.com/chelnak/ysmrr v0.5.0