The Missing Web Server in Your Docker Container
You have a simple index.php that echoes "Hello World" and a Dockerfile built from the official PHP samples. You build the image, run the container mapping port 7000 to the host, and navigate to http://localhost:7000/. The browser shows only the raw text output of the script instead of rendering it as an HTML page. This happens because your container lacks a web server to serve files over HTTP.
The Dockerfile provided in the examples uses the php:7.2-cli base image and executes the command php ./index.php. This approach runs PHP directly as a command-line interpreter. When you execute docker run -p 7000:80 --rm --name hello-world-test my-php-app, you are essentially spinning up a script that prints output to standard output (stdout) and exits immediately. The port mapping -p 7000:80 connects the container's port 80 to your host's port 7000, but since no process is listening on that port inside the container, there is nothing for your browser to connect to.
To make a PHP script accessible via a URL, you need a web server like Apache or Nginx running inside the container. The official documentation demonstrates this by suggesting the use of php:7.2-apache as the base image instead of php:7.2-cli. This image includes the Apache HTTP Server pre-configured to listen on port 80 and serves content from the /var/www/html directory.
Here is an updated Dockerfile that aligns with standard practices for serving static PHP files:
FROM php:7.2-apache
COPY index.php /var/www/html/
EXPOSE 80
CMD ["apache2-foreground"]
In this configuration, the COPY command places your file in the correct directory where Apache expects to find it. The EXPOSE directive documents that the container uses port 80, and the CMD instruction starts the Apache process using the foreground flag, which keeps the container running as long as the server is active. You can still map this to host port 7000 using the same -p 7000:80 flag when running the container.
Alternatively, if you prefer not to switch base images, you can install Apache within the CLI image and configure it manually. This approach adds more steps but gives you granular control over the server configuration. You would need to install apache2, copy your default config files, and adjust the document root to point to your application directory.
Regardless of the method chosen, the critical distinction lies in how PHP is executed. The CLI image treats PHP as a tool for scripting tasks or command-line tools, whereas the Apache image treats it as a web application framework running behind a server. If you are developing a full-stack application with multiple routes and dynamic behavior, consider leveraging Laravel's built-in development server or deploying a production-ready stack like LEMP (Linux, Nginx, MySQL, PHP). For more complex setups involving frameworks like Laravel, the deployment strategy often involves using php-fpm with Nginx to handle static assets separately from the PHP process.
Understanding these architectural differences ensures that your container not only runs but serves content correctly over HTTP.
