{"id":25018,"date":"2015-08-20T13:54:39","date_gmt":"2015-08-20T08:24:39","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=25018"},"modified":"2016-12-15T18:05:25","modified_gmt":"2016-12-15T12:35:25","slug":"setting-up-sendmail-inside-your-docker-container","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/setting-up-sendmail-inside-your-docker-container\/","title":{"rendered":"Setting up Sendmail inside your Docker container"},"content":{"rendered":"<p>In my<a href=\"http:\/\/www.tothenew.com\/blog\/wordpress-docker\/\"> last blog<\/a>, we talked about setting up a WordPress site on Docker. I hope you all are now clear on how Docker works &amp; how you can set up your own PHP applications on it.<\/p>\n<p>In this blog, we will be talking about how to send emails from your Docker container. My <strong>use-case <\/strong>was that, I had a PHP application running inside a Docker container &amp; the application was generating emails, which needed to be sent via a mail server. I decided to setup Sendmail inside the docker container.\u00a0Below are the steps to setup Sendmail.<\/p>\n<p>As we know that <a title=\"docker devOps\" href=\"http:\/\/www.tothenew.com\/devops-chef-puppet-docker\">your Docker<\/a> 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\u00a0entry in hosts file and then start the Sendmail service inside it.<\/p>\n<p>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 <strong>centos:latest<\/strong> image &amp; 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&#8217;s name the script as\u00a0<strong>dockerscriptetchosts.sh.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>[js]<\/p>\n<p>#!\/bin\/bash<\/p>\n<p>line=$(head -n 1 \/etc\/hosts)<br \/>\nline2=$(echo $line | awk &#8216;{print $2}&#8217;)<\/p>\n<p>echo &quot;$line $line2.localdomain&quot; &gt;&gt; \/etc\/hosts<br \/>\n[\/js]<\/p>\n<p>&nbsp;<\/p>\n<p>We are doing this as Sendmail needs a FQDN to start.\u00a0It will add the following line to the \/etc\/hosts file:-<\/p>\n<p><code>container_IP container_id container_id.localdomain<\/code><\/p>\n<p><strong>container_id.localdomain<\/strong> will act as a FQDN for the Docker container.<\/p>\n<p>Now, comes the Docker file. The Docker file below, will install Sendmail service in the Docker image<b>.\u00a0<\/b>Let the name of the below Docker file be\u00a0<strong>Dockerfilesendmail,\u00a0<\/strong>which will generate a final image named\u00a0<strong>newcentosimage,<\/strong>\u00a0after we build it using the Docker build command that will be discussed later.<\/p>\n<p>[js]<\/p>\n<p>FROM centos:latest<\/p>\n<p>MAINTAINER Ranvijay Jamwal \u201cranvijay.jamwal@tothenew.com\u201d<\/p>\n<p>RUN yum update<\/p>\n<p>RUN yum -y install sendmail<\/p>\n<p>ADD dockerscriptetchosts.sh \/home\/dockerscriptetchosts.sh<\/p>\n<p>[\/js]<\/p>\n<p><strong>RUN<\/strong> is used to run commands inside intermediate containers created by Docker file. Also, <strong>ADD<\/strong><\/p>\n<p><code>ADD \/home\/ubuntu\/dockerscriptetchosts.sh \/home\/dockerscriptetchosts.sh<\/code><\/p>\n<p>is used to add the script (dockerscriptetchosts.sh<strong>)\u00a0<\/strong>to the Docker image location \/home\/ from my host machine. We will just add the script but not execute it. The reason we won&#8217;t do that is because we would be getting a new container_id for every intermediate container created by Docker file.<\/p>\n<p>After this just build the Docker file. You can do that by using the following command:-<\/p>\n<p><code>sudo docker build -t newcentosimage -f Dockerfilesendmail \/home\/ubuntu\/<\/code><\/p>\n<p><strong>-t<\/strong>: tag of the new image that will be created.<br \/>\n<strong>-f<\/strong>: name of Dockerfile.<br \/>\n<strong>\/home\/ubuntu\/<\/strong>: a\u00a0directory where my Docker file is present.<\/p>\n<p>Now, Just run a new container from the newly create image &#8220;newcentosimage&#8221;.<br \/>\nThis script needs to be run inside the newly created container which can be done using the following command:-<\/p>\n<p><code>sudo docker exec -d $container_id bash \/home\/dockerscriptetchosts.sh<\/code><\/p>\n<p>After this,\u00a0you 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.<\/p>\n<p>&nbsp;<\/p>\n<pre>sudo docker exec -d $container_id \/etc\/init.d\/sendmail start\r\nsudo docker exec -d $container_id \/usr\/sbin\/httpd -k restart<\/pre>\n<p>&nbsp;<\/p>\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &amp; 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 [&hellip;]<\/p>\n","protected":false},"author":174,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":273},"categories":[1174,2348],"tags":[248,1883,2250,2249,2248,2247],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/25018"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/174"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=25018"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/25018\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=25018"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=25018"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=25018"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}