MailCatcher in Docker

How to set up a fake SMTP server in a container for receiving and viewing emails

Courtney Zhan
3 min readSep 11, 2022

--

In previous articles, we discussed email testing and using a fake SMTP server to catch emails. This article will show you how to set up a Docker container to host MailCatcher, a simple fake SMTP server.

Why is Docker a good set-up option for MailCatcher?

  • Independent
    MailCatcher is an isolated and independent component with no dependencies. It makes sense to stand up an individual container to handle this.
  • Reusable
    MailCatcher can be used to service any number of applications.
    Depending on your use case, you may want to set up multiple containers (with different ports) to separate the apps. It is easy to duplicate an existing Docker image into a new container.

So, if you already using Docker, then MailCatcher-in-Docker is a quick and easy setup.

Setting up MailCatcher

The general steps to set up and run MailCatcher:

  1. Install Ruby
  2. Install the gem mailcatcher
  3. Run mailcatcher
  4. Verify MailCatcher is running
    View mail at localhost:1080
    Send mail to localhost:1025

Creating the Dockerfile

1. Install Ruby

This could be done the “classic” way (pulling down a Ubuntu image, running command/s to install Ruby, etc).

Luckily, we can skip all that by pulling down the official Ruby Docker image (ruby).

FROM ruby

Translating the rest of the setup steps is quite easy.

2. Install MailCatcher

In the dockerfile, use RUN to execute:

RUN gem install mailcatcher --no-document

The no-document flag means that the documentation for mailcatcher will not be installed. Since this is inside a container we do not need the

--

--