Sign inSign up

jmwilly/caddy-ratelimit

By jmwilly

•Updated over 1 year ago

Caddy with rate limit extension(arm64). See: https://github.com/mholt/caddy-ratelimit

Image
Security
Web servers
0

1.2K

jmwilly/caddy-ratelimit repository overview

⁠Run with default Caddyfile:

docker run --name caddy -p 80:80 -p 443:443 -p 2019:2019 jmwilly/caddy-ratelimit:latest # 2019 is caddy's admin

⁠Or provide custom Caddyfile:

docker run --name caddy -v /home/admin/Caddyfile:/etc/caddy/Caddyfile -p 80:80 -p 443:443 -p 2019:2019 jmwilly/caddy-ratelimit

⁠Manage:

docker restart caddy

docker stop caddy

⁠If you don't trust random docker images(you shouldn't) or you need it for another CPU architecture you can make your own, replace my username by yours when desired:

⁠Dockerfile:
# Build stage
FROM golang:alpine AS builder

# Install build dependencies: git for fetching modules, build-base for compilation
RUN apk add --no-cache git

# Install xcaddy: tool that builds Caddy with custom plugins
RUN go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest

# Set working directory for better organization
WORKDIR /src-build

# Build Caddy with rate limit module
RUN xcaddy build \
    --with github.com/mholt/caddy-ratelimit@latest \
    --output /usr/local/bin/caddy

# Final stage
FROM alpine:latest

# Install runtime dependencies
RUN apk add --no-cache ca-certificates mailcap

# Create Caddy user for privilege separation
RUN addgroup -S caddy
RUN adduser -S -G caddy caddy

# Copy compiled Caddy binary
COPY --from=builder /usr/local/bin/caddy /usr/local/bin/caddy

# Ownership and permissions
RUN chown caddy:caddy /usr/local/bin/caddy 
RUN chmod 755 /usr/local/bin/caddy

# Create standard directories
RUN mkdir -p /config /data /etc/caddy 
RUN chown -R caddy:caddy /config /data /etc/caddy

# Default Caddyfile for first-time users
COPY <<EOF /etc/caddy/Caddyfile
{
    auto_https off
    admin 0.0.0.0:2019
}
:80 {
    respond "Caddy rate-limited is runnig!"
}
EOF
RUN chown caddy:caddy /etc/caddy/Caddyfile

# Switch to non-root user for security
USER caddy

# Volumes for persistence and configuration
VOLUME ["/data", "/config"]

# Expose ports: 80 (HTTP), 443 (HTTPS), 2019 (Admin API)
EXPOSE 80 443 2019

# Healthcheck for monitoring in containerized environments
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s \
    CMD wget -qO- http://localhost:80 || exit 1

# Binary path and command
ENTRYPOINT ["/usr/local/bin/caddy"]
CMD ["run", "--config", "/etc/caddy/Caddyfile", "--adapter", "caddyfile"]

⁠Build

cd to Dockerfile, then:

docker build -t jmwilly/caddy-ratelimit:latest .

⁠Push

sudo docker push jmwilly/caddy-ratelimit

Tag summary

Content type

Image

Digest

sha256:99b67a094…

Size

29.2 MB

Last updated

over 1 year ago

docker pull jmwilly/caddy-ratelimit