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

Docker Compose中Apache连接PHP-FPM时DNS查询失败求助

问题:Apache容器无法解析PHP-FPM容器的DNS

使用Docker Compose搭建PHP Web应用,拆分Apache和PHP-FPM两个容器,数据库独立连接。访问http://localhost:8000/script.php时,Apache错误日志提示DNS lookup failure for: php returned by /script.php。

Apache错误日志

[proxy:debug] mod_proxy.c(1506): AH01143: Running scheme fcgi handler (attempt 0), referer: localhost:8080/application.html
[proxy_fcgi:debug] mod_proxy_fcgi.c(1054): AH01076: url: fcgi://php:9000/usr/local/apache2/htdocs/script.php proxyname: (null) proxyport: 0, referer: localhost:8080/application.html
[proxy_fcgi:debug] mod_proxy_fcgi.c(1063): AH01078: serving URL fcgi://php:9000/usr/local/apache2/htdocs/script.php, referer: localhost:8080/application.html
[proxy:debug] proxy_util.c(2538): AH00942: FCGI: has acquired connection for (*:80)
[proxy:debug] proxy_util.c(2596): AH00944: connecting fcgi://php:9000/usr/local/apache2/htdocs/script.php to php:9000, referer: localhost:8080/application.html
[proxy:error] AH00898: DNS lookup failure for: php returned by /script.php, referer: localhost:8080/application.html
[proxy:debug] proxy_util.c(2554): AH00943: FCGI: has released connection for (*:80)

现有配置文件

docker-compose.yml

version: "3.7"

services:
  apache:
    build: 
      args:
        DBHOST: ${DBHOST} 
        DBPORT: ${DBPORT} 
        DBNAME: ${DBNAME} 
        DBUSER: ${DBUSER} 
        DBPASSWORD: ${DBPASSWORD} 
      context: .
      dockerfile: ./path/to/Dockerfile
    links:
      - php
    ports:
      - "8080:8080"
  php:
    build: 
      args: 
          DBHOST: ${DBHOST} 
          DBPORT: ${DBPORT} 
          DBNAME: ${DBNAME} 
          DBUSER: ${DBUSER} 
          DBPASSWORD: ${DBPASSWORD} 
      dockerfile: ./path/to/Dockerfile
    context: .
    expose:
      - "9000"
    ports:
      - 9000:9000

PHP Dockerfile

FROM php:8.1-fpm
ARG DBHOST
ARG DBPORT
ARG DBNAME
ARG DBUSER
ARG DBPASSWORD
ENV DBHOST $DBHOST
ENV DBPORT $DBPORT
ENV DBNAME $DBNAME
ENV DBUSER $DBUSER
ENV DBPASSWORD $DBPASSWORD
RUN apt-get update

RUN apt-get install -y libpq-dev \
    && docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
    && docker-php-ext-install pdo pdo_pgsql pgsql

# I don't know exactly where they should be, so I copied the files to different locations and none solve 
# the error I'm getting
COPY . /var/www/html
COPY . /
RUN mkdir -p /usr/local/apache2/htdocs
COPY . /usr/local/apache2/htdocs

COPY ./htdocs/ /usr/local/apache2/htdocs
COPY ./htdocs/ /
COPY ./htdocs/ /var/www/html

Apache配置

<FilesMatch \.php$>
        SetHandler "proxy:fcgi://php:9000"
</FilesMatch>
修复方案

1. 修正docker-compose.yml语法错误

php服务中context和dockerfile顺序写反,Docker Compose会忽略错误配置项,导致PHP容器未正确加入默认网络,Apache无法通过服务名解析。调整为:

php:
  build: 
    args: 
        DBHOST: ${DBHOST} 
        DBPORT: ${DBPORT} 
        DBNAME: ${DBNAME} 
        DBUSER: ${DBUSER} 
        DBPASSWORD: ${DBPASSWORD} 
    context: .
    dockerfile: ./path/to/Dockerfile
  expose:
    - "9000"

Docker Compose 3+版本中links已过时,同一compose文件的服务默认在同一网络,可删除apache服务下的links: - php。

2. 统一代码目录路径

Apache文档根目录为/usr/local/apache2/htdocs,但PHP-FPM默认监听/var/www/html,导致无法找到脚本文件。两种修正方式:

  • 修改Apache代理配置,指定PHP侧路径:
<FilesMatch \.php$>
        SetHandler "proxy:fcgi://php:9000/var/www/html"
</FilesMatch>
  • 统一两边目录:在Apache Dockerfile中将文档根目录设为/var/www/html,同时简化PHP Dockerfile的文件复制:
# 删除所有重复COPY命令,只保留这一行
COPY ./htdocs/ /var/www/html

3. 验证网络连通性

重构启动容器后,进入Apache容器测试解析:

# 进入Apache容器
docker exec -it <apache容器名> sh
# 测试PHP服务解析
ping php

能ping通则网络正常,否则检查compose配置和容器启动状态。

4. 检查PHP-FPM端口监听

进入PHP容器验证端口状态:

docker exec -it <php容器名> sh
netstat -tulpn | grep 9000

确保PHP-FPM在0.0.0.0:9000监听(默认配置无需修改)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 13:35:58