Issue - Dockerfile - Postfix SendGrid on CentOS

Greetings,

Having issues to start a container that build using Dockerfile.

I am trying to simply run a CentOS image that has Postfix configured with SendGrid or any SMTP Relay service # KB # Integrate SendGrid with Postfix - SendGrid Documentation | SendGrid | Twilio

Dockerfile below:

FROM centos:7

# Update package repository and install postfix and SASL

RUN yum update -y && yum install -y postfix cyrus-sasl-plain



# Configure postfix to listen on all interfaces and enable SASL

RUN sed -i 's/inet_interfaces = localhost/inet_interfaces = all/' /etc/postfix/main.cf \

&& echo "broken_sasl_auth_clients = yes" >> /etc/postfix/main.cf \

&& echo "smtpd_sasl_auth_enable = yes" >> /etc/postfix/main.cf \

&& echo "smtpd_sasl_security_options = noanonymous" >> /etc/postfix/main.cf \

&& echo "smtp_sasl_tls_security_options = noanonymous" >> /etc/postfix/main.cf \

&& echo "smtp_tls_security_level = encrypt" >> /etc/postfix/main.cf \

&& echo "header_size_limit = 4096000" >> /etc/postfix/main.cf \

&& echo "relayhost = [smtp.sendgrid.net]:587" >> /etc/postfix/main.cf

# Expose the SMTP port (25)

EXPOSE 25

# Run a script to create a SASL password file for the specified user with the specified password

CMD ["/bin/bash", "-c", "echo $USERNAME $PASSWORD > /etc/postfix/sasl_passwd && chmod 600 /etc/postfix/sasl_passwd && echo 'smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd' >> /etc/postfix/main.cf && postfix start"]


Then I do a docker build

docker build -t user/centos-postfix-sendgrid . --no-cache=true --platform=linux/amd64

Then docker run

docker run -itd --cap-add=NET_BIND_SERVICE -p 2513:25 -e USERNAME=apikey -e PASSWORD=passwordhere --name centos7_postfix_sendgrid user/centos-postfix-sendgrid

The container doesn’t start, not sure what issue is here. Any ideas?

Thanks

Do you see any logs? (docker logs containername)
Have you tried to run the command in the container manually? (“docker run --rm -it imagename bash” and run the command defined in the Dockerfile)

Note that the process inside the container has to run in the foreground to keep the container alive.
Instead of separating commands using && in one CMD definition, you should create a custom entrypoint or command script and run the final command with exec. I have some examples why you should use exec:

1 Like

Hey @rimelek and thanks for the response!

The only log before the container automatically stops is this:

* 12/31/2022 1:53:36 PM postfix/postfix-script: starting the Postfix mail system

* Container stopped

I ran the docker run as you asked --rm and without the -d, and it’s seems to be running!

docker run --rm -it -p 2513:25 -e USERNAME=apikey -e PASSWORD=thisisyourapikeypassword --name centos7_postfix_sendgrid_2 user/centos-postfix-sendgrid bash

And I am able to keep the container running for sure as long I am bash in it.

Any idea what the issue with -d to have detached and running on docker run automatically without the bash? or if there is something missing from the Dockerfile to have that fixed?

postfix runs in the background except the “starting” message.

This might help:

1 Like

It’s a bit complex that what I thought when I made that Dockerfile, not sure if mailslurper would make it a better alternative?

Maybe, at least a local inbox and mailserver, not with SMTP relay like Postfix.