php - PHP容器在localhost上看不到我的应用程序

标签 php docker localhost containers

我想在容器上运行PHP应用程序,当我进入localhost:5000时,要查看网站。运行容器时,我看不到位于localhost:5000的网站,只能看到172.12.0.6:80的网站(我不知道这是什么IP)。

这是dockerfile:

FROM php:7.0-apache
RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf
COPY ./index.php /var/www/html/
EXPOSE 5000

这是index.php,如果您有兴趣的话(我知道它很复杂):
<html>
<title>website</title>
        <body>
                <?php
                        echo "hello world";
                ?>
        </body>
</html>

这就是我构建和运行镜像的方式:
$ sudo docker build -t website website/
$ sudo docker run -it --name website --network mynetwork  website:latest

这是运行图像时的日志:
$ sudo docker run -it --name website --network mynetwork  website:latest

[Mon Feb 25 16:08:05.912611 2019] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.25 (Debian) PHP/7.0.33 configured -- resuming normal operations
[Mon Feb 25 16:08:05.912682 2019] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'

最佳答案

您正在将容器连接到自定义网络mynetwork。因此,您需要显式公开端口80->5000:

docker run -it --name website --network mynetwork  -p 5000:80 website:latest

有了这个,你可以到达localhost:5000上的容器

另一个选择-使用network=host模式。在这种模式下,您不需要(实际上也不能)暴露端口,但是会牺牲容器的隔离性,因为容器正在使用主机的网络。
docker run -it --name website --network=host website:latest

有了这个,你可以到达localhost:80上的容器。请注意,使用network=host不能更改容器的原始端口。

Dockerfile中的EXPOSE 5000语句仅用于提供信息和doesn't actually perform port exposing:

The EXPOSE instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be publisher

关于php - PHP容器在localhost上看不到我的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54870515/

相关文章:

php - PHP 的 REST 基础知识

php - SQL 插入二进制设置为 30

ruby-on-rails - 无法通过Rails Docker,Carrierwave将文件上传到AWS S3

docker - Heroku Docker镜像部署

php - 使用 XAMPP 设置 $_SESSION 在本地主机上不起作用

php - 将 php 表打印为 pdf

php - Wordpress - 可以在数据库丢失的情况下重建吗?

azure - 如何自动化 Azure Web 应用程序部署以从 Azure 容器中选择最新图像

php - 本地主机和 Azure 云服务上的代码行为不同。为什么?

java - 端口 8080 已被 "SYSTEM"使用