Today, we will build PHP Image from Scratch in order to deploy base PHP applications easily. Please keep in mind, this is development purposes for beginners. We will publish new article for production purposes.
Let’s get started.
Preparing Dockerfile for base PHP applications
Firstly, create Dockerfile to list configuration. At the beginning of the page, we start by adding this line.
This enables us to install php.
Secondly, we add a new line to use files.
COPY . .
So that you can access every files in main root. However, if you wish use to only specific files, you can specify them manually like:
COPY ./index.php ./
Thanks to this, you can just move specific files and they are available to use.
In the next step, we expose desired port to run php application.
At the end of the file, we can run our app by executing following command line.
CMD ["php", "-S", "0.0.0.0:3000"]
To make life easier for you, you can get the file entirely.
FROM php COPY . . EXPOSE 3000 CMD ["php", "-S", "0.0.0.0:3000"]
Installing docker image
Executing this command line, you can get php image.
docker build . -t w3cloudhub/php
Running docker image
docker run --name=php -p=3000:3000 w3cloudhub/php
Congratulations! 🥳 If you see following line in the CMD, your dockerfile and image are ready to use.
PHP 8.2.0 Development Server (http://0.0.0.0:3000) started
Thank you for reading. Please leave a comment about your feelings and any issues.