Laravel mailcatcher 抛出异常无法分配请求的地址

Laravel with mailcatcher thrown exception Cannot assign requested address

我在将 mailcatcher 与 laravel ( "laravel/framework": "^8.54",)

一起使用时遇到错误
"message": "Connection could not be established with host localhost :stream_socket_client(): unable to connect to localhost:8003 (Cannot assign requested address)",
  "exception": "Swift_TransportException",
  "file": "/var/www/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php",
  "line": 261,

所以,我像往常一样发送电子邮件,我检查了使用的默认邮件 smpt

config/mail.php

'mailers' => [
    'smtp' => [
        'transport' => 'smtp',
        'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
        'port' => env('MAIL_PORT', 587),
        'encryption' => env('MAIL_ENCRYPTION', 'tls'),
        'username' => env('MAIL_USERNAME'),
        'password' => env('MAIL_PASSWORD'),
        'timeout' => null,
        'auth_mode' => null,
    ],

在代码中

use Illuminate\Support\Facades\Mail;
//

Mail::send((new ContactUsEmail($contactUs)));

我的.env

MAIL_MAILER=smtp
MAIL_HOST=localhost
MAIL_PORT=8003
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=from_admin@admin.com
MAIL_TO_ADDRESS=to_admin@admin.com
MAIL_FROM_NAME="${APP_NAME}"
MAIL_TO_NAME=admin

当我调试它时 \Swift_Transport_StreamBuffer

params = {array} [8]
 protocol = ""
 host = "localhost"
 port = {int} 8003
 timeout = {int} 30
 blocking = {int} 1
 tls = false
 type = {int} 1
 stream_context_options = {array} [0]

看起来端口正确

当我打开 http://localhost:8100/

时,mailcatcher 可用

docker-编写yml:

php-larabostad:
    build:
        context: .
        dockerfile: ./php/Dockerfile
    restart: on-failure
    volumes:
        - "../:/var/www"
        - "./php/php.ini:/usr/local/etc/php/conf.d/custom.ini"
        - "./data/php:/var/larabostad:z"
    networks:
        - php-larabostad-networks
        - mysql-larabostad-networks
        - mailcatcher-larabostad-networks


mailcatcher:
    image: schickling/mailcatcher:latest
    container_name: myapp-mailcatcher
    ports:
        - "8003:1025"
        - "8100:1080"
    networks:
        - mailcatcher-larabostad-networks

docker-撰写ps

docker_nginx-larabostad_1   /docker-entrypoint.sh ngin ...   Up      0.0.0.0:443->443/tcp,:::443->443/tcp,                            
                                                                     0.0.0.0:80->80/tcp,:::80->80/tcp                                 
docker_php-larabostad_1     bash /usr/local/bin/docker ...   Up      9000/tcp                                                         
myapp-mailcatcher           mailcatcher --no-quit --fo ...   Up      0.0.0.0:8003->1025/tcp,:::8003->1025/tcp,                        
                                                                     0.0.0.0:8100->1080/tcp,:::8100->1080/tcp                         
mysql8                      docker-entrypoint.sh mysql ...   Up      0.0.0.0:3306->3306/tcp,:::3306->3306/tcp, 33060/tcp   

我做错了什么?

更新

从 php 容器执行 ping

/var/www# ping mailcatcher
PING mailcatcher (172.23.0.2) 56(84) bytes of data.
64 bytes from myapp-mailcatcher.docker_mailcatcher-larabostad-networks (172.23.0.2): icmp_seq=1 ttl=64 time=0.237 ms
64 bytes from myapp-mailcatcher.docker_mailcatcher-larabostad-networks (172.23.0.2): icmp_seq=2 ttl=64 time=0.138 ms

新错误

  "message": "Connection could not be established with host mailcatcher :stream_socket_client(): unable to connect to mailcatcher:8003 (Connection refused)",
  "exception": "Swift_TransportException",
  "file": "/var/www/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php",
  "line": 261,

Compose 中的所有 docker 服务在一个网络中工作(默认情况下)。如果你想从一项服务连接到其他服务 - 使用他们的名字。而且,因为您连接到专用网络 - 使用标准端口

MAIL_HOST=mailcatcher
MAIL_PORT=1025