From 13d2a17df836c76b2cd46fc8cdd8c25b178d55c8 Mon Sep 17 00:00:00 2001 From: Teddysun Date: Sun, 20 Sep 2020 14:18:27 +0900 Subject: [PATCH] Added Caddy v1.0.5 Docker Image Signed-off-by: Teddysun --- docker/caddy/Caddyfile | 25 ++ docker/caddy/Dockerfile | 39 +++ docker/caddy/Dockerfile.architecture | 40 +++ docker/caddy/README.md | 44 ++++ docker/caddy/build_caddy_forwardproxy_v1.sh | 46 ++++ docker/caddy/caddy.sh | 54 ++++ docker/caddy/index.html | 269 ++++++++++++++++++++ 7 files changed, 517 insertions(+) create mode 100644 docker/caddy/Caddyfile create mode 100644 docker/caddy/Dockerfile create mode 100644 docker/caddy/Dockerfile.architecture create mode 100644 docker/caddy/README.md create mode 100644 docker/caddy/build_caddy_forwardproxy_v1.sh create mode 100644 docker/caddy/caddy.sh create mode 100644 docker/caddy/index.html diff --git a/docker/caddy/Caddyfile b/docker/caddy/Caddyfile new file mode 100644 index 0000000..ad36e18 --- /dev/null +++ b/docker/caddy/Caddyfile @@ -0,0 +1,25 @@ +# The Caddyfile is an easy way to configure your Caddy web server. +# +# Unless the file starts with a global options block, the first +# uncommented line is always the address of your site. +# +# To use your own domain name (with automatic HTTPS), first make +# sure your domain's A/AAAA DNS records are properly pointed to +# this machine's public IP, then replace the line below with your +# domain name. +:80 + +# Set this path to your site's directory. +root * /usr/share/caddy + +# Enable the static file server. +file_server + +# Another common task is to set up a reverse proxy: +# reverse_proxy localhost:8080 + +# Or serve a PHP site through php-fpm: +# php_fastcgi localhost:9000 + +# Refer to the Caddy docs for more information: +# https://caddyserver.com/docs/caddyfile diff --git a/docker/caddy/Dockerfile b/docker/caddy/Dockerfile new file mode 100644 index 0000000..cb640cb --- /dev/null +++ b/docker/caddy/Dockerfile @@ -0,0 +1,39 @@ +# Dockerfile for caddy v1.0.5 based alpine +# Copyright (C) 2020 Teddysun +# Reference URL: +# https://github.com/caddyserver/caddy +# https://github.com/caddyserver/forwardproxy + +FROM alpine:3.12 +LABEL maintainer="Teddysun " + +WORKDIR /root +COPY caddy.sh /root/caddy.sh +RUN set -ex \ + && mkdir -p /config/caddy /data/caddy /etc/caddy /usr/share/caddy \ + && apk add --no-cache tzdata ca-certificates mailcap \ + && chmod +x /root/caddy.sh \ + && /root/caddy.sh \ + && rm -fv /root/caddy.sh + +# set up nsswitch.conf for Go's "netgo" implementation +# see: https://github.com/docker-library/golang/blob/1eb096131592bcbc90aa3b97471811c798a93573/1.14/alpine3.12/Dockerfile#L9 +RUN [ ! -e /etc/nsswitch.conf ] && echo 'hosts: files dns' > /etc/nsswitch.conf + +COPY Caddyfile /etc/caddy/Caddyfile +COPY index.html /usr/share/caddy/index.html + +# See https://caddyserver.com/docs/conventions#file-locations for details +ENV XDG_CONFIG_HOME /config +ENV XDG_DATA_HOME /data + +VOLUME /etc/caddy +VOLUME /config +VOLUME /data + +EXPOSE 80 +EXPOSE 443 +EXPOSE 2015 + +ENV TZ=Asia/Shanghai +CMD [ "/usr/bin/caddy", "--config", "/etc/caddy/Caddyfile" ] diff --git a/docker/caddy/Dockerfile.architecture b/docker/caddy/Dockerfile.architecture new file mode 100644 index 0000000..c0683ce --- /dev/null +++ b/docker/caddy/Dockerfile.architecture @@ -0,0 +1,40 @@ +# Dockerfile for caddy v1.0.5 based alpine +# Copyright (C) 2020 Teddysun +# Reference URL: +# https://github.com/caddyserver/caddy +# https://github.com/caddyserver/forwardproxy + +FROM --platform=${TARGETPLATFORM} alpine:3.12 +LABEL maintainer="Teddysun " + +ARG TARGETPLATFORM +WORKDIR /root +COPY caddy.sh /root/caddy.sh +RUN set -ex \ + && mkdir -p /config/caddy /data/caddy /etc/caddy /usr/share/caddy \ + && apk add --no-cache tzdata ca-certificates mailcap \ + && chmod +x /root/caddy.sh \ + && /root/caddy.sh "${TARGETPLATFORM}" \ + && rm -fv /root/caddy.sh + +# set up nsswitch.conf for Go's "netgo" implementation +# see: https://github.com/docker-library/golang/blob/1eb096131592bcbc90aa3b97471811c798a93573/1.14/alpine3.12/Dockerfile#L9 +RUN [ ! -e /etc/nsswitch.conf ] && echo 'hosts: files dns' > /etc/nsswitch.conf + +COPY Caddyfile /etc/caddy/Caddyfile +COPY index.html /usr/share/caddy/index.html + +# See https://caddyserver.com/docs/conventions#file-locations for details +ENV XDG_CONFIG_HOME /config +ENV XDG_DATA_HOME /data + +VOLUME /etc/caddy +VOLUME /config +VOLUME /data + +EXPOSE 80 +EXPOSE 443 +EXPOSE 2015 + +ENV TZ=Asia/Shanghai +CMD [ "/usr/bin/caddy", "--config", "/etc/caddy/Caddyfile" ] diff --git a/docker/caddy/README.md b/docker/caddy/README.md new file mode 100644 index 0000000..f5f3d93 --- /dev/null +++ b/docker/caddy/README.md @@ -0,0 +1,44 @@ +## Caddy Docker Image by Teddysun + +[Caddy][1] is a powerful, enterprise-ready, open source web server with automatic HTTPS written in Go. + +Docker images are built for quick deployment in various computing cloud providers. + +For more information on docker and containerization technologies, refer to [official document][2]. + +## Prepare the host + +If you need to install docker by yourself, follow the [official installation guide][3]. + +## Pull the image + +```bash +$ docker pull teddysun/caddy +``` + +This pulls the version **v1.0.5** of Caddy. + +It can be found at [Docker Hub][4]. + +## Start a container + +You **must create a configuration file** `/etc/caddy/Caddyfile` in host at first: + +``` +$ mkdir -p /etc/caddy +``` + +A sample `Caddyfile` please visit [here](https://github.com/caddyserver/dist/blob/master/config/Caddyfile) + +Caddy requires write access to two locations: a [data directory](https://caddyserver.com/docs/conventions#data-directory), and a [configuration directory](https://caddyserver.com/docs/conventions#configuration-directory). + +There is an example to override the default `Caddyfile`, you can mount a new one at `/etc/caddy/Caddyfile` like below: + +```bash +$ docker run -d -p 80:80 --name caddy --restart=always -v /etc/caddy:/etc/caddy teddysun/caddy +``` + +[1]: https://caddyserver.com/ +[2]: https://docs.docker.com/ +[3]: https://docs.docker.com/install/ +[4]: https://hub.docker.com/r/teddysun/caddy/ \ No newline at end of file diff --git a/docker/caddy/build_caddy_forwardproxy_v1.sh b/docker/caddy/build_caddy_forwardproxy_v1.sh new file mode 100644 index 0000000..f5dfa48 --- /dev/null +++ b/docker/caddy/build_caddy_forwardproxy_v1.sh @@ -0,0 +1,46 @@ +#!/bin/sh +# +# This is a Shell script for build multi-architectures caddy binary file with forwardproxy plugin +# +# Supported architectures: amd64, arm32v6, arm32v7, arm64v8, i386, ppc64le, s390x +# +# Copyright (C) 2020 Teddysun +# +# Reference URL: +# https://github.com/caddyserver/caddy +# https://github.com/caddyserver/forwardproxy + +cur_dir="$(pwd)" + +COMMANDS=( git go ) +for CMD in "${COMMANDS[@]}"; do + if [ ! "$(command -v "${CMD}")" ]; then + echo "${CMD} is not installed, please install it and try again" && exit 1 + fi +done + +cd ${cur_dir} +echo "git clone https://github.com/caddyserver/forwardproxy.git" +git clone https://github.com/caddyserver/forwardproxy.git +cd forwardproxy/cmd/caddy || exit 2 +go get -d -v + +LDFLAGS="-s -w" +ARCHS=( 386 amd64 arm arm64 ppc64le s390x ) +ARMS=( 6 7 ) + +for ARCH in ${ARCHS[@]}; do + if [ "${ARCH}" = "arm" ]; then + for V in ${ARMS[@]}; do + echo "Building caddy_linux_${ARCH}${V}" + env CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} GOARM=${V} go build -v -ldflags "${LDFLAGS}" -o ${cur_dir}/caddy_linux_${ARCH}${V} + done + else + echo "Building caddy_linux_${ARCH}" + env CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} go build -v -ldflags "${LDFLAGS}" -o ${cur_dir}/caddy_linux_${ARCH} + fi +done + +chmod +x ${cur_dir}/caddy_* +# clean up +cd ${cur_dir} && rm -fr forwardproxy diff --git a/docker/caddy/caddy.sh b/docker/caddy/caddy.sh new file mode 100644 index 0000000..d7383ca --- /dev/null +++ b/docker/caddy/caddy.sh @@ -0,0 +1,54 @@ +#!/bin/sh +# +# This is a Shell script for caddy based alpine with Docker image +# +# Copyright (C) 2019 - 2020 Teddysun +# +# Reference URL: +# https://github.com/caddyserver/caddy +# https://github.com/caddyserver/forwardproxy + +PLATFORM=$1 +if [ -z "$PLATFORM" ]; then + ARCH="amd64" +else + case "$PLATFORM" in + linux/386) + ARCH="386" + ;; + linux/amd64) + ARCH="amd64" + ;; + linux/arm/v6) + ARCH="arm6" + ;; + linux/arm/v7) + ARCH="arm7" + ;; + linux/arm64|linux/arm64/v8) + ARCH="arm64" + ;; + linux/ppc64le) + ARCH="ppc64le" + ;; + linux/s390x) + ARCH="s390x" + ;; + *) + ARCH="" + ;; + esac +fi +[ -z "${ARCH}" ] && echo "Error: Not supported OS Architecture" && exit 1 +# Download binary file +CADDY_FILE="caddy_linux_${ARCH}" + +echo "Downloading binary file: ${CADDY_FILE}" +wget -O /usr/bin/caddy https://dl.lamp.sh/files/${CADDY_FILE} > /dev/null 2>&1 +if [ $? -ne 0 ]; then + echo "Error: Failed to download binary file: ${CADDY_FILE}" && exit 1 +fi +echo "Download binary file: ${CADDY_FILE} completed" + +chmod +x /usr/bin/caddy +/usr/bin/caddy version diff --git a/docker/caddy/index.html b/docker/caddy/index.html new file mode 100644 index 0000000..eeabaa3 --- /dev/null +++ b/docker/caddy/index.html @@ -0,0 +1,269 @@ + + + + Caddy works! + + + + + + +
+
+
+
+ + +

+ Congratulations! + おめでとう! + Felicidades! + 恭喜! + बधाई हो! + Поздравляю!
 + 🎊 +

+ +

+ Your web server is working. Now make it work for you. 💪 +

+

+ Caddy is ready to serve your site over HTTPS: +

+
    +
  1. Point your domain's A/AAAA DNS records at this machine.
  2. +
  3. Upload your site's files to /var/www/html. +
  4. + Edit your Caddyfile at /etc/caddy/Caddyfile: +
      +
    1. Replace :80 with your domain name
    2. +
    3. Change the site root to /var/www/html
    4. +
    +
  5. +
  6. Reload the configuration: systemctl reload caddy
  7. +
  8. Visit your site!
  9. +
+

If that worked 🥳

+

+ Awesome! You won't have to look at this slanted page anymore. +

+

+ Remember, Caddy can do a lot more than serve static files. It's also a powerful reverse proxy and application platform. You can use the Caddyfile to enable any other features you need. Or you could use Caddy's API to configure it programmatically. +

+

+ Everything you need to know is either in the 📖 Caddy documentation or the manual for your OS/platform. Have fun! +

+ +

If that didn't work 😶

+

+ It's okay, you can fix it! First check the following things: +

+
    +
  • Service status: systemctl status caddy
  • +
  • Logs: journalctl --no-pager -u caddy
  • +
  • Are your site's files readable by the caddy user and group? ls -la /var/www/html
  • +
  • Is the caddy home directory writeable? ls -la /var/lib/caddy
  • +
  • Ensure your domain's A and/or AAAA records point to your machine's public IP address: dig example.com
  • +
  • Are your ports 80 and 443 externally reachable, and is Caddy able to bind to them? Check your firewalls, port forwarding, and other network configuration.
  • +
+

+ WAIT! Before trying again, switch to Let's Encrypt's staging environment to avoid being accidentally rate limited. Once you get everything else working, it's safe to switch back. +

+

+ Depending on your DNS provider, it may take a while for the DNS records to propagate. Even when everything is configured properly, automated challenges to obtain TLS certificates usually take several seconds, but may take up to several minutes or hours. +

+ If you still need help, we have a great community! First try a search, and if your question is original, go ahead and ask it! Remember to pay it forward and help others too. 😁 +

+

+ Visit Caddy on: + GitHub + or + Twitter + or + Our Forum +

+
+
+ + + + + \ No newline at end of file