Setting up a mailcatcher LXC container for development purposes

Setting up a mailcatcher LXC container for development purposes
by Bob Brown bob@turboweb.co.nz, 14th Jan 2015.
[TOC]
The goal is to create and configure a LXC container that other containers (and the host machine itself) can deliver email to. Ubuntu 14.04 is the host OS. All email will be accepted and be available via a single IMAP account for browsing. We will use the following:
- exim for accepting mail
- dovecot to serve that mail to clients
- roundcube configured to work with dovecot (as an alternative to using a desktop mail client like Thunderbird)
Initial config
If you haven’t already, install LXC on your machine/server:
sudo apt-get install lxc
Note that all commands to work with the containers will be run as root (i.e. prefixed with sudo).
Create and start the container
First create the container - we are using “mc” as the container name.
sudo lxc-create -t ubuntu-cloud -n mc -- -S /home/bob/.ssh/id_rsa.pub
Now start the container:
sudo lxc-start -n mc -d
Install packages on the container
Attach to the container and install some packages:
sudo lxc-attach -n mc
root@mc:/# apt-get update
root@mc:/# apt-get install -y exim4 mysql-server php5-mysql dovecot-imapd roundcube
Answer the installation questions as follows:
- When asked for a password for mysql, use “root”.
- “Yes” (default) to making a self-signed certificate (default hostname of “localhost” is fine)
- “Yes” to configuring database for roundcube with dbconfig-common
- “mysql” (default) to DB type to use
- “root” for the database administrative password
- blank password (default) for the MySQL application password for roundcube
Configure dovecot
Edit /etc/dovecot/conf.d/10-master.conf and uncomment the line port = 143 then restart dovecot:
service dovecot restart
Configure roundcube
When roundcube installs it doesn’t make itself available via the apache configuration by default. To do this edit the /etc/roundcube/apache.conf file and uncomment the two Alias lines at the top.
To make things easier for logging in, edit /etc/roundcube/main.inc.php and set the default host to localhost:
$rcmail_config['default_host'] = 'localhost';
Also enable PHP’s mcrypt extension:
root@mc:/# php5enmod mcrypt
Finally restart Apache:
apache2ctl restart
At this point you should be able to log into RoundCube with the default “ubuntu” user with the password “ubuntu” that is created when an LXC container is created. Open your browser and point it at http://mc.lxc/roundcube (assuming you have the .lxc TLD resolving to containers via NetworkManager - see http://seminar.io/2014/07/27/dns-resolution-for-lxc-in-ubuntu-trusty/).
If you get an error when logging in you can check the RoundCube error log at /var/lib/roundcube/logs/error.
Configure exim
Allow SMTP connections from host machine
We need to allow connections from the host machine to exim’s SMTP service for delivering mail. We can use the dpkg-configure wizard for this:
dpkg-reconfigure exim4-config
- For the first answer, select “internet site; mail is sent and received directly using SMTP”.
- For the third answer, enter “127.0.0.1 ; 0.0.0.0 ; ::1” - this will allow external SMTP connections.
- For the fifth answer, enter “*” - this will allow any message to be accepted, not just email messages addressed to users on this machine.
Catch all email
We need to configure exim to accept email for any email address and redirect it to the local ubuntu user.
Edit the file /etc/exim4/exim4.conf.template and insert this exim “router” immediately after the “begin routers” line (about line 962).
catchall:
driver = redirect
domains = ! +local_domains
data = ubuntu@localhost
no_more
And finally restart exim:
service exim4 restart
Testing and Completion
You should now be able to configure a Thunderbird account to point to the server, or log in and send emails from the RoundCube installation.