Quantcast
Channel: Tutorial – PhpStorm Blog
Viewing all articles
Browse latest Browse all 31

Quickstart with Docker in PhpStorm

$
0
0

So, you’ve decided to try something new today and started a project from scratch. Your first step will be to set up a development environment: at the bare minimum, you’d want to run a web server and a PHP interpreter (preferably – with the debugging engine installed).

With Docker, you can start developing, running, and debugging your code in a matter of minutes!

Probably the easiest way to integrate Docker with PhpStorm is to use the PhpStorm Docker registry. It provides a selection of preconfigured Docker images curated by the PhpStorm team, which cover the most common PHP development needs.

Before you proceed, make sure that you have Docker installed on your machine: see how to do it on Windows and on macOS.

Defining the environment

To get started, we create a new project in PhpStorm. Next, we create a new file named

docker-compose.yml
 , which will describe the configuration of the services comprising our app. In our case, it will be a single webserver service:
version: '2'
services:
  webserver:
    image: phpstorm/php-71-apache-xdebug-26
    ports:
      - "80:80"
    volumes:
      - ./:/var/www/html
    environment:
      XDEBUG_CONFIG: remote_host=host.docker.internal

As you can see, we use the preconfigured Docker image comprising the Apache web server and PHP 7.1 with Xdebug.

Note that we use the

host.docker.internal
  value to refer to the remote host. In Docker for Windows and Docker for Mac, this value automatically resolves to the internal address of the host, letting you easily connect to it from the container.

Our environment is fully described:

docker-compose

To start using it, we need to create a dedicated run/debug configuration.

Creating a run/debug configuration

Right-click

docker-compose.yml
  and select Create… from the context menu:

create_docker_compose_run_config

In the dialog that opens, provide the name of the configuration and apply your changes:

create_docker_compose_run_config_dialog

You can now start the configuration from the toolbar:

run_config_run_toolbar

PhpStorm will automatically download the required image and start the web server:

docker_start_service

That’s it: we’ve got everything ready for running and debugging our code!

Running and debugging code

Let’s ensure that everything works as expected. To do this, we’ll create the most simple Hello world PHP file and try to debug it following the PhpStorm Zero-Configuration Debugging approach.

Since we already have Xdebug installed and configured, this will only require that you:

  • Have a debugging extension installed and enabled for your browser:chrome_debug_extension
  • Set a breakpoint in your code:
    breakpoint_set
  • Enable listening to incoming debug connections in PhpStorm:enable_listening_debug_connections

Now, simply open the page in the browser, and the debugging session will be started automatically:

debugging_started

We encourage you to further explore the PhpStorm Docker registry: while we’ve looked at a very simple case, you can use the described technique to provide your environment with, for example, a database, or an sftp server.
Using these Docker images will save you a lot of effort and let you start coding in a matter of a minute, or even less!

If you’d like to learn more about Docker and how to use it in PhpStorm, make sure to check out the excellent tutorial series by Pascal Landau, and PhpStorm documentation, of course.

Your JetBrains PhpStorm Team
The Drive to Develop


Viewing all articles
Browse latest Browse all 31

Trending Articles