Setting up SMTP server for your app in a container

Viewed 16511

I have an app which sends email and requires smtp running on port 25. For which I created another container and mapped port 25 from host to container.

That didnt quite work well, as it kept throwing the following error

ERROR: for smtp Cannot start service smtp: driver failed programming external connectivity on endpoint push_smtp_1 (25f260f6185dd34cfdb8fb9956c28187028aaca4d850d7a73acc4c2180c55696): Error starting userland proxy: Bind for 0.0.0.0:25: unexpected error (Failure EADDRINUSE)

Not sure what could be wrong here, following the other posts I tried restarting the docker client, as well as verified that there is nothing else running on port 25 lsof -i:25 Let me know if I am missing something here.

The 2nd part to this question is, what is the Ideal way to deal with smtp server.

  1. Should the smtp server be created within the app container. Came across this blog http://www.tothenew.com/blog/setting-up-sendmail-inside-your-docker-container/

  2. If not (1), then is it better to create a smtp container and map ports.Is so, whats the reason for getting the above error.

Below is how my docker-compose:

version: '3'
services:
  push:
    image: emailService
    ports:
    - "9602:9602/tcp"
    networks:
    - default
    build:
      context: ./
      dockerfile: Dockerfile
      args:
      - "TARGET=build"
    depends_on:
    - gearmand
    - smtp
  smtp:
    image: catatnight/postfix:latest
    ports:
    - "25:25"
    networks:
    - default
  gearmand:
    image: <path>/<to>/gearmand:latest
    ports:
    - "4730:4730/tcp"
    networks:
    - default

Thanks!

3 Answers

If you want the SMTP server to just be reachable from the other container and not from the outside, no need to map the port.

Using docker-compose, all defined containers will automatically be added to a network in which containers can reach each other by their name (see https://docs.docker.com/compose/networking/). If your custom "default" network is a bridge network, this will work as well.

That means, your SMTP container will directly be reachable at smtp:25 from other containers (i.e. its internal port and internal hostname instead of the host port and publicly routable IP address of your docker host).

Nobody else will be able to use your SMTP server like that. I think this might lead to problems with recipients not accepting the emails sent by it (see https://serverfault.com/q/364473). @David Maze has a point in saying that it's probably better to use a public/official mail provider anyways.

Use some other port on the host:

version: '3'
services:
  push:
    image: emailService
    ports:
    - "9602:9602/tcp"
    networks:
    - default
    build:
      context: ./
      dockerfile: Dockerfile
      args:
      - "TARGET=build"
    depends_on:
    - gearmand
    - smtp
  smtp:
    image: catatnight/postfix:latest
    ports:
    - "2525:25"
    networks:
    - default
  gearmand:
    image: <path>/<to>/gearmand:latest
    ports:
    - "4730:4730/tcp"
    networks:
    - default
Related