1

I've created a docker image of a springboot maven application. This application communicate with smtp server to send email through port 587. The app works fine locally, however when I create a docker image and run it, the application breaks when it try to access a socket to send email.

This is how my Dockerfile looks like.

FROM adoptopenjdk:11-jre-hotspot

RUN mkdir /opt/app

ADD target/camunda-fire-alarm.jar camunda-fire-alarm.jar

EXPOSE 8080 587

ENTRYPOINT ["java", "-jar", "camunda-fire-alarm.jar"]

The following are the commands I use to build and run the docker image.

build command:

docker build -f Dockerfile -t camunda-fire-alarm .

run command:

docker run -p 8080:8080 -p 587:587 camunda-fire-alarm

This is the Java code I use to send email from outlook

public static void SendFromOutlook(String from, String pass, String to, String subject, String body) 
{
        final String username = from;  // like yourname@outlook.com
        final String password = pass;   // password here

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp-mail.outlook.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });
        session.setDebug(true);

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(username));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(String.valueOf(to)));   // like inzi769@gmail.com
            message.setSubject(subject);
            message.setText(body);

            System.out.println("Sending email to : "+to + " with message < " + body + " >");
            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
}

EDIT: Stacktrace

 ENGINE-16004 Exception while closing command context: ENGINE-03051 There was an exception while invoking the TaskListener. Message: 'Exception while invoking TaskListener: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp-mail.outlook.com, 587; timeout -1;
  nested exception is:
        java.net.UnknownHostException: smtp-mail.outlook.com'

org.camunda.bpm.engine.ProcessEngineException: ENGINE-03051 There was an exception while invoking the TaskListener. Message: 'Exception while invoking TaskListener: com.sun.mail.util.M
ailConnectException: Couldn't connect to host, port: smtp-mail.outlook.com, 587; timeout -1;
  nested exception is:
        java.net.UnknownHostException: smtp-mail.outlook.com'
        at org.camunda.bpm.engine.impl.db.EnginePersistenceLogger.invokeTaskListenerException(EnginePersistenceLogger.java:449) ~[camunda-engine-7.12.0.jar!/:7.12.0]

Not sure why the docker breaks the application during runtime. I would appreciate any help/guidelines.

Thanks in Advance

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
Ali Asad
  • 251
  • 4
  • 15
  • 1
    Usually Docker container can access the internet, so I suspect you are looking at a general connectivity issue with your docker setup. Check out this post: https://stackoverflow.com/questions/20430371/my-docker-container-has-no-internet – Fritz Duchardt May 19 '20 at 15:20
  • 1
    Can you provide a stacktrace of the exception? – Elgarni May 19 '20 at 15:24
  • hi @Elgarni thanks for your comment. I've updated the post with stacktrace report. – Ali Asad May 19 '20 at 15:50
  • hi @FritzDuchardt thanks for your comment. I've checked that post but that didn't help me. I'm actually new to Docker, that post contains some technical level expertise which I don't have. Would appreciate if you could share a simple solution to my problem. Thank you – Ali Asad May 19 '20 at 15:56
  • It sounds DNS queries are not being resolved, clearly from `UnknownHostException`. I wonder what is the environment you're running docker, is it a linux host, or a VM? Can you do `docker run --rm -it adoptopenjdk:11-jre-hotspot sh -c "curl 216.58.211.206"`, and share the output (216.58.211.206 is Google's ip) – Elgarni May 19 '20 at 20:26

0 Answers0