Setting up Sendmail inside your Docker container

20 / Aug / 2015 by Ranvijay Jamwal 4 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.

 


#!/bin/bash

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

echo "$line $line2.localdomain" >> /etc/hosts

 

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.


FROM centos:latest

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

RUN yum update

RUN yum -y install sendmail

ADD dockerscriptetchosts.sh /home/dockerscriptetchosts.sh

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 (4)

  1. 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
  2. Oscar Fanelli

    Easier (and actually working) way:

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

    Reply

Leave a comment -