Building Caddy Docker Images on a Raspberry Pi
The base Caddy Docker image is intentionally lean—around 50MB—to avoid bundling unnecessary components. However, if you need extra functionality, such as automatic SSL certificate issuance using the ACME DNS challenge via Cloudflare and Let’s Encrypt (as I did), you’ll need an image that includes the required plug-in. You have two options: use a community-maintained prebuilt image (the easy way), or build your own (the hard way). Here, we’re going with the hard way.
Step 1 - Install Go#
Pi My Life Up has an excellent tutorial on how to do that on a Raspberry Pi.
Step 2 - Build xcaddy#
Follow the Install steps in the xcaddy GitHub repo.
Step 3 - Compile the Caddy Binary#
-
Create a new working directory
mkdir go cd go -
Build the Caddy binary. Replace the “cloudflare” part with whichever plug-in you’re trying to use.
xcaddy build --with github.com/caddy-dns/cloudflare
Step 4 - Create the Docker Image#
- Create the dockerfile.
nano dockerfile - Paste the following contents
# Dockerfile
FROM alpine:latest
# Install dependencies
RUN apk add --no-cache ca-certificates
# Copy your custom Caddy binary
COPY caddy /usr/bin/caddy
RUN chmod +x /usr/bin/caddy
# Create config and data directories
RUN mkdir -p /etc/caddy /config /data
# Expose ports
EXPOSE 80 443
# Run Caddy
ENTRYPOINT ["/usr/bin/caddy"]
CMD ["run", "--config", "/etc/caddy/Caddyfile", "--adapter", "caddyfile"]
- Do the nano “save file” dance: press CTRL+x, press y, press ENTER.
- Build the Docker image. Don’t forget the “.” at the end of the command. It’s easy to miss if you’re new to this stuff. The image here is called “my-caddy-cloudflare” but feel free to call the image whatever you want.
docker build -t my-caddy-cloudflare .- Reference the new image name in your “docker compose” or “docker run” command. Here’s an example:
services:
caddy:
image: my-caddy-cloudflare
container_name: caddy
restart: unless-stopped
ports:
- "80:80"
- "443:443"
- "443:443/udp"
volumes:
- ./conf:/etc/caddy
- ./site:/srv
- caddy_data:/data
- caddy_config:/config
- /var/run/tailscale/tailscaled.sock:/var/run/tailscale/tailscaled.sock
volumes:
caddy_data:
caddy_config:
Read other posts