PhpStorm 2021.2 Help

Node.js with Docker

You can quickly bootstrap your Node.js application with Docker to run, debug, and profile it from PhpStorm. The IDE will take care of the initial configuration by automatically creating a new Dockerfile, building and running an image, syncing your source code, and installing npm dependencies in the container.

You can find some examples at Quick Tour of WebStorm and Docker.

Before you start

  1. Install and enable the Node.js Remote Interpreter plugin on the Settings/Preferences | Plugins page, tab Marketplace, as described in Installing plugins from JetBrains repository.

  2. Make sure the Node.js and Docker required plugins are enabled on the Settings/Preferences | Plugins page, tab Installed, see Managing plugins for details.

  3. Download, install, and configure Docker as described in Docker

Create a Node.js run/debug configuration

  1. From the main menu, choose Run | Edit Configuration, then in the Edit Configurations dialog, click the Add button on the toolbar and select Node.js from the list. The Run/Debug Configuration: Node.js dialog opens.

  2. In the JavaScript File field, specify the path to the main file of the application that starts it (for example, bin/www for Express applications).

  3. If necessary, specify some optional settings as described in Running and debugging Node.js applications.

Specify a Node.js interpreter

Select one of the configured Node.js interpreters of the type Remote Interpreter - Docker from the Node interpreter list or configure a new one.

  1. In the Run/Debug Configuration: Node.js dialog, click the Browse button next to the Node interpreter field.

  2. In the Node.js Interpreters dialog that opens with a list of all the currently configured interpreters, click the Add button on the toolbar. In the dialog that opens, choose Add Remote from the context menu .

  3. In the Configure Node.js Remote Interpreter dialog that opens, select the Docker option.

    1. In the Server field, specify the Docker configuration to use, see Configure the Docker daemon connection settings for details. Select a configuration from the list or click New... and create a new configuration on the Docker page that opens.

    2. In the Image name field, specify the base Docker image to use. Choose one of the previously downloaded or your custom images from the list or type the image name manually, for example, node:argon or mhart/alpine-node. When you later launch the run configuration, Docker will search for the specified image on your machine. If the search fails, the image will be downloaded from the Docker Official Images repository on the Docker Registry page.

    3. The Node.js interpreter path field shows the location of the default Node.js interpreter from the specified image.

    4. When you click OK, PhpStorm closes the Configure Node.js Remote Interpreter dialog and brings you to the Node.js Interpreters dialog where the new interpreter configuration is added to the list. Click OK to return to the run configuration.

Specify the Docker container settings

  • Click the Browse button next to the Edit Docker Container Settings field and specify the settings in the Edit Docker Container Settings dialog that opens.

  • Alternatively, select the Auto configure checkbox to do it automatically. Learn more about the Auto configure mode at Quick Tour of WebStorm and Docker: What Happens On Each Run.

Even with automatic configuration, you still need to bind the port on which your application is running with the port of the container. Those exposed ports are available on the Docker host’s IP address (by default 192.168.99.100). Such binding is required when you debug the client side of a Express application. In this case, you need to open the browser from your computer and access the application at the container host through the port specified in the application.

Configure port bindings

  1. Click the Browse button in the Docker Container Settings field and expand the Port bindings area in the Edit Docker Container Settings dialog that opens.

  2. Click the Add button and in the Port bindings dialog that opens, map the ports as follows:

    • In the Container port field, type the port specified in your application.

    • In the Host port field, type the port through which you want to open the application in the browser from your computer.

    • In the Host IP field, type the IP address of the Docker's host, the default IP is 192.168.99.100. The host is specified in the API URL field on the Docker page of the Settings/Preferences dialog.

    • Click OK to return to the Edit Docker Container Settings dialog where the new port mapping is added to the list.

  3. Click OK to return to the Run/Debug Configuration: Node.js dialog.

Run your application

  1. Create a Node.js configuration as described above.

  2. From the Select run/debug configuration list on the toolbar, select the newly created Node.js configuration.

  3. Click the Run button to the Select run/debug configuration list.

Start debugging

  1. Set the breakpoints in the Node.js code as necessary.

  2. Create a Node.js configuration as described above, select it from the Select run/debug configuration list on the toolbar, and click the Debug button next to the list.

  3. Proceed as when starting the debugger together with a Node.js application.

Using a remote Node.js interpreter in a Docker container with a Docker Compose definition

With Docker Compose, you can run a Node.js server and your application code as separate services. Each service can be scaled by adding more containers if necessary. This enables you to perform efficient development and testing in a dynamic environment that is similar to production one.

Configuring mappings

When you debug an application with a remote Node.js interpreter, the debugger tells PhpStorm the name of the currently processed file and the number of the line to be processed. PhpStorm opens the local copy of this file and indicates the line with the provided number. This behaviour is enabled by specifying correspondence between files and folders on the server and their local copies. This correspondence is called mapping, it is set in the debug configuration.

  1. From the main menu, choose Run | Edit Configurations. In the Edit Configuration dialog that opens, click the Add New Configuration button on the toolbar and choose Node.js from the context menu.

  2. In the Run/Debug Configuration: Node.js dialog that opens, choose the required remote interpreter from the Node interpreter list.

  3. Click the Browse button next to the Path Mappings field. The Edit Project Path Mappings Dialog that opens shows the path mappings retrieved from the deployment configuration. These mappings are read-only.

    • To add a custom mapping, click the Add button and specify the path in the project and the corresponding path on the remote runtime environment in the Local Path and Remote Path fields respectively. Type the paths manually or click the Browse button and select the relevant files or folders in the dialog that opens.

    • To remove a custom mapping, select it in the list and click the Remove button.

Last modified: 23 November 2021