You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker中PHP容器部署Mosquitto报Cannot assign requested address错误

问题:PHP容器内安装Mosquitto无法启动,报错“Cannot assign requested address”

我有一个基于Docker的PHP应用,之前运行正常,现在想在PHP容器里安装Mosquitto消息代理,但启动后无法正常工作。以下是相关配置:

docker-compose.yaml

version: '3.8'

services:
    nginx:
        image: nginx:stable-alpine
        ports:
            - "8288:80"
        volumes:
            - ./:/var/www/
            - ./dockers/nginx/default.conf:/etc/nginx/conf.d/default.conf
        depends_on:
            - php
    php:
        build:
            context: .
            dockerfile: dockers/php/Dockerfile
        volumes:
            - ./:/var/www/
            - ./dockers/mosquitto/mosquitto.conf:/etc/mosquitto/conf.d/default.conf
        ports:
            - 9004:9000
            - 1883:1883
        depends_on:
            - mysql
    mysql:
        image: mysql:8.0
        container_name: access-control-mysql
        tty: true
        ports:
            - "7306:3306"
        volumes:
            - ./mysql:/var/lib/mysql
        environment:
            MYSQL_DATABASE: ${DB_DATABASE}
            MYSQL_USER: ${DB_USERNAME}
            MYSQL_PASSWORD: ${DB_PASSWORD}
            MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}

PHP容器Dockerfile

FROM 8.1-fpm

RUN apt-get update -yqq

# Install & enable Xdebug for code coverage reports
RUN pecl install xdebug imagick
RUN docker-php-ext-enable xdebug imagick

RUN apt-get install -y mosquitto libmosquitto-dev
# Set working directory
WORKDIR /var/www

# Add script file to run command
COPY scripts/cmd.sh /usr/local/bin/cmd
RUN chmod 0755 /usr/local/bin/cmd

# Change current user to www
USER www

# Expose port 9000 and start php-fpm server
EXPOSE 9000

CMD ["/usr/local/bin/cmd"]

mosquitto.conf

listener 1883
allow_anonymous true

cmd.sh

/usr/local/bin/composer install
php artisan config:cache
php artisan migrate
php artisan ide-helper:generate
php artisan ide-helper:models --nowrite
php artisan db:seed
php-fpm

启动容器后,执行docker-compose exec php mosquitto时出现以下错误:

1669015395: mosquitto version 2.0.11 starting
1669015395: Using default config.
1669015395: Starting in local only mode. Connections will only be possible from clients running on this machine.
1669015395: Create a configuration file which defines a listener to allow remote access.
1669015395: For more details see https://mosquitto.org/documentation/authentication-methods/
1669015395: Opening ipv4 listen socket on port 1883.
1669015395: Opening ipv6 listen socket on port 1883.
1669015395: Error: Cannot assign requested address
1669015395: mosquitto version 2.0.11 running

之前用独立Mosquitto容器(配置如下)时,相同的mosquitto.conf可以正常运行;也试过在mosquitto.conf的listener里指定PHP容器IP,还是报同样的错,求解决。

独立Mosquitto容器配置

mqtt:
  image: eclipse-mosquitto:latest
  ports:
    - 1884:1883
  volumes:
    - ./dockers/mosquitto/mosquitto.conf:/mosquitto/config/mosquitto.conf
    - ./mosquitto/data:/mosquitto/data
    - ./mosquitto/log:/mosquitto/log

解决方案

1. 修复配置文件加载问题

当前你将mosquitto.conf挂载到/etc/mosquitto/conf.d/default.conf,但Mosquitto默认不会自动加载该目录下的配置(除非主配置文件明确指定include_dir /etc/mosquitto/conf.d)。

直接修改挂载路径,替换默认主配置文件:
在docker-compose.yaml的php服务volumes中修改:

volumes:
  - ./:/var/www/
  - ./dockers/mosquitto/mosquitto.conf:/etc/mosquitto/mosquitto.conf

2. 解决用户权限问题

PHP容器以www用户运行,但Mosquitto需要读写/var/log/mosquitto、/var/lib/mosquitto目录,www用户默认无这些权限,会导致端口绑定失败。

在Dockerfile中添加权限配置:

RUN mkdir -p /var/log/mosquitto /var/lib/mosquitto && \
    chown -R www:www /var/log/mosquitto /var/lib/mosquitto /etc/mosquitto

3. 修正端口监听配置

错误提示无法分配地址,大概率是IPv6监听冲突。修改mosquitto.conf,指定仅监听IPv4所有地址:

listener 1883 0.0.0.0
allow_anonymous true

4. 调整启动方式

手动进入容器启动Mosquitto不是长期方案,将其加入cmd.sh,以守护进程方式启动:
修改cmd.sh:

/usr/local/bin/composer install
php artisan config:cache
php artisan migrate
php artisan ide-helper:generate
php artisan ide-helper:models --nowrite
php artisan db:seed
# 后台启动Mosquitto
mosquitto -d
php-fpm

内容的提问来源于stack exchange,提问作者simpsons3

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 05:40:21