我想在容器上运行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/