Featured image of post Docker setup for Mailslurper

Docker setup for Mailslurper

🐳 Docker Setup for Mailslurper – A Lightweight Local SMTP Server

Mailslurper is a lightweight SMTP server designed for development environments. It captures outgoing emails so developers can inspect them without sending anything to real inboxes.

The project is open source, but it’s not actively maintained, so getting it to work in Docker requires a bit of extra effort.

This guide walks you through building and running Mailslurper in Docker using its original source code.

πŸ“¦ Step 1: Create a Dockerfile

We’ll compile Mailslurper from source in a multi-stage Docker build.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
FROM golang:1.17-alpine AS builder

# Required for CGO and SQLite
RUN apk add --no-cache git build-base sqlite-dev

WORKDIR /src
RUN git clone --branch release-1.15.0 --depth 1 https://github.com/mailslurper/mailslurper.git .
WORKDIR /src

# Setup modules
RUN go mod init github.com/mailslurper/mailslurper  true
RUN go mod tidy  true
RUN go get github.com/globalsign/mgo@v2 || true

# Build with CGO enabled (default)
WORKDIR /src/cmd/mailslurper
RUN go build -o /mailslurper

FROM alpine:3.20

WORKDIR /app

# Install CA certs and libc + sqlite for dynamic linking
# ca-certificates - optionally
RUN apk add --no-cache ca-certificates libc6-compat sqlite

# Add config.json
RUN echo '{\
  "wwwAddress": "0.0.0.0",\
  "wwwPort": 8080,\
  "wwwPublicURL": "",\
  "serviceAddress": "0.0.0.0",\
  "servicePort": 8085,\
  "servicePublicURL": "",\
  "smtpAddress": "0.0.0.0",\
  "smtpPort": 2500,\
  "dbEngine": "SQLite",\
  "dbHost": "",\
  "dbPort": 0,\
  "dbDatabase": "./mailslurper.db",\
  "dbUserName": "",\
  "dbPassword": "",\
  "maxWorkers": 1000,\
  "autoStartBrowser": false,\
  "keyFile": "",\
  "certFile": "",\
  "adminKeyFile": "",\
  "adminCertFile": ""\
}' > /app/config.json

# Copy built binary
COPY --from=builder /mailslurper /app/mailslurper

EXPOSE 2500 8080 8085

CMD ["./mailslurper", "--config", "config.json"]

πŸ§ͺ Step 2: Add Mailslurper to docker-compose.yml

You can now include Mailslurper as a service in any Docker Compose project to test outbound emails locally:

1
2
3
4
5
6
7
8
9
version: "3.8"

services:
  mailslurper:
    build: .
    ports:
      - "2500:2500" # SMTP interface
      - "8080:8080" # Web UI
      - "8085:8085" # Admin service interface

πŸš€ Step 3: Run It

Start Mailslurper using Docker Compose:

1
docker compose up

Once running, you can:

Send test emails from any service in your stack to localhost:2500

View and inspect emails via the Mailslurper Web UI at http://localhost:8080

And of course, change the ports according to your requirements.

🌎 Earth, Germany, Munich, Made with β™₯