Paradigm Shift Design

ISHITOYA Kentaro's blog.

How to Use mhsendmail in a Docker Container on Apple Silicon

Appleシリコンのコンテナでmhsendmailを使う方法。色々ググったんだけどmhsendmailが古すぎてAppleシリコンに対応しておらず。自前でビルドする方法を模索したログ。

When you search for methods to send emails to Mailhog in environments not using Laravel, you are often advised to use mhsendmail. However, on Apple Silicon, you encounter an error like this:

$ mhsendmail
runtime: failed to create new OS thread (have 2 already; errno=22)
fatal error: newosproc

... [Error Stack Trace] ...

Essentially, this is because it's not built for arm64.

Referring to the following documentation, https://docs.docker.com/build/guide/multi-stage/#add-stages, here is a Dockerfile intended for use with php:8.3. The reason for using git clone is that it seems go install does not support forks (though I haven't thoroughly checked):

FROM --platform=${BUILDPLATFORM} golang:1.21 AS golang
RUN apt-get update && apt upgrade -y && apt-get install -y git
WORKDIR /src
RUN git clone -b arm64 https://github.com/BKHZ/mhsendmail.git && cd mhsendmail
WORKDIR /src/mhsendmail
ENV CGO_ENABLED=0
COPY . .
ARG TARGETOS
ARG TARGETARCH
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go mod vendor
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o /usr/local/bin/mhsendmail

FROM php:8.3-apache

COPY --from=golang /usr/local/bin/mhsendmail /usr/local/bin/mhsendmail

With the above Dockerfile, now it works fine.