简体   繁体   English

Docker 与 php 内置服务器

[英]Docker with php built-in server

I'm trying to run php built-in server ( php -S localhost:8080 ) via docker, I cannot access site from the host though - I always end up with Connection reset.我正在尝试通过 docker 运行 php 内置服务器 ( php -S localhost:8080 ),但我无法从主机访问站点 - 我总是以连接重置结束。

Here's a simple Dockerfile I build on:这是我构建的一个简单的 Dockerfile:

FROM centos:centos6

RUN rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm
RUN rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
RUN rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

RUN yum --enablerepo=remi,remi-php55 install -y php php-opcache php-cli php-pear php-common && yum clean all
RUN php -r "readfile('https://getcomposer.org/installer');" | php
RUN echo "date.timezone = Europe/Prague" >> /etc/php.ini
RUN mv composer.phar /usr/bin/composer
RUN php -r "eval('?>'.file_get_contents('http://backend.bolt80.com/piecrust/install'));"
RUN mv piecrust.phar /usr/bin/chef

CMD ["/bin/bash"]

Is it even possible to run this server with docker?甚至可以用 docker 运行这个服务器吗? While trying to make it work, I found out that when nginx was installed and set to listen on this very port, it is accessible from the host.在尝试让它工作时,我发现当 nginx 安装并设置为监听这个端口时,它可以从主机访问。 PHP built-in server seems to be hidden from the host, thus not able to serve any requests though. PHP 内置服务器似乎对主机隐藏,因此无法处理任何请求。

Anyone was successful making this work?有人成功地完成了这项工作吗?

If from within the docker container you start your webserver with php -S localhost:8080 then the webserver will only accept connections originating from the docker container itself. 如果从Docker容器中使用php -S localhost:8080启动Web服务器,则Web服务器将仅接受源自Docker容器本身的连接。

To be able to communicate with your webserver from the docker host you need to make two changes: 为了能够从Docker主机与Web服务器通信,您需要进行两项更改:

  • in your Dockerfile, add EXPOSE 8080 , or when running the container add -p 8080 to the docker run command line. 在Dockerfile中,添加EXPOSE 8080 ,或者在运行容器时将-p 8080添加到EXPOSE 8080 docker run命令行。 This will tell the docker host that your container has a program which expects communication on port 8080 这将告诉Docker主机您的容器具有一个程序,该程序希望在端口8080上进行通信
  • start the webserver with php -S 0.0.0.0:8080 so it also accepts connections from outside of the docker container itself 使用php -S 0.0.0.0:8080启动Web服务器,因此它也接受来自Docker容器本身外部的连接

Once you have a container running with those changes, open up a new terminal on the docker host and use the docker ps command to see what port on the docker host is forwarded to port 8080 in the container. 一旦容器运行了这些更改,请在docker主机上打开一个新终端,并使用docker ps命令查看docker主机上的哪个端口转发到容器中的端口8080。 For instance: 例如:

$ docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                     NAMES
fbccf4058b07        test:latest         "php -S 0.0.0.0:8080   4 minutes ago       Up 4 minutes        0.0.0.0:49153->8080/tcp   sad_hawking

In this example port 49153 of the docker host is to be used. 在此示例中,将使用Docker主机的端口49153 Then query your webserver to validate you can communicate with it: 然后查询您的网络服务器以确认您可以与其通信:

$ curl http://localhost:49153

For docker compose对于docker compose

services:
  api:
    build: ./api
    command: php -S 0.0.0.0:80
    ports:
      - 80:80

with dockerfile, placed in./api folder同dockerfile,放在./api文件夹

FROM php:apline
WORKDIR /
COPY . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM