Skip to content

Add a Dockerfile to allow running from a container #250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
79 changes: 79 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down