PHP Development with Docker and VSCode

Nowadays it is a must-have skill to use docker for your development. To start the development with docker here are some prerequisites:

  • If you’re on Windows you have WSL installed on your system. It will also be required for Docker.
  • You have Docker installed on your system.
  • You have Visual Studio Code installed with the following extensions
  • An interface to connect to a database like MySQL

Now let’s start a sample PHP project to develop with docker.

  • Create directory php-base-docker
  • Open this directory in VSCode. If you’re on a Windows system it is recommended to use WSL
  • Create file index.php and type the code as shown in the screenshot below
  • Now press Ctl+Shift+P, search for ‘Dev Containers: Open Folder in Container…’ and hit enter.
  • Select your current directory
  • In the next step at ‘Add Dev Container Configuration Files’ search and select ‘PHP & MariaDB’
  • Select the PHP version. 8.2-bullseye is PHP 8.2 version.
  • Then select any features to install if you want or leave empty and hit OK
  • It will create a directory .devcontainer and a few files in it
  • You can check that now VSCode has opened this directory in container mode. If you open the terminal and run the command uname you will see that you’re already inside the container
  • In the terminal run the following command

sudo chmod a+x "$(pwd)" && sudo rm -rf /var/www/html && sudo ln -s "$(pwd)" /var/www/html

  • Then run the following command to check if Apache web server is running

service apache2 status

  • If it is not then run the following command to run the Apache server

service apache2 start

  • In the Ports tab you can see what ports are being forwarded so you can connect to them from your local system

It is possible that you need URL rewriting enabled in apache. To do so run the following commands.

sudo a2enmod rewrite

service apache restart

  • You can connect to database using your favorite client or you can install any related extension
  • You can connect to this MariaDB database server using mariadb as username and password and connect to localhost on port 3306.
  • You can also debug the code. Press Ctrl+Shift+D to start the debugger.

For further reading see the references section below.

References:

Share your love
Muhammad Jawaid Shamshad
Muhammad Jawaid Shamshad
Articles: 128

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.