Setting up sendmail inside your Docker container

20 / Aug / 2015 by Ranvijay Jamwal 3 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. So, lets get started and see how we can do that.

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 entry in hosts file and then start the sendmail service inside it.

In my 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 lets understand the script we would be using. We will be running this script inside the docker container later. Lets 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 wont 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/: 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, its recommended that after starting the sendmail 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, yoursendmail 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 (3)

  1. 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 -