Working with PhpStorm and Docker on Windows
If you are web-developer then you probably used (or still using) some tool that emulating web-server environment. Unfortunately a lot of Windows users still using WAMP or OpenServer.
This is a very great example of horrible idea. Just because real web-servers based on Linux systems. In addition, as you might know – PHP behavior on Windows differs with Linux’s PHP. This will cause many problems with directory path separators, extensions etc. Therefore, some people decided to use Vagrant as emulator of real Linux web-server. However, Vagrant tool based on a Virtual Box machine that need a lot of resources of your computer such as processor’s time, RAM and hard drive. Fortunately, now we have Docker on Windows that uses Hyper-V feature. With Docker you can finally decrease at least CPU and hard drive usage.
So I hope you are using at least PhpStorm because we are going to setup our project with Docker integrated tool on Windows 10.
One important thing you have to know: you have to enable "Expose daemon on tcp://..." feature in the Docker's settings.
PhpStorm will use port 2375 over TCP to communicate with Docker.
Another one important setting for Docker is enabling drive sharing. We need to enable sharing on disk where our project is located. This option has to be enabled to synchronize our project files with virtual machine in Docker.
Configuring Docker plugin
Once you configured Docker – you have to enable Docker integration in the PhpStorm. Just go to setting and add new Docker connection:
Then you have to create Dockerfile in your PhpStorm project. In most cases, you might need just a ready-to-use web-server. So if you don’t know how to write this one I created universal Dockerfile for this purpose:
Ok. Now let's explain what that instruction file do.
Our OS will be Debian. It will contain nginx, PHP7.1 (FPM) with composer, MySQL and it will be accessible via SSL (443 port) and standard 80 port at the address mywebsite.dev.
Please note that if we want to access our website via mywebsite.dev – then we have to add this alias into C:\Windows\system32\drivers\etc\hosts.
Also we'll have ability to connect to MySQL from our computer outside the Docker. Also as you might see our project has name "mywebsite". Of course you can change it to yours.
Other configuration files for docker container
Next step we have to create configuration files that will be copied to our system in Docker. Here is configuration for nginx website. Please note that we are redirecting all request to index.php:
Here is configuration for MySQL (MariaDB) 50-server.cnf:
And three configuration files for PHP:
php.ini (just copy and paste from default settings and add lines at the bottom of the file):
The parameters above will enable xdebug extension.
All right! That's all of configs!
Docker deployment settings
Now we have to setup Docker deployment in the PhpStorm for our project. Go to the Run/Debug configuration in the PhpStorm and add new Docker Deployment:
In the Deployment tab, you have to name your deployment:
In the Container tab you have to specify ports and volume bindings.
Please note that in some versions of PhpStorm path mapping displaying wrong directory separators. So if your project located at D:\projects\myproject.dev then you have to type it like /d/projects/myproject.dev
That's all. After configuring your project, you'll be able to deploy it via Run-button:
Please note that you don’t need to redeploy project each time you've changed something in the code. You just have to run container that you need. And if you want to log into the bash you have to choose "Exec" option in the menu of container and then type "bash":
Success! You did it! I hope this article was helpful for you. If you need more answers – feel free to contact me.