Docker环境下多Laravel项目PHP-FPM端口占用(Address already in use)问题排查求助
Docker环境下多Laravel项目PHP-FPM端口占用(Address already in use)问题排查求助
各位大佬好!我现在在做一个包含3个独立Laravel项目的应用,想通过单个Nginx Docker容器反向代理这3个Laravel API,但遇到了PHP-FPM端口占用的棘手问题,折腾好几天都没找到根因,来求助大家!
我的项目结构
项目根目录结构如下:
/project-folder ├── auth_service/ # Laravel认证服务 ├── appointment_service/ # Laravel预约服务 ├── hospital_service/ # Laravel医院服务 ├── gateway/ # Nginx网关配置目录 └── docker-compose.yml
各服务的配置细节
1. 服务的Dockerfile(以auth_service为例,其他服务仅端口不同)
每个Laravel服务都基于php:8.2-fpm构建,我特意修改了PHP-FPM默认的9000端口,比如auth_service改成9001,appointment_service改成9002:
FROM php:8.2-fpm WORKDIR /var/www RUN apt-get update && apt-get install -y \ build-essential \ libpng-dev \ libjpeg-dev \ libonig-dev \ libxml2-dev \ zip \ unzip \ curl \ git \ iproute2 procps net-tools \ && docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd COPY --from=composer:2.6 /usr/bin/composer /usr/bin/composer COPY . /var/www # 修改PHP-FPM监听端口为9001 RUN sed -i 's/9000/9001/' /usr/local/etc/php-fpm.d/zz-docker.conf COPY ./php-fpm.conf /usr/local/etc/php-fpm.d/www.conf RUN chown -R www-data:www-data /var/www && chmod -R 755 /var/www EXPOSE 9001 CMD ["php-fpm", "-F"]
2. Nginx网关配置(gateway/default.conf)
我在Nginx里配置了路径路由,不同前缀的请求反向代理到对应服务:
server { listen 80; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log warn; location /auth { proxy_pass http://auth_service:9001; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Authorization $http_authorization; } location /appointment { proxy_pass http://appointment_service:9002; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Authorization $http_authorization; } }
3. docker-compose.yml配置
所有服务都加入同一个自定义网络,Nginx容器依赖其他服务启动:
version: '3.8' services: auth_service: build: context: ./auth_service container_name: auth_service networks: - app-network appointment_service: build: context: ./appointment_service container_name: appointment_service networks: - app-network nginx_gateway: image: nginx:latest container_name: nginx_gateway volumes: - ./gateway/default.conf:/etc/nginx/conf.d/default.conf - ./logs/nginx:/var/log/nginx ports: - "80:80" depends_on: - auth_service - appointment_service networks: - app-network networks: app-network: driver: bridge
遇到的核心问题
不管我给每个服务分配的端口是9001、9002还是其他未被占用的端口,启动时总会出现端口被占用的错误,PHP-FPM启动失败日志如下:
# php-fpm -F -O [06-May-2025 01:36:13] NOTICE: Failed implicitly binding to ::, retrying with 0.0.0.0 [06-May-2025 01:36:13] ERROR: unable to bind listening socket for address '9001': Address already in use (98) [06-May-2025 01:36:13] ERROR: FPM initialization failed
我已尝试的排查操作
- 确保每个服务的PHP-FPM端口完全不重复(9001/9002/9003)
- 反复检查Dockerfile里的端口修改配置,确认
zz-docker.conf和自定义的www.conf都已更新端口 - 清理Docker镜像缓存,重新构建所有服务镜像后启动,问题依然存在
临时解决方法(编辑补充)
后来我换成了Laravel Octane替代PHP-FPM,不仅解决了端口占用的问题,API响应速度还提升了不少,但我还是想搞清楚原来的PHP-FPM配置哪里出了问题,或者有没有正确的配置方式可以避免这个问题?
感谢各位大佬的指点!
内容来源于stack exchange
相关产品推荐
相关产品推荐

