From 9686393777f3c89e6b116175034a26e0c67add60 Mon Sep 17 00:00:00 2001 From: Carl Kittelberger Date: Thu, 13 Apr 2017 05:59:17 +0200 Subject: [PATCH] Initial commit. --- .dockerignore | 8 +++ .editorconfig | 12 +++++ Dockerfile | 51 ++++++++++++++++++ files/bin/docker-caddy-build | 25 +++++++++ files/bin/docker-caddy-install-plugin | 60 ++++++++++++++++++++++ files/bin/docker-caddy-pack-source | 20 ++++++++ files/bin/docker-caddy-remove-git-metadata | 13 +++++ files/bin/docker-caddy-unpack-source | 13 +++++ 8 files changed, 202 insertions(+) create mode 100644 .dockerignore create mode 100644 .editorconfig create mode 100644 Dockerfile create mode 100755 files/bin/docker-caddy-build create mode 100755 files/bin/docker-caddy-install-plugin create mode 100755 files/bin/docker-caddy-pack-source create mode 100755 files/bin/docker-caddy-remove-git-metadata create mode 100755 files/bin/docker-caddy-unpack-source diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..36009fc --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +### + +.git* + +.dockerignore +Dockerfile + +.editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..4a7ea30 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..59929d1 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,51 @@ +FROM golang:1.8-alpine + +WORKDIR /data + +RUN apk add --no-cache \ + ca-certificates &&\ + update-ca-certificates + +# Install build dependencies (permanently since we are going to keep the +# ability to install additional plugins which will require a complete Caddy +# rebuild) +RUN apk add --no-cache --virtual build-deps \ + bash \ + gcc \ + git \ + xz + +# Preconfiguration +RUN \ + git config --global http.followRedirects true &&\ + mkdir -vp "${GOPATH}" + +# Add the scripts +COPY files/ /usr/local/ +# Fix permissions and line endings in case we use `docker build` from Windows +# because remember, Docker client can run on Windows despite the host running +# on Linux! +RUN \ + chmod -v +x /usr/local/bin/* &&\ + sed -i 's,\r,,g' /usr/local/bin/* + +# Install Caddy itself +ARG CADDY_VERSION=master +RUN \ + echo "*** Fetching Caddy..." &&\ + git clone --recursive "https://github.com/mholt/caddy.git" \ + "$GOPATH/src/github.com/mholt/caddy" &&\ + (cd "$GOPATH/src/github.com/mholt/caddy" &&\ + git checkout "$CADDY_VERSION") &&\ + docker-caddy-build &&\ + docker-caddy-pack-source + +# I would have run "setcap cap_net_bind_service=+ep /usr/local/bin/caddy" as well but it will not work in Docker +# thanks to inconsistencies with aufs and kernel settings on Ubuntu. +# +# My recommendation is to let Caddy bind to a non-root port instead and use the Docker userland proxy if possible, +# so for example binding Caddy to port 2200 and telling Docker to forward port 80 to port 2200 in the container +# should be just fine. + +ENTRYPOINT ["caddy"] +CMD ["-agree","-conf=/data/Caddyfile"] diff --git a/files/bin/docker-caddy-build b/files/bin/docker-caddy-build new file mode 100755 index 0000000..701bfd2 --- /dev/null +++ b/files/bin/docker-caddy-build @@ -0,0 +1,25 @@ +#!/bin/bash + +set -e + +export PATH="$GOPATH/bin:$PATH" + +docker-caddy-unpack-source + +cd "$GOPATH/src/github.com/mholt/caddy/caddy" + +# Make sure all dependencies are available +go get -d -v . + +docker-caddy-remove-git-metadata + +./build.bash "/usr/local/bin/caddy" + +# Remove some reproducible and for runtime unnecessary build output files +# to save on the image size later on +rm -rfv \ + "/tmp"/* \ + "/var/tmp"/* \ + "exec" \ + "lib" \ + "pkg" diff --git a/files/bin/docker-caddy-install-plugin b/files/bin/docker-caddy-install-plugin new file mode 100755 index 0000000..0a79d15 --- /dev/null +++ b/files/bin/docker-caddy-install-plugin @@ -0,0 +1,60 @@ +#!/bin/bash + +set -e + +pluginspath="$GOPATH/src/github.com/mholt/caddy/caddy/caddymain/install_plugins.go" + +plugin() { + echo "*** Fetching plugin $1..." + + # Generate Go import path from given Git URL + importpath="${3:-$(echo "$1" | sed -e 's,^.\+://,,' -e 's,\.git$,,')}" + git clone "$1" "$GOPATH/src/$importpath" + + ( + cd "$GOPATH/src/$importpath" + + # Checkout wanted version if any given + if [ ! -z "$2" ] + then + git checkout "$2" + fi + + echo "*** Preparing plugin $1..." + + # Fetch dependencies + go get -d -v . + + # Run generate across all files (if any extra tools are necessary you will + # need to install them before running this script) + go generate -v . + ) + + docker-caddy-remove-git-metadata + + # Add the newly available plugin to an extra Go source file that will be + # compiled into the Caddy binary along with the rest of the source tree. + if [ ! -e "$pluginspath" ] + then + echo "package caddymain" > "$pluginspath" + fi + echo "*** Adding ${importpath} to $pluginspath..." + echo "import _ \"${importpath}\"" >> "$pluginspath" +} + +docker-caddy-unpack-source + +# Parse arguments and prepare each given plugin. +# Supported syntaxes (where reference can be a commit hash or branch name etc.): +# - http://server.com/user/repository.git +# - https://server.com/user/repository.git +# - git://server.com/user/repository.git +# - http://server.com/user/repository.git#reference +# - https://server.com/user/repository.git#reference +# - git://server.com/user/repository.git#reference +for plugin in "$@"; do + (IFS='#'; plugin $plugin); +done + +# Rebuild Caddy with the given plugins now installed +exec docker-caddy-build diff --git a/files/bin/docker-caddy-pack-source b/files/bin/docker-caddy-pack-source new file mode 100755 index 0000000..4a7b44e --- /dev/null +++ b/files/bin/docker-caddy-pack-source @@ -0,0 +1,20 @@ +#!/bin/bash + +set -e + +if [ -n "${DISABLE_SOURCE_COMPRESSION}" ]; then + exit 0 +fi + +if [ ! -d "${GOPATH}/src/github.com/mholt/caddy" ]; then + echo "Tried to pack source when the source directory has been deleted." >&2 + exit 1 +fi + +( + cd "${GOPATH}" + mkdir -p "/usr/src" + tar cJ -f "/usr/src/go.tar.xz" . +) + +exec rm -r "${GOPATH}" diff --git a/files/bin/docker-caddy-remove-git-metadata b/files/bin/docker-caddy-remove-git-metadata new file mode 100755 index 0000000..0ba84a8 --- /dev/null +++ b/files/bin/docker-caddy-remove-git-metadata @@ -0,0 +1,13 @@ +#!/bin/bash + +set -e + +if [ -n "${DISABLE_GIT_METADATA_REMOVAL}" ]; then + exit 0 +fi + +# Remove Git metadata +exec find "${GOPATH}" \ + -name .git -type d \ + ! -path '*/src/github.com/mholt/caddy/*' \ + -exec rm -r {} + diff --git a/files/bin/docker-caddy-unpack-source b/files/bin/docker-caddy-unpack-source new file mode 100755 index 0000000..29cc051 --- /dev/null +++ b/files/bin/docker-caddy-unpack-source @@ -0,0 +1,13 @@ +#!/bin/bash + +set -e + +if [ -n "${DISABLE_SOURCE_COMPRESSION}" ]; then + exit 0 +fi + +if [ ! -d "${GOPATH}/src/github.com/mholt/caddy" ]; then + mkdir -p "${GOPATH}" + tar xJ -C "${GOPATH}" -f "/usr/src/go.tar.xz" + rm "/usr/src/go.tar.xz" +fi