1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2025-12-17 17:47:03 +00:00

Docker best practices

- Install and remove OS and Go build deps in a single layer
- Use COPY instead of ADD
- WORKDIR now resolves ENV variables, use one
- Use VOLUMES for configuration and datastore
- Added launcher script start-inbucket.sh
- Made docker-run.sh more powerful
This commit is contained in:
James Hillyerd
2016-02-22 22:39:14 -08:00
parent 9fafbf73d0
commit f996fa2ae7
6 changed files with 116 additions and 17 deletions

View File

@@ -4,19 +4,22 @@
FROM golang:1.6-alpine
MAINTAINER James Hillyerd, @jameshillyerd
# Dependencies
RUN apk update && apk add git
# Configuration (WORKDIR doesn't support env vars)
ENV INBUCKET_SRC $GOPATH/src/github.com/jhillyerd/inbucket
ENV INBUCKET_HOME /opt/inbucket
WORKDIR /opt/inbucket
ENTRYPOINT ["bin/inbucket"]
CMD ["/etc/opt/inbucket.conf"]
WORKDIR $INBUCKET_HOME
ENTRYPOINT ["/con/context/start-inbucket.sh"]
CMD ["/con/configuration/inbucket.conf"]
# Ports: SMTP, HTTP, POP3
EXPOSE 10025 10080 10110
# Persistent Volumes, following convention at:
# https://github.com/docker/docker/issues/9277
# NOTE /con/context is also used, not exposed by default
VOLUME /con/configuration
VOLUME /con/data
# Build Inbucket
ADD . $INBUCKET_SRC/
COPY . $INBUCKET_SRC/
RUN "$INBUCKET_SRC/etc/docker/install.sh"

View File

@@ -86,13 +86,13 @@ public.dir=%(install.dir)s/themes/%(theme)s/public
# Path to the greeting HTML displayed on front page, can
# be moved out of installation dir for customization
greeting.file=/etc/opt/inbucket-greeting.html
greeting.file=/con/configuration/greeting.html
#############################################################################
[datastore]
# Path to the datastore, mail will be written into subdirectories
path=/var/opt/inbucket
path=/con/data
# How many minutes after receipt should a message be stored until it's
# automatically purged. To retain messages until manually deleted, set this

View File

@@ -0,0 +1,24 @@
#!/bin/sh
# start-inbucket.sh
# description: start inbucket (runs within a docker container)
CONF_SOURCE="$INBUCKET_HOME/defaults"
CONF_TARGET="/con/configuration"
set -eo pipefail
install_default_config() {
local file="$1"
local source="$CONF_SOURCE/$file"
local target="$CONF_TARGET/$file"
if [ ! -e "$target" ]; then
echo "Installing default $file to $CONF_TARGET"
install "$source" "$target"
fi
}
install_default_config "inbucket.conf"
install_default_config "greeting.html"
exec "$INBUCKET_HOME/bin/inbucket" $*

View File

@@ -2,4 +2,61 @@
# docker-run.sh
# description: Launch Inbucket's docker image
docker run -p 9000:10080 -p 2500:10025 -p 1100:10110 jhillyerd/inbucket
# Docker Image Tag
IMAGE="jhillyerd/inbucket"
# Ports exposed on host:
PORT_HTTP=9000
PORT_SMTP=2500
PORT_POP3=1100
# Volumes exposed on host:
VOL_CONFIG="/tmp/inbucket/config"
VOL_DATA="/tmp/inbucket/data"
set -eo pipefail
main() {
local run_opts=""
for arg in $*; do
case "$arg" in
-h)
usage
exit
;;
-r)
reset
;;
-d)
run_opts="$run_opts -d"
;;
*)
usage
exit 1
;;
esac
done
docker run $run_opts \
-p $PORT_HTTP:10080 \
-p $PORT_SMTP:10025 \
-p $PORT_POP3:10110 \
-v "$VOL_CONFIG:/con/configuration" \
-v "$VOL_DATA:/con/data" \
"$IMAGE"
}
usage() {
echo "$0 [options]" 2>&1
echo " -d detach - detach and print container ID" 2>&1
echo " -r reset - purge config and data before startup" 2>&1
echo " -h help - print this message" 2>&1
}
reset() {
/bin/rm -rf "$VOL_CONFIG"
/bin/rm -rf "$VOL_DATA"
}
main $*

View File

@@ -4,9 +4,14 @@
set -eo pipefail
installdir="${INBUCKET_HOME}"
srcdir="${INBUCKET_SRC}"
installdir="$INBUCKET_HOME"
srcdir="$INBUCKET_SRC"
bindir="$installdir/bin"
defaultsdir="$installdir/defaults"
contextdir="/con/context"
echo "### Installing OS Build Dependencies"
apk add --no-cache --virtual .build-deps git
# Setup
export GOBIN="$bindir"
@@ -16,7 +21,7 @@ go clean
# Build
echo "### Fetching Dependencies"
go get -d -t -v ./...
go get -t -v ./...
echo "### Testing Inbucket"
go test ./...
@@ -25,9 +30,19 @@ echo "### Building Inbucket"
go build -o inbucket -ldflags "-X 'main.BUILDDATE=$builddate'" -v .
echo "### Installing Inbucket"
set -x
mkdir -p "$bindir"
mkdir -p "/etc/opt"
mv inbucket "$bindir"
install etc/docker/inbucket.conf /etc/opt/inbucket.conf
install etc/docker/greeting.html /etc/opt/inbucket-greeting.html
install inbucket "$bindir"
mkdir -p "$contextdir"
install etc/docker/defaults/start-inbucket.sh "$contextdir"
cp -r themes "$installdir/"
mkdir -p "$defaultsdir"
cp etc/docker/defaults/inbucket.conf "$defaultsdir"
cp etc/docker/defaults/greeting.html "$defaultsdir"
set +x
echo "### Removing OS Build Dependencies"
apk del .build-deps
echo "### Removing $GOPATH"
rm -rf "$GOPATH"