Home Account

How to make a docker image with the php built-in server

2022-07-11 12:12 dennis iversen

Tags: php docker

Basic example of making a docker image containing a built-in php server.

Build a container

Let's make it inside a test dir.

mkdir test
cd test

Make a file named Dockerfile inside this test directory with the following content:

FROM php:8.1-cli
RUN docker-php-ext-install pdo_mysql
COPY . /app
WORKDIR /app
CMD [ "./serv"]
EXPOSE 8000

The above Dockerfile adds the extension pdo_mysql to the docker image. You may add other extensions.

Now build the image from the current directory:

docker build -t php-server .

Run the container

You will notice the CMD in the above is ./serv. This is the entrypoint when the container is run.

This could look something like this:

#!/bin/sh

php -S localhost:8000 -t .

Make this file executable:

chmod +x ./serv

Save the serv command inside the test dir (or inside any other dir where you will run the image).

Also make an index.php file inside the test dir (or inside any other dir where you will run the image) :

<?php

echo "Hello world!";

Now run the image in this directory:

docker run -it -v $PWD:/app --network=host --rm --name test-server php-server

Now you can vist http://localhost:8000

This page has been requested 315 times