dr9379 2019-08-15 20:18
浏览 850

无法在Docker容器中发送邮件

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:
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 matlab计算中误差
    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料
    • ¥15 使用R语言marginaleffects包进行边际效应图绘制
    • ¥20 usb设备兼容性问题
    • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊