- Phpstorm Docker Xdebug Not Working
- Phpstorm Laravel Docker Tutorial
- Phpstorm Laravel Dockers
- Phpstorm Xdebug Docker Cli
- Phpstorm Docker Compose
- Phpstorm Xdebug Docker Laravel
Created at 2020-10-10Updated at 2020-10-11Category Docker Tag Resource / Docker / PHP / PhpStorm / PhpUnit / Linux
I have recently configured my windows 10 laptop with an additional SSD, so I could experiment with Linux. I have already installed Pop!_OS Git, PhpStorm and Docker. I haven’t installed PHP or Composer locally. Next I want to learn how to use this new environment. This is what I have found out so far.
$ cd $ docker build -t laravel-tutorial. The final step is to run the container you have just built using Docker: $ docker run -it -p 8000:8000 laravel-tutorial The command tells Docker to run the container and forward the exposed port 8000 to port 8000 on your local machine. PhpStorm can be setup to use Docker. Thanks to Gary Hockin’s excellent YouTube video Running PHPUnit Tests in PhpStorm with Docker, the setup process can be easily replicated. There is a four stage process: Configure PhpStorm to use Docker.
Start with a Project
One of my favorite projects is the Gilded Rose Kata. I can clone that from github as follows:
Create docker-compose.yml
It is possible to run PHP cli without a docker-compose file, I have found it is easier to set up PhpStorm using this intermediate step.
PhpStorm has several preconfigured Docker containers, source:
They can be used as follows:
Php 7.3 CLI and XDebug 2.7
docker-compose.yml
The above will work for Linux, for Windows and MacOS the XDEBUG_CONFIG:
will need the changed as follows:
Windows and MacOS
Windows and MacOS replace with XDEBUG_CONFIG:host.docker.internal
, which will automatically resolve to the internal address of the host Docker is running on.
MacOS with local Homebrew php-fpm
If you use a local Homebrew php-fpm installation, port 9000 (which is the default debugging port) may become occupied. PhpStorm will remain silent on starting listening for incoming connections. If this is the case, in the Settings | Languages & Frameworks | PHP | Debug, set the Debug port to 9001, and use the following configuration line instead.
Apache, PHP 7.3, XDebug 2.7 and MySQL
For information this is the LAMP version (based on the phpstorm-workshop).
docker-compose.yml
Install dependencies using composer
To keep things simple the composer Docker container can be used to install the dependencies.
This is the script recommended on the docker hub composer page to avoid filesystem permissions problems
By default, Composer runs as root inside the container. This can lead to permission issues on your host filesystem. You can work around this by running the container with a different user:
On windows change $PWD
for the full path to the project (note: forward slash / as separator), remove the line end and run the command as one line:
Alternatively, more complex projects will need specific PHP extensions to be installed, which are not included in the Composer Docker container. The following method could be used to install Composer, inside the container and install the dependencies.
- Access bash in the php-cli container:
docker-compose run --rm php-cli /bin/bash
- Install Composer, by following the download instructions for Linux
- Still, inside the container, install dependencies:
php composer.phar install
- Exit the container
exit
Note: In Linux, using the second method Composer will create the vendor folder as root!
The permissions can be changed using chown:
Further information
There is a detailed description about running Docker containers as current host user.
The official documentation on Docker run and docker-compose cli reference.
Configure PhpStorm
Now the project has been cloned from GitHub and the dependencies have been installed. PhpStorm can be setup to use Docker. Thanks to Gary Hockin’s excellent YouTube video Running PHPUnit Tests in PhpStorm with Docker, the setup process can be easily replicated.
There is a four stage process:
- Configure PhpStorm to use Docker
- Configure the remote interpreter
- Configure PhpUnit
- Create Test runner
1. Configure PhpStorm to use Docker
- Settings (Ctrl + Alt + S)
- Search for Docker
- Under Build, Execution, Deployment
- Click + to add
- Select Unix socket
- Confirm connection was successful
2. Configure the default CLI interpreter
- Settings (Ctrl + Alt + S)
- Search for CLI interpreter
- Under Language & Frameworks > PHP
- Click the ellipse button next to CLI Interpreter
- Click +
- Select From Docker, Vagrant…
- Choose Docker Compose
- Choose the Service from the drop down list (e.g. php-cli)
- Select OK
- Change the name e.g. Docker PHP
- Apply and OK
- Check the mapping
- e.g. for a web project <Project root>→/var/www/html
- e.g. for an app project <Project root>→/app
3. Configure PhpUnit
- Settings (Ctrl + Alt + S)
- Search for Test Frameworks
- Under Language & Frameworks > PHP
- Click +
- Select PhpUnit from remote interpreter
- Choose the interpreter created above, e.g. Docker PHP
- Confirm the path mappings, as above <Project root>→/app
- Input the script path based on the mapping inside the container e.g. /app/vendor/autoload.php
- Under Test runner, tick Default configuration script, type in the path, in the docker container. e.g. /app/phpunit.xml
4. Create the test runner
- Click Edit Configuration (next to run test button)
- Click + to add
- Select PHPUnit
- Under Test Runner choose Defined in the configuration file
- Name - e.g. Docker PHPUnit
- Click Play to run all the tests!
What about configuring xDebug?
Thanks to this setup, xDebug has been automatically configured! It will use the default PHP Interpreter, which was configured in step 2. A breakpoint can be set in the app or tests can be run with coverage :)
Enjoy the kata!
Edit: Added details on running commands on MacOS and Windows and small tweaks.
Prerequisites
Before you start, you need to have some certain prerequisites in place:
- PHP 7.2+
- Local machine running the latest version of Docker.
Get Started
Let's create a Laravel project and then see how we can dockerize it using Docker.
Laravel Requirements
The Laravel framework has a few system requirements:
- PHP >= 5.5.9
- PHP Extensions:
- OpenSSLup
- PDO
- Mbstring
- Tokenizer
Step 1 - Install Laravel
Laravel utilizes Composer to manage its dependencies. So, before using Laravel, make sure you have Composer installed on your machine.
Install Composer
Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. To a install it quickly, just open up a terminal and run this command:
Composer is multi-platform and it run equally well on Windows, Linux and macOS. Here you will get more information regarding how to install composer in each platform:
Step 2 - Create a Laravel Project
Via Laravel Installer
First, download the Laravel installer using Composer:
Once installed, you can use the laravel new
command to create a fresh laravel project.
Via Composer Create-Project
Alternatively, you may also install Laravel by issuing the Composer create-project
command in your terminal:
Here is a brief breakdown of the directories in a common Laravel application:
- app/: This is the source folder where our application code lives. All controllers, policies, and models are inside this folder
- bootstrap/: Holds the application's startup script and a few class map files
- config/: Holds the app's configuration files. These are usually not modified directly but instead, rely on the values set up in the .env (environment) file at the root of the app
- database/: Houses the database files including migrations, seeds and test factories
- public/: Publicly accessible folder holding compiled assets and of course an index.php file
- resources/: Contains front-end assets such as javascript files, language files, CSS/SASS files and all templates used in the application (called blade templates)
- routes/: All routes in the application are inside here. There are a few different 'scopes' of routes but the one we will be focusing on is the web.php file
- storage/: All temporary cache files used by the application, session files, compiled view scripts and log files
- tests/: Contains test files for the application such as unit tests and functional tests.
- vendor/: All dependency packages installed with composer
Then, to install all dependencies and run the project, you need to run the following commads:
You will see your Laravel server running on: http://127.0.0.1:8000
Congratulations, you’ve created a Laravel project!
Note: For more extensive instructions on setting up Laravel, refer to the Laravel documentation here.
Step 3 - Dockerize the Project
Setup Docker
Phpstorm Docker Xdebug Not Working
Before creating a container for the Laravel application and shipping it off, you need to install Docker on your local machine. For learning purpose, you will install Docker Community Edition. Select your OS from the list below and follow the setup instructions:
Make the docker App image
The next stage to improve your docker php development workflow is adding a Dockerfile
to your project. The structure of a Dockerfile
can be considered a series of instructions on how to build your container/image.
Start the Dockerfile by creating an empty file named Dockerfile
in the root of your project. Then, complete each instruction according to the following example:
Building and Running the Container
Building the container is very straight forward once you have Docker and Docker Machine on your system. The following command will look for your Dockerfile
and download all the necessary layers required to get your container image running. Afterwards, it will run the instructions in the Dockerfile
and leave you with a container that is ready to start.
To build your php laravel docker container, you will use the docker build
command and provide a tag or a name for the container, so you can reference it later when you want to run it. The final part of the command tells Docker which directory to build from.
The final step is to run the container you have just built using Docker:
The command tells Docker to run the container and forward the exposed port 8000 to port 8000 on your local machine. After you run this command, you should be able to visit http://localhost:8000 in your browser.
You can see the Docker containers that are currently running on your system (along with their Container IDs) with:
To turn off your Docker container, run:
Push to cloud
1. Create your app
In order to install your docker php example, just create a new app via cli or admin panel and set a port to 8000.
2. Push your docker container
3. Set up resources
4. Logs and app status
Phpstorm Laravel Docker Tutorial
5. Release your app
Phpstorm Laravel Dockers
After to click on the release button, your php docker tutorial will be deployed, Just click on the generated URL and you will get your app running.
Now you can deploy your php laravel app without a massive build time.
Bonus 1: SSL certificate for HTTPS
Phpstorm Xdebug Docker Cli
It's already done for you. If you need to connect your custom domain, SSL certificate will be provided for it.
Phpstorm Docker Compose
Bonus 2: Autoscaling
With autoscaling the app will be scaled up when CPU and RAM load goes up and scaled down when it goes down.
Phpstorm Xdebug Docker Laravel
Now you can deploy your php laravel app without a massive build time.