DEV Community

Cover image for Docker for PHP begginers as simple as possible
Martin Jirasek
Martin Jirasek

Posted on

Docker for PHP begginers as simple as possible

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.

  1. Start Docker
    • Icon that indicates whether the Docker is running Image description
  2. Launch your favorite PHP editor/IDE – I'm using IntelliJ PHPStorm
  3. Create a new project in Ubuntu (see the WSL2 installation above)
    • I created mine in the following directory: \\wsl.localhost\Ubuntu\home\development\docker-php-simple Image description
  4. In the project directory, create the following files and folder structure Image description
  5. 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!';

Enter fullscreen mode Exit fullscreen mode
  • .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>
Enter fullscreen mode Exit fullscreen mode
  • 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"
Enter fullscreen mode Exit fullscreen mode

Let's get it running!

Follow these steps based on your preferred method:

Option 1: Using the Docker Plugin in Your Editor

  1. Open the docker-compose.yml file in your editor.
  2. 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.

Image description

Option 2: Using a Terminal

  1. Open your terminal (e.g., Git Bash, Command Prompt, or any terminal you prefer).
  2. Navigate to your project directory
  3. Run the following command to start the containers:
   docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

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. 🎉
Image description


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)