PHP VS Code Docker

PHP Development in VS Code and Docker Dev Containers

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
php docker container,php vs code
  • Now press Ctl+Shift+P, search for ‘Dev Containers: Open Folder in Container…’ and hit enter.
php docker container,php vs code
  • Select your current directory
php docker container,php vs code
  • In the next step at ‘Add Dev Container Configuration Files’ search and select ‘PHP & MariaDB’
php docker container,php vs code
  • Select the PHP version. 8.2-bullseye is PHP 8.2 version.
php docker container,php vs code
  • Then select any features to install if you want or leave empty and hit OK
php docker container,php vs code
  • It will create a directory .devcontainer and a few files in it
php docker container,php vs code
  • You can check that now VS Code 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
php docker container,php vs code
  • 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

php docker container,php vs code
  • Then run the following command to check if Apache web server is running

service apache2 status

php docker container,php vs code
  • If it is not then run the following command to run the Apache server

service apache2 start

php docker container,php vs code
  • In the Ports tab you can see what ports are being forwarded so you can connect to them from your local system
php docker container,php vs code

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
php docker container,php vs code
  • You can connect to this MariaDB database server using mariadb as username and password and connect to localhost on port 3306.
php docker container,php vs code
php docker container,php vs code
  • You can also debug the code. Press Ctrl+Shift+D to start the debugger.

For further reading see the references section below.

References:

Add a Comment

Your email address will not be published. Required fields are marked *

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