Create your own PHP application with built-in PHP web server
Go to Project 3 from the git repository root:
cd projects/p03
Project structure:
.
├── Dockerfile
└── www
└── index.php
The content of the index.php
<?php
file_put_contents(__DIR__ . '/access.txt', date('Y.m.d. H:i:s') . "\n", FILE_APPEND);
echo 'P03<br/>';
echo nl2br(file_get_contents(__DIR__ . '/access.txt'));
and the Dockerfile
FROM php:7.4-alpine
LABEL hu.itsziget.ld.project=p03
COPY www /var/www
CMD ["php", "-S", "0.0.0.0:80", "-t", "/var/www"]
# CMD php -S 0.0.0.0:80 -t /var/www
# is the same as
# CMD ["sh", "-c", "php -S 0.0.0.0:80 -t /var/www"]
Build an image:
docker build -t localhost/p03_php .
Start the container:
docker run -d --name p03_php -p "8080:80" localhost/p03_php
Open in a web browser and reload the page multiple times. You can see the output is different each time with more lines.
Now delete the container. Probably you already now how, but as a reminder I show you again:
docker rm -f p03_php
Execute the “docker run …” command again and reload the example web page to see how you have lost the previously generated lines and delete the container again.
Now start the container with a volume to preserve data:
docker run -d --mount source=p03_php_www,target=/var/www --name p03_php -p "8080:80" localhost/p03_php
This way you can delete and create the container repeatedly a you will never lose your data until you delete the volume. You can see all volumes with the following command:
docker volume ls
# or
docker volume list
After you have deleted the container, you can delete the volume:
docker rm -f p03_php
docker volume rm p03_php_www