Setting up Sendmail inside your Docker container

20 / Aug / 2015 by Ranvijay Jamwal 6 comments

In my last blog, we talked about setting up a WordPress site on Docker. I hope you all are now clear on how Docker works & how you can set up your own PHP applications on it.

In this blog, we will be talking about how to send emails from your Docker container. My use-case was that, I had a PHP application running inside a Docker container & the application was generating emails, which needed to be sent via a mail server. I decided to setup Sendmail inside the docker container. Below are the steps to setup Sendmail.

As we know that your Docker container can communicate to the outside world through all the ports via the host machine so we do not need to expose the SMTP port. What we need to do is install Sendmail inside our docker container, make an entry in hosts file and then start the Sendmail service inside it.

In the given use case, I already had a PHP application running inside my Docker container being served by httpd (apache2). You can take a basic centos:latest image & use the same Docker file I have given below to install Sendmail in the image. But, before that let us understand the script we would be using. We will be running this script inside the docker container later. Let’s name the script as dockerscriptetchosts.sh.

 

[js]

#!/bin/bash

line=$(head -n 1 /etc/hosts)
line2=$(echo $line | awk ‘{print $2}’)

echo "$line $line2.localdomain" >> /etc/hosts
[/js]

 

We are doing this as Sendmail needs a FQDN to start. It will add the following line to the /etc/hosts file:-

container_IP container_id container_id.localdomain

container_id.localdomain will act as a FQDN for the Docker container.

Now, comes the Docker file. The Docker file below, will install Sendmail service in the Docker imageLet the name of the below Docker file be Dockerfilesendmail, which will generate a final image named newcentosimage, after we build it using the Docker build command that will be discussed later.

[js]

FROM centos:latest

MAINTAINER Ranvijay Jamwal “ranvijay.jamwal@tothenew.com”

RUN yum update

RUN yum -y install sendmail

ADD dockerscriptetchosts.sh /home/dockerscriptetchosts.sh

[/js]

RUN is used to run commands inside intermediate containers created by Docker file. Also, ADD

ADD /home/ubuntu/dockerscriptetchosts.sh /home/dockerscriptetchosts.sh

is used to add the script (dockerscriptetchosts.shto the Docker image location /home/ from my host machine. We will just add the script but not execute it. The reason we won’t do that is because we would be getting a new container_id for every intermediate container created by Docker file.

After this just build the Docker file. You can do that by using the following command:-

sudo docker build -t newcentosimage -f Dockerfilesendmail /home/ubuntu/

-t: tag of the new image that will be created.
-f: name of Dockerfile.
/home/ubuntu/: a directory where my Docker file is present.

Now, Just run a new container from the newly create image “newcentosimage”.
This script needs to be run inside the newly created container which can be done using the following command:-

sudo docker exec -d $container_id bash /home/dockerscriptetchosts.sh

After this, you just need to start the Sendmail service inside the container. In case, your application running inside the container needs to use Sendmail, it is recommended that after starting the send mail you also restart your web server.

 

sudo docker exec -d $container_id /etc/init.d/sendmail start
sudo docker exec -d $container_id /usr/sbin/httpd -k restart

 

Now, your Sendmail service is up and running inside your container with you doing very few things. Also, few things would not need to be repeated.

FOUND THIS USEFUL? SHARE IT

comments (6)

  1. Cassius

    The write of this seems to be suggesting that sendmail and their webserver run in the same container. That goes against the single process policy of running a container, which kind of invalidates the entire post. Really, the webserver and mailserver should be running in separate containers and mail from webserver should be relayed via the mailserver container.

    Reply
  2. Jonathan Stiansen

    I found this to be patently incorrect, the best (and easiest) option is was done using the `docker run` flag. `–hostname`. This hostname is what the container will be called, and will automatically be put in both as the hostname, but also in the `/etc/hosts` file. E.g. `docker run –hostname=myContainer.myDomian `

    Reply
  3. Damian

    Easiest way I found was changing the CMD command.
    Just change /usr/local/bin/apache2-foreground to whatever your current docker image is running.
    CMD /usr/sbin/service sendmail restart && /usr/local/bin/apache2-foreground

    Reply
  4. Oscar Fanelli

    Easier (and actually working) way:

    Add in Dockerfile
    RUN echo -e “$(hostname -i)\t$(hostname) $(hostname).localhost” >> /etc/hosts

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *