Skip to content

Commit d18c71a

Browse files
committed
Docker: initiate merge of #409
git-svn-id: https://svn.code.sf.net/p/davmail/code/trunk@3794 3d1905a2-6b24-0410-a738-b14d5a86fcbd
1 parent bb6b241 commit d18c71a

File tree

6 files changed

+213
-0
lines changed

6 files changed

+213
-0
lines changed

src/docker/Dockerfile

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
FROM debian:12 AS base
2+
RUN apt-get update
3+
4+
5+
# First we build the jar file
6+
FROM base AS builder
7+
WORKDIR /davmail
8+
9+
RUN apt-get install -y ant git
10+
COPY . .
11+
RUN ant -Dfile.encoding=UTF-8
12+
13+
14+
# Then we run the jar file
15+
FROM base AS runner
16+
WORKDIR /davmail
17+
18+
RUN apt-get install -y openjdk-17-jre libcommons-codec-java libcommons-logging-java libhtmlcleaner-java libhttpclient-java libjackrabbit-java libjcifs-java libjettison-java libjna-java liblog4j1.2-java libmail-java libopenjfx-java libservlet-api-java libslf4j-java libstax2-api-java libswt-cairo-gtk-4-jni libswt-gtk-4-java libwoodstox-java
19+
20+
# Copy jar file
21+
COPY --from=builder /davmail/dist/davmail.jar /davmail/davmail.jar
22+
23+
# Copy default davmail.properties and set tokenFilePath
24+
COPY --from=builder /davmail/src/etc/davmail.properties /config/davmail.properties
25+
RUN sed -i 's/#davmail.oauth.tokenFilePath=/davmail.oauth.tokenFilePath=\/config\/.env.oauth/' /config/davmail.properties
26+
27+
VOLUME [ "/config" ]
28+
EXPOSE 1110 1025 1143 1080 1389
29+
30+
COPY --from=builder /davmail/src/contribs/docker/entrypoint.sh /davmail/entrypoint.sh
31+
ENTRYPOINT [ "/davmail/entrypoint.sh" ]
32+
33+

src/docker/Makefile

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
IMAGE_NAME = mguessan/davmail
2+
IMAGE_VERSION ?= ""
3+
4+
ifeq ($(IMAGE_VERSION),"")
5+
IMAGE_LABEL ?= $(IMAGE_NAME)
6+
else
7+
IMAGE_LABEL ?= $(IMAGE_NAME):${IMAGE_VERSION}
8+
endif
9+
10+
image:
11+
@echo "Building Docker image ${IMAGE_LABEL}..."
12+
docker build . -t $(IMAGE_LABEL)
13+
14+
tag-latest-without-build:
15+
@echo "Tagging Docker image ${IMAGE_LABEL} with latest..."
16+
docker tag `docker image ls --format '{{.ID}}' $(IMAGE_LABEL)` $(IMAGE_NAME):latest
17+
18+
run:
19+
@echo "Starting davmail"
20+
docker compose up -d
21+
22+
run-gui:
23+
@echo "Running davmail with GUI (to get token)"
24+
docker run --network=host --rm --name davmail --hostname davmail \
25+
-u "$UID" \
26+
-e "DISPLAY=${DISPLAY}" \
27+
-v /tmp/.X11-unix:/tmp/.X11-unix \
28+
-v "${XAUTHORITY:-$HOME/.Xauthority}:/.Xauthority:ro" \
29+
-v ./config:/config \
30+
davmail davmail --token
31+
32+
latest: image tag-latest-without-build

src/docker/README.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Davmail Docker
2+
3+
To run davmail in Docker, download the `compose.yml` file and run `docker compose up -d`.
4+
5+
Make sure to change the ports in the `compose.yml` file to make sure you are not exposing them to the public (unless you want that, of course).
6+
7+
If you want to set up OAuth2, but your host is headless/does not have access to a GUI, please see below
8+
9+
If you want to set up SSL/certificates, scroll down to SSL
10+
11+
# OAuth2 on headless
12+
13+
Download the `Dockerfile` on your **PC** (needs GUI access) and run the commands below
14+
15+
```
16+
docker build . -t davmail # this might take a bit
17+
docker run --network=host --rm --name davmail --hostname davmail -v /tmp/.X11-unix:/tmp/.X11-unix -e "DISPLAY=${DISPLAY}" -v "${XAUTHORITY:-$HOME/.Xauthority}:/.Xauthority:ro" -v ./config:/config -u "$UID" davmail davmail --token
18+
```
19+
20+
A new window should pop up with a few settings. Please change "Exchange Protocol" from `EWS` to for example `O365Manual` (or whatever method you want to use, see [Exchange protocol](https://davmail.sourceforge.net/gettingstarted.html)) and press "Save"
21+
22+
Next, open your email client, e.g. thunderbird. Add a new account and make sure to click "Configure manually" or something like that.
23+
24+
Set receiving/incoming (IMAP) as following:
25+
- hostname: `localhost`
26+
- port: `1143`
27+
- Connection security: `none`
28+
- Authentication method: `Normal password`
29+
30+
Set sending/outgoing (SMTP) as following:
31+
- hostname: `localhost`
32+
- port: `1025`
33+
- Connection security: `none`
34+
- Authentication method: `Normal password`
35+
36+
Both using username `<email>` and password `<password>`. Note that `<email>` has to match the email you want log in to (using oauth2), and `<password>` can be ANY password, even different than your account password.
37+
38+
After pressing connect on the email client (sometimes you may need to ignore ssl warnings), the GUI from davmail should show instructions on how to authenticate (depending on the Exchange Protocol you set before). Please follow these instructions and confirm that your account is now connected. To move the configuration to your server/headless instance, stop the docker container (Ctrl+C) and type `cat config/davmail.properties`.
39+
40+
At the bottom, there is an entry called `davmail.oauth.<email>.refreshToken={AES}...`. Edit `config/.env.oauth` on your server to include `<email>={AES}...` and restart the container.
41+
42+
You can now configure your email client again using the same steps before, but instead of localhost you should use your server ip (make sure you put in the same `<password>`).
43+
44+
# SSL
45+
46+
Below are two methods described to setup SSL. Either using a service like `letsencrypt` or providing your own certificate, or using `traefik` (a reverse proxy).
47+
48+
## Letsencrypt/own certificate
49+
50+
If you are running davmail on your server, make sure to setup SSL. In order to do this, use a service like letsencrypt or create a self signed certificate. In the case of letsencrypt, go to `certs/live/<DOMAIN>` and run the following command
51+
52+
```
53+
openssl pkcs12 -export -in fullchain.pem -inkey privkey.pem -certfile cert.pem -out davmail.p12
54+
```
55+
56+
Make sure to set a password `<password2>` and make sure it is different than `<password>` (it can be the same, but for your own sake use a different one, please).
57+
58+
Move the new `davmail.p12` file over to the `config` directory on your server, edit `config/davmail.properties` (make sure to put the correct `<password2>`!).
59+
```
60+
davmail.ssl.keystoreType=PKCS12
61+
davmail.ssl.keyPass=<password2>
62+
davmail.ssl.keystoreFile=/davmail.p12
63+
davmail.ssl.keystorePass=<password2>
64+
```
65+
66+
Restart your container and go to your email client. Go to account settings and enable SSL (keep the same ports) and connect.
67+
68+
## Traefik
69+
70+
Note that communication between your email client and traefik will be encrypted, and then traefik will forward the unencrypted traffic (through the docker's internal network) to your DavMail instance. This way, you don't have to add another certificate manager if you are already using traefik to handle your certificates.
71+
72+
First download the new `compose-traefik.yml` from this directory and rename it to `compose.yml`. Then edit/create a `.env` file and put
73+
```
74+
DOMAIN=domain.com
75+
```
76+
77+
This means that you can access your davmail instance over `davmail.domain.com`, make sure to change domain.com such that traefik can generate a certificate for it.
78+
79+
80+
To use traefik to manage your certificate, change your `traefik.yml` config (or `.toml`, depending on you traefik configuration) and add the following entrypoints:
81+
82+
```
83+
entryPoints:
84+
imap-tls:
85+
address: :1143
86+
smtp-tls:
87+
address: :1025
88+
```
89+
90+
Then change the `compose.yml` where you have you traefik instance, expose port 1143 and 1025 (and others if needed) and make sure that both davmail and traefik share a docker network. At last, run `docker compose up -d` for both traefik and davmail and now you can point your email clients to `davmail.domain.com` and enable SSL (note that the port stays the same).

src/docker/compose-traefik.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
services:
2+
davmail:
3+
container_name: davmail
4+
hostname: davmail
5+
image: ghcr.io/mguessan/davmail:stable
6+
restart: unless-stopped
7+
volumes:
8+
- ./config:/config
9+
labels:
10+
- traefik.enable=true
11+
# Used for SSl cert
12+
- traefik.http.routers.davmail.rule=Host(`davmail.${DOMAIN}`)
13+
- traefik.http.routers.davmail.service=noop@internal
14+
- traefik.http.routers.davmail.middlewares=sso-required@file
15+
# IMAP
16+
- traefik.tcp.routers.imap-tls.rule=HostSNI(`*`)
17+
- traefik.tcp.routers.imap-tls.entrypoints=imap-tls
18+
- traefik.tcp.routers.imap-tls.service=imap-tls
19+
- traefik.tcp.services.imap-tls.loadbalancer.server.port=1143
20+
- traefik.tcp.routers.imap-tls.tls=true
21+
# SMTP
22+
- traefik.tcp.routers.smtp-tls.rule=HostSNI(`*`)
23+
- traefik.tcp.routers.smtp-tls.entrypoints=smtp-tls
24+
- traefik.tcp.routers.smtp-tls.service=smtp-tls
25+
- traefik.tcp.services.smtp-tls.loadbalancer.server.port=1025
26+
- traefik.tcp.routers.smtp-tls.tls=true

src/docker/compose.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# See src/contribs/docker/README.md for instructions
2+
3+
services:
4+
davmail:
5+
container_name: davmail
6+
hostname: davmail
7+
image: ghcr.io/mguessan/davmail:stable
8+
restart: unless-stopped
9+
ports:
10+
- 127.0.0.1:1025:1025 # SMTP
11+
- 127.0.0.1:1143:1143 # IMAP
12+
volumes:
13+
- ./config:/config

src/docker/entrypoint.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
#
3+
# davmail.properties is in /davmail.properties
4+
# /home is home directory
5+
#
6+
# set memory and enable DNS expiration
7+
8+
: "${JAVA_OPT_BASE:=-Xmx512M -Dsun.net.inetaddr.ttl=60}"
9+
: "${JAVA_OPT_EXPORTS:=}"
10+
JAVA_OPTS="$JAVA_OPT_BASE $JAVA_OPT_EXPORTS $JAVA_OPT_JAR $JAVA_OPT_USER"
11+
12+
# Determined experimentally
13+
export CLASSPATH=/davmail/davmail.jar:/usr/share/java/commons-logging.jar:/usr/share/java/httpclient.jar:/usr/share/java/httpcore.jar:/usr/share/java/jackrabbit-webdav.jar:/usr/share/java/javafx-base.jar:/usr/share/java/javafx-controls.jar:/usr/share/java/javafx-graphics.jar:/usr/share/java/javafx-media.jar:/usr/share/java/javafx-swing.jar:/usr/share/java/javafx-web.jar:/usr/share/java/javax.mail.jar:/usr/share/java/jettison.jar:/usr/share/java/jna.jar:/usr/share/java/log4j-1.2.jar:/usr/share/java/swt4.jar:/usr/share/java/stax2-api.jar:xercesImpl.jar:woodstox-core-asl.jar:commons-codec.jar:htmlcleaner.jar:jdom2.jar:jcifs.jar
14+
export SWT_GTK3=0
15+
16+
if [ -z "$1" ]; then set -- /config/davmail.properties -notray; fi
17+
18+
# shellcheck disable=SC2086
19+
exec "${JAVA:-java}" $JAVA_OPTS davmail.DavGateway "$@"

0 commit comments

Comments
 (0)