Apache配置 / 在Docker中无法访问localhost:8080
在Docker上尝试通过 [php8.5.3-apache] 映像来安装Moodle时遇到问题。
Here is my Dockerfile :
FROM php:8.5.3-apache
WORKDIR /var/www/html/
# COPY ./moodle.conf /etc/apache2/conf-available/moodle.conf to set up the Apache configuration for Moodle. This file should contain the necessary directives to serve the Moodle application correctly.
COPY ./moodle_listener.conf /etc/apache2/moodle_listener.conf
COPY ./moodle_listeners.conf /etc/apache2/sites-available/moodle_listeners.conf
# Installe les dépendances
RUN apt-get update && \
apt-get install -y --no-install-recommends \
git \
unzip \
libzip-dev \
libjpeg-dev \
libpng-dev \
libfreetype6-dev \
libpq-dev \
libicu-dev \
libxml2-dev \
libonig-dev && \
docker-php-ext-configure gd --with-freetype --with-jpeg && \
docker-php-ext-install -j$(nproc) zip gd pgsql pdo_pgsql intl soap
# Clone Moodle 5.1+ et configure la structure
RUN mkdir /var/www/html/moodle && \
cd /var/www/html/moodle && \
git clone -b MOODLE_501_STABLE git://git.moodle.org/moodle.git . && \
chown -R root /var/www/html/moodle/ && \
chmod -R 0755 /var/www/html/moodle/
# Create data directory
RUN mkdir -p /var/www/html/moodledata && \
chown -R www-data:www-data /var/www/html/moodledata && \
chmod -R 0777 /var/www/html/moodledata
# Ensure the default site is enabled
RUN ln -sf /etc/apache2/sites-available/moodle_listeners.conf /etc/apache2/sites-enabled/
CMD ["apache2ctl", "-D", "FOREGROUND"]
EXPOSE 8080
Here is the moodle_listeners.conf :
<VirtualHost *:8080>
Include /etc/apache2/moodle_listener.conf
</VirtualHost>
并且moodle_listener.conf就在那里,复制自 Moodle Code Restructure 文章(原始文件在 此处)。
This is the error code when I docker run -p 8080:8080 --name moodle_setup moodle5.1 :
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Tue Mar 03 14:21:37.735910 2026] [mpm_prefork:notice] [pid 8:tid 8] AH00163: Apache/2.4.66 (Debian) PHP/8.5.3 configured -- resuming normal operations
[Tue Mar 03 14:21:37.735943 2026] [core:notice] [pid 8:tid 8] AH00094: Command line: '/usr/sbin/apache2 -D FOREGROUND'
我在网上尝试了一些方法,但都没有结果。谢谢你的帮助。 仓库在这里:https://github.com/sambarnades/Moodle-5.1_PHP-8.5.3。
03/03/2026 18:32 CET+1编辑
在浏览了一些链接并考虑建议之后,我做了这些事情:
- 将所有映射到 :80端口
COPY ./moodle_listeners.conf /etc/apache2/sites-available/000-default.conf以删除apache 000-default.conf(但我想保留获得整洁代码的主要方法。)- 将DocumentRoot指向
/var/www/html/moodle/public,而不是/var/www/html/moodle
目前它起作用了!谢谢!
解决方案
From my earlier comment:
The EXPOSE 指令不会在该数字上公开或运行某项服务。相反,它更多用于文档化,显示正在运行的服务会使用该数字。
如果你想把主机机器的端口路由到本地运行的Apache容器,可以通过执行 docker run -p 8080:80 php:8.5.3-apache 来实现。
另外,在某些情况下,你可能被迫在非特权端口上运行某个容器服务。虽然有些Apache Docker镜像通过环境变量在某些情况下允许这样做,但 php:8.5.3-apache 映像似乎目前不具备这项能力。
目前,运行你的镜像的最佳方式仍然是照你现在的做法,创建你自己的自定义镜像,并把任何附加的端口监听器拷贝到 /etc/apache2/ports.conf。一旦就位,你的自定义镜像就能够对默认的 80 之外的其他端口请求做出响应。