From 8e31fe25acc05e2fdc2a2384898b66f6ca1f1291 Mon Sep 17 00:00:00 2001 From: minus Date: Tue, 26 Jul 2022 15:30:23 +0000 Subject: [PATCH] Make Docker image easier to use (#50) * Use single server example as default frontend This just works out of the box * Refactor Dockerfile - Remove the asset dir since it's embedded in the executable anyway - Use the code in the current directory to build instead of downloading. Makes local testing very easy. - Cache Go dependencies - Run without root privileges. Note that using a BoltDB database requires mounting a volume with the correct permissions. The default database has thus been changed to memory so that starting the image just works. * Automatically publish images --- .github/workflows/docker-publish.yml | 56 ++++ Dockerfile | 26 +- settings.toml | 4 +- web/assets/example-singleServer-full.html | 365 ---------------------- web/assets/index.html | 137 +------- 5 files changed, 76 insertions(+), 512 deletions(-) create mode 100644 .github/workflows/docker-publish.yml delete mode 100755 web/assets/example-singleServer-full.html diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml new file mode 100644 index 0000000..0b7ca69 --- /dev/null +++ b/.github/workflows/docker-publish.yml @@ -0,0 +1,56 @@ +name: Docker +on: + push: + branches: [ "master" ] + # Publish semver tags as releases. + tags: [ 'v*.*.*' ] + pull_request: + branches: [ "master" ] + +env: + # Use docker.io for Docker Hub if empty + REGISTRY: ghcr.io + # github.repository as / + IMAGE_NAME: ${{ github.repository }} + + +jobs: + build: + + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + # Login against a Docker registry except on PR + # https://github.com/docker/login-action + - name: Log into registry ${{ env.REGISTRY }} + if: github.event_name != 'pull_request' + uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + # Extract metadata (tags, labels) for Docker + # https://github.com/docker/metadata-action + - name: Extract Docker metadata + id: meta + uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + # Build and push Docker image with Buildx (don't push on PR) + # https://github.com/docker/build-push-action + - name: Build and push Docker image + id: build-and-push + uses: docker/build-push-action@ac9327eae2b366085ac7f6a2d02df8aa8ead720a + with: + context: . + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/Dockerfile b/Dockerfile index 500f58c..5270193 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,20 +1,18 @@ -FROM golang:alpine AS build_base -#ENV GOARCH arm64 -#ENV GOARCH amd64 -RUN apk add --no-cache git gcc ca-certificates libc-dev \ -&& mkdir -p /go/src/github.com/librespeed/ \ -&& cd /go/src/github.com/librespeed/ \ -&& git clone https://github.com/librespeed/speedtest-go.git -WORKDIR /go/src/github.com/librespeed/speedtest-go -RUN go get ./ && go build -ldflags "-w -s" -trimpath -o speedtest main.go +FROM golang:1.18-alpine AS build_base +RUN apk add --no-cache git gcc ca-certificates libc-dev +WORKDIR /build +COPY go.mod go.sum ./ +RUN go mod download +COPY ./ ./ +RUN go build -ldflags "-w -s" -trimpath -o speedtest . -FROM alpine:3.15 -RUN apk add ca-certificates +FROM alpine:3.16 +RUN apk add --no-cache ca-certificates WORKDIR /app -COPY --from=build_base /go/src/github.com/librespeed/speedtest-go/speedtest . -COPY --from=build_base /go/src/github.com/librespeed/speedtest-go/web/assets ./assets -COPY --from=build_base /go/src/github.com/librespeed/speedtest-go/settings.toml . +COPY --from=build_base /build/speedtest ./ +COPY settings.toml ./ +USER nobody EXPOSE 8989 CMD ["./speedtest"] diff --git a/settings.toml b/settings.toml index bdfe9fb..0f0de1f 100644 --- a/settings.toml +++ b/settings.toml @@ -22,7 +22,7 @@ redact_ip_addresses=false # database type for statistics data, currently supports: none, memory, bolt, mysql, postgresql # if none is specified, no telemetry/stats will be recorded, and no result PNG will be generated -database_type="bolt" +database_type="memory" database_hostname="" database_name="" database_username="" @@ -37,4 +37,4 @@ enable_http2=false # if you use HTTP/2 or TLS, you need to prepare certificates and private keys # tls_cert_file="cert.pem" -# tls_key_file="privkey.pem" \ No newline at end of file +# tls_key_file="privkey.pem" diff --git a/web/assets/example-singleServer-full.html b/web/assets/example-singleServer-full.html deleted file mode 100755 index 823ef9f..0000000 --- a/web/assets/example-singleServer-full.html +++ /dev/null @@ -1,365 +0,0 @@ - - - - - - - - - -LibreSpeed Example - - -

LibreSpeed Example

-
-

- Privacy -
-
-
-
Ping
-
-
ms
-
-
-
Jitter
-
-
ms
-
-
-
-
-
Download
- -
-
Mbps
-
-
-
Upload
- -
-
Mbps
-
-
-
- -
- -
- Source code -
- - - - diff --git a/web/assets/index.html b/web/assets/index.html index 125edf5..823ef9f 100755 --- a/web/assets/index.html +++ b/web/assets/index.html @@ -1,81 +1,16 @@ - +