I would like to target this article at beginners because I havenβt been able to find a proper article about a simple container for PHP. I want to give a heads-up to nitpickers in advance that Iβm focusing only on the simplest approach.
Please note that I am working on Windows, but aside from a few minor details, the process is the same on macOS or Linux.
The premise is to have Docker installed. I wonβt cover this in the article, as it depends on your operating system. However, itβs not too complicated.
Additionally, I have one recommendation for Windows users: use WSL2. The instructions are available Microsoft: How to install Linux on Windows with WSL, and based on my experience, installation is just a matter of following a few commands from the page. If you carefully follow the steps one by one, youβll get it done successfully.
If everything is ready, we can dive in.
-
Start Docker
- Icon that indicates whether the Docker is running
- Icon that indicates whether the Docker is running
- Launch your favorite PHP editor/IDE β I'm using IntelliJ PHPStorm
-
Create a new project. I place project into Ubuntu directories (see the WSL2 installation above)
- I created mine in the following directory:
\\wsl.localhost\Ubuntu\home\development\docker-php-simple
- I created mine in the following directory:
-
In the project directory, create the following files and folder structure
-
Now, let's add content to these three files:
-
public/index.php: Prepare a nice piece of code that welcomes you :-)
-
<?php
echo 'Hello, Developer!';
-
.docker/apache/sites-available/000-default.conf: A simple configuration that specifies the default directory of our website
<VirtualHost *:80>
DocumentRoot "/var/www/html/public"
</VirtualHost>
-
docker-compose.yml: Configuration settings for the Docker containers
services:
app:
image: php:8.4-apache
container_name: docker-php-simple
tty: true
volumes:
# Mount the current directory to the /var/www/html directory in the container
# It means that the files in the current directory (./) will be available in the container at /var/www/html
- ./:/var/www/html
# Apache configuration file for the virtual host
- ./.docker/apache/sites-available/000-default.conf:/etc/apache2/sites-available/000-default.conf
# working_dir: /var/www/html
ports:
# Your web server will be available at http://localhost:8080
- "8080:80"
Let's get it running!
Follow these steps based on your preferred method:
Option 1: Using the Docker Plugin in Your Editor
- Open the
docker-compose.ymlfile in your editor. - Look for the double arrows (in PHPStorm) (βΆοΈβΆοΈ) in the interface and click them.
This will start your Docker containers. The starting process and result is visible in IDE's console.
Option 2: Using a Terminal
- Open your terminal (e.g., Git Bash, Command Prompt, or any terminal you prefer).
- Navigate to your project directory
- Run the following command to start the containers:
docker-compose up -d
Once the containers are up, your application should be running.
You can open the Docker application, and you should see a running container with your application, named docker-php-simple.
If everything is set up correctly, the container should have a "Running" status, and you can manage it directly from the Docker dashboard, such as stopping, restarting, or inspecting logs if needed. π

And as the grand finale, here is our application in action! π
Check it in your browser by accessing the appropriate URL http://localhost:8080/. π
You should see your welcome message Hello, Developer!


Top comments (0)