From b0e3c474616cb25b053105e21342f2f4613123a2 Mon Sep 17 00:00:00 2001 From: Exadra37 Date: Sun, 21 Jan 2018 00:53:34 +0000 Subject: [PATCH] Added docker compose file customizable from a .env file. Signed-off-by: Exadra37 --- .env-example | 58 ++++++++++++++++++++++++++++++++ .gitignore | 1 + Dockerfile | 27 ++++++++++----- README.md | 82 +++++++++++++++++++++++++++------------------- docker-compose.yml | 26 +++++++++++++++ 5 files changed, 151 insertions(+), 43 deletions(-) create mode 100644 .env-example create mode 100644 .gitignore create mode 100644 docker-compose.yml diff --git a/.env-example b/.env-example new file mode 100644 index 0000000..8692653 --- /dev/null +++ b/.env-example @@ -0,0 +1,58 @@ +# +# DOCKER COMPOSER CUSTOMIZATION +# + +# Exim version needs to be the correct one for the required Alpine version +ALPINE_VERSION=3.7 +EXIM_VERSION=4.89.1-r0 + +# Useful to build the docker image from other path +EXIM_IMAGE_BUILD_CONTEXT=. + +# Builds a local docker image with the whatsoever name we want +# The EXIM_VERSION will be auto appended to the name: +# bambucha/exim-relay-local:4.89.1-r0 +EXIM_IMAGE=bambucha/exim-relay-local + +# Default port map is 25:25 but we can override it at any time as suit us +EXIM_PORT_MAP=2525:25 + +# By default a native docker volume is used with name smpt-dkim mapping to +# container dir /dkim. +# +# Docker volumes are suitable for heavy workloads, thus a normal host volume is +# the preferred approach for that scenarios. +# +# To use a host volume like /home/username/.exim/storage/dkim we need to create +# it with: +# $ mkdir -p /home/username/.exim/storage/dkim +# $ sudo chown 100:101 /home/username/.exim/storage/dkim +# +# The use of UID:GID as 100:101 is to match the ones used by the exim user on +# the container. +# +# Now use it just uncomment the below line and replace username with your host user. +#DKIM_VOLUME=/home/username/.exim/storage/dkim + +# So if we have a domain like example.com we want to set this to: +HOSTNAME=mail.example.com + + +# +# DOCKER CONTAINER ENVIRONMENT +# + +# REQUIRED +DKIM_DOMAINS=example.com + +# Custimize here the ips or range of ips from where Exim is allowed to relay. +RELAY_FROM_HOSTS=10.0.0.0/8:172.16.0.0/12 + +# Customize the size used by OpenSSL to generate the DKIM key +DKIM_KEY_SIZE=2048 + +# Customize here the the headers to be used in the DKIM signature. +DKIM_SIGN_HEADERS=Date:From:To:Subject:Message-ID + +# https://www.emailarchitect.net/domainkeys/doc/html/selector.htm +DKIM_SELECTOR=dkim diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c49bd7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env diff --git a/Dockerfile b/Dockerfile index eb4b32e..2f1d6f2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,20 +1,29 @@ -FROM alpine:3.6 +ARG ALPINE_VERSION=3.7 + +FROM alpine:$ALPINE_VERSION MAINTAINER Dmitry Prazdnichnov -ARG VERSION=4.89-r5 +ARG EXIM_VERSION=4.89.1-r0 + +ARG DKIM_DOMAINS=domain.tld +ARG RELAY_FROM_HOSTS=10.0.0.0/8:172.16.0.0/12:192.168.0.0/16 +ARG DKIM_KEY_SIZE=1024 +ARG DKIM_SELECTOR=dkim +ARG DKIM_SIGN_HEADERS=Date:From:To:Subject:Message-ID -LABEL org.label-schema.version=$VERSION \ +LABEL org.label-schema.version=${EXIM_VERSION} \ org.label-schema.vcs-url=https://github.com/bambocher/docker-exim-relay \ org.label-schema.license=MIT \ org.label-schema.schema-version=1.0 -ENV RELAY_FROM_HOSTS=10.0.0.0/8:172.16.0.0/12:192.168.0.0/16 \ - DKIM_KEY_SIZE=1024 \ - DKIM_SELECTOR=dkim \ - DKIM_SIGN_HEADERS=Date:From:To:Subject:Message-ID +ENV RELAY_FROM_HOSTS=${RELAY_FROM_HOSTS} \ + DKIM_DOMAINS=${DKIM_DOMAINS} \ + DKIM_KEY_SIZE=${DKIM_KEY_SIZE} \ + DKIM_SELECTOR=${DKIM_SELECTOR} \ + DKIM_SIGN_HEADERS=${DKIM_SIGN_HEADERS} -RUN apk --no-cache add exim=$VERSION libcap openssl \ - && mkdir /dkim /var/log/exim /usr/lib/exim /var/spool/exim \ +RUN apk --no-cache add exim=${EXIM_VERSION} libcap openssl \ + && mkdir -p /dkim /var/log/exim /usr/lib/exim /var/spool/exim \ && ln -s /dev/stdout /var/log/exim/main \ && ln -s /dev/stderr /var/log/exim/panic \ && ln -s /dev/stderr /var/log/exim/reject \ diff --git a/README.md b/README.md index 715bb24..0c84397 100644 --- a/README.md +++ b/README.md @@ -9,13 +9,13 @@ Create docker volume for dkim keys: ```shell -docker volume create --name=smtp-dkim +sudo docker volume create --name=smtp-dkim ``` Create docker container: ```shell -docker run \ +sudo docker run \ -d \ --name smtp \ --restart=always \ @@ -29,27 +29,41 @@ docker run \ ## [Docker Compose](https://docs.docker.com/compose/compose-file) -```yml -version: "2" -services: - smtp: - restart: always - image: bambucha/exim-relay - user: exim - ports: - - "25:25" - volumes: - - smtp-dkim:/dkim - hostname: mail.example.com - environment: - - RELAY_FROM_HOSTS=10.0.0.0/8:172.16.0.0/12:192.168.0.0/16 - - DKIM_KEY_SIZE=1024 - - DKIM_SELECTOR=dkim - - DKIM_SIGN_HEADERS=Date:From:To:Subject:Message-ID - - DKIM_DOMAINS=example.com -volumes: - smtp-dkim: - driver: local +Please rename `.env-example` to `.env` in order to customize how to build the + image and containers to run Exim from Docker. + +The `.env` file contains comments explaining how each parameter in the + `docker-compose.yml` can be overridden. + + +#### Run Exim on the background + +```shell +sudo docker-compose up -d smtp +``` + +#### Run Exim attached to the Shell + +```shell +sudo docker-compose up smtp +``` + +#### Destroy the running Exim container + +``` +sudo docker-compose down +``` + +#### Access the Shell of the running container + +```shell +sudo docker-compose exec smtp /bin/sh +``` + +#### Access the Shell without having a container running + +```shell +sudo docker-compose run -u root --entrypoint=/bin/sh --rm smtp ``` ## Reverse PTR @@ -73,13 +87,13 @@ example.com. 300 IN TXT "v=spf1 a mx -all" Get dkim public key with docker exec: ```shell -docker exec -it smtp cat /dkim/example.com.pub +sudo docker exec -it smtp cat /dkim/example.com.pub ``` or get dkim public key with docker-compose exec: ```shell -docker-compose exec smtp cat /dkim/example.com.pub +sudo docker-compose exec smtp cat /dkim/example.com.pub ``` or get dkim public key from docker volume: @@ -99,25 +113,25 @@ dkim._domainkey.example.com. 300 IN TXT "v=DKIM1; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GN Print a count of the messages in the queue: ```shell -docker exec -it smtp exim -bpc +sudo docker exec -it smtp exim -bpc ``` Print a listing of the messages in the queue (time queued, size, message-id, sender, recipient): ```shell -docker exec -it smtp exim -bp +sudo docker exec -it smtp exim -bp ``` Remove all frozen messages: ```shell -docker exec -it smtp exim -bpu | grep frozen | awk {'print $3'} | xargs exim -Mrm +sudo docker exec -it smtp exim -bpu | grep frozen | awk {'print $3'} | xargs exim -Mrm ``` Test how exim will route a given address: ```shell -docker exec -it smtp exim -bt test@gmail.com +sudo docker exec -it smtp exim -bt test@gmail.com ``` ``` @@ -133,31 +147,31 @@ test@gmail.com Display all of Exim's configuration settings: ```shell -docker exec -it smtp exim -bP +sudo docker exec -it smtp exim -bP ``` View a message's headers: ```shell -docker exec -it smtp exim -Mvh +sudo docker exec -it smtp exim -Mvh ``` View a message's body: ```shell -docker exec -it smtp exim -Mvb +sudo docker exec -it smtp exim -Mvb ``` View a message's logs: ```shell -docker exec -it smtp exim -Mar +sudo docker exec -it smtp exim -Mar ``` Remove a message from the queue: ```shell -docker exec -it smtp exim -Mrm [ ... ] +sudo docker exec -it smtp exim -Mrm [ ... ] ``` Send a message: diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..e740683 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,26 @@ +version: "2.1" + +services: + smtp: + restart: always + build: + context: ${EXIM_IMAGE_BUILD_CONTEXT:-.} + args: + ALPINE_VERSION: ${ALPINE_VERSION:-3.7} + EXIM_VERSION: ${EXIM_VERSION:-4.90} + image: ${EXIM_IMAGE:-bambucha/exim-relay}:${EXIM_VERSION:-4.90} + ports: + - ${EXIM_PORT_MAP:-25:25} + volumes: + - ${DKIM_VOLUME:-smtp-dkim}:/dkim + hostname: ${HOSTNAME:-mail.domain.tld} + environment: + DKIM_DOMAINS: ${DKIM_DOMAINS:-domain.tld} + RELAY_FROM_HOSTS: ${RELAY_FROM_HOSTS:-10.0.0.0/8:172.16.0.0/12:192.168.0.0/16} + DKIM_KEY_SIZE: ${DKIM_KEY_SIZE:-1024} + DKIM_SELECTOR: ${DKIM_SELECTOR:-dkim} + DKIM_SIGN_HEADERS: ${DKIM_SIGN_HEADERS:-Date:From:To:Subject:Message-ID} + +volumes: + smtp-dkim: + driver: local