title: Application not able to connect to smtp host and port 587 using docker
body: <p>I am new to docker. I need your help in resolving one of the docker issue which I am facing for past 1 week. I have a Spring Boot application which connects to a company email server using SMTP host and SMTP port to send out emails. The application is working fine when it is working as a normal java program locally. Application is able to establish a connection to SMTP server and able to send out emails. Now, as soon as the application is on docker container, I am getting “connection timed out” error. </p>
  
  <p>Please note that I am able to send out curl commands like google.com from docker container just to check if I am able to connect to the internet or not and was able to get the response there. I am not sure what could go wrong as curl is working for HTTP but SMTP connection is not working on docker container. Do I need to change anything to docker settings?</p>
  
  <p>I have also tried making the /etc/hosts file and /etc/resolve.conf at par with the host configuration file but still not able to proceed ahead. I am using mac so I thought that the error might have come because of the settings of thin layer VM that docker for mac creates, so I tried running the application on linux machine, but still got no success.</p>
  
  <p>Any suggestions or pointers would be highly appreciated.</p>
  
  <p>The error stack trace:</p>
  
  <pre><code>notification-service        | Caused by: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.company.com, 587; timeout 10000
  notification-service        |   at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2209) ~[javax.mail-1.6.1.jar!/:1.6.1]
  notification-service        |   at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:740) ~[javax.mail-1.6.1.jar!/:1.6.1]
  notification-service        |   at javax.mail.Service.connect(Service.java:366) ~[javax.mail-1.6.1.jar!/:1.6.1]
  notification-service        |   at javax.mail.Service.connect(Service.java:246) ~[javax.mail-1.6.1.jar!/:1.6.1]
  notification-service        |   at com.company.EmailSender.sendEmailNotification(EmailSender.java:127) ~[classes!/:na]
  notification-service        |   ... 38 common frames omitted
  notification-service        | Caused by: java.net.SocketTimeoutException: connect timed out
  notification-service        |   at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_171]
  notification-service        |   at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_171]
  notification-service        |   at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_171]
  notification-service        |   at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_171]
  notification-service        |   at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_171]
  notification-service        |   at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_171]
  notification-service        |   at com.sun.mail.util.WriteTimeoutSocket.connect(WriteTimeoutSocket.java:115) ~[javax.mail-1.6.1.jar!/:1.6.1]
  notification-service        |   at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:351) ~[javax.mail-1.6.1.jar!/:1.6.1]
  notification-service        |   at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:239) ~[javax.mail-1.6.1.jar!/:1.6.1]
  notification-service        |   at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2175) ~[javax.mail-1.6.1.jar!/:1.6.1]
  notification-service        |   ... 42 common frames omitted
  </code></pre>
  
  <p>This is my Dockerfile</p>
  
  <pre><code>FROM com.company/serverjre:8
  
  ARG JAR_FILE
  ARG JAR_INSTRUMENT
  
  VOLUME /tmp
  COPY ${JAR_FILE} /app.jar
  COPY ${JAR_INSTRUMENT} /spring-instrument.jar
  
  EXPOSE 9000
  
  ENTRYPOINT exec java -javaagent:/spring-instrument.jar -Duser.timezone=GMT -Djava.security.egd=file:/dev/./urandom $JAVA_OPTS -jar /app.jar
  </code></pre>
  
  <p>This is a snippet of my docker-compose.yml</p>
  
  <pre><code>version: '3.5'
  services:
    notification-service:
      hostname: notification-service
      container_name: notification-service
      image: com.company/notification-service:1.0-SNAPSHOT
      depends_on:
        - kafka
        - kafka-schema-registry
      ports:
        - 9810:9810
      volumes:
        - ./notification-service-application.yaml:/config/application.yml
  </code></pre>
  
  <p>Tried adding the ports 587:587 in docker-compose.yml but no success.</p>