I'm developing an app that sends emails using an smtp server located in another private network. This server doesn't use authentication, it only restricts the the ip address of my pc.
I'm using the net/smtp
package to send emails.
When I run the app in localhost everything works perfectly, however when I run it in a docker container it gives me timeout when trying to send the email. I can ping
the server inside the container, is just the mail functionality that doesn't work.
I tried using the google's smtp service and it does work. I also exposed the 587 port in the dockerfile however still doesn't work.
Any help will be appreciated.
Edit:
To send emails I used the sendMail function, which works in localhost and also when I use google's smtp server.
func (s *Service) SendSMTPMail(mail *models.Mail) error {
err := smtp.SendMail(
s.mailServerAddr,
s.mailAuth, // in this case mailAuth is nil
s.emailSender.Address,
mail.Recipients,
[]byte(s.buildMessage(mail)),
)
return err
}
And this is the dockerfile
#BUILD STAGE
FROM golang:alpine AS builder
LABEL stage=builder
WORKDIR /mail_service
RUN apk add --no-cache git
COPY . .
RUN go get -d -v ./...
RUN go build -o ./mail_service
# --------------------------------------------------
#FINAL STAGE
#FROM centos:latest
FROM alpine:3.9
RUN apk --no-cache add ca-certificates \
curl
RUN adduser -D -g 'gouser' gouser && \
mkdir -p /mail_service && \
chown -R gouser:gouser /mail_service
WORKDIR /mail_service
COPY --from=builder /mail_service .
USER gouser
HEALTHCHECK --interval=30s --timeout=5s --retries=3 --start-period=1m CMD curl -f http://localhost:8080/mail-service/healthcheck || exit 1
EXPOSE 8080 587
ENTRYPOINT ./mail_service
Lastly, this is the docker-compose file, I run it with docker stack deploy -c docker-compose.yml mail_stack
version: '3.7'
services:
mailer:
image: mail_service
ports:
- "8080:8080"
- "25:25"
- "587:587"
deploy:
replicas: 2
depends_on:
- db
networks:
- mail_net
db:
image: mail_service_db
ports:
- '4000:3306'
volumes:
- "mail_v_data:/var/lib/mysql"
networks:
- mail_net
volumes:
mail_v_data:
networks:
mail_net: