Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker with php built-in server

Tags:

php

docker

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.

Here's a simple Dockerfile I build on:

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? 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. PHP built-in server seems to be hidden from the host, thus not able to serve any requests though.

Anyone was successful making this work?

like image 703
Michal Zimmermann Avatar asked Aug 31 '14 11:08

Michal Zimmermann


1 Answers

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.

To be able to communicate with your webserver from the docker host you need to make two changes:

  • in your Dockerfile, add EXPOSE 8080, or when running the container add -p 8080 to the docker run command line. This will tell the docker host that your container has a program which expects communication on port 8080
  • start the webserver with php -S 0.0.0.0:8080 so it also accepts connections from outside of the docker container itself

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. 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. Then query your webserver to validate you can communicate with it:

$ curl http://localhost:49153

like image 104
Thomasleveil Avatar answered Nov 15 '22 06:11

Thomasleveil