Search code examples
webstorm

How to tell WebStorm to look in Docker container for project?


I have my current project directory looking like so:

.
├── backend
│   ├── Dockerfile # NestJS Dockerfile.
│   ├── docker # Folder that contains docker-compose.yml file.
│   ├── package.json
│   └── src
└── frontend
    ├── Dockerfile # Angular Dockerfile.
    ├── package.json
    └── src

My docker-compose.yml file looks like so:

version: "3.7"
services:
  # ########################
  # Back-End Container
  # ########################
  backend: # Node-Express backend that acts as an API.
    container_name: nest_backend
    build:
      context: ../
    restart: always
    expose:
      - 3000
    ports:
      - "3000:3000"
    volumes:
      - /home/node/node_modules
      - "../:/home/node/"
  # ########################
  # Front-End Container
  # ########################
  frontend: # Angular frontend to be served to client.
    container_name: angular_frontend
    build:
      context: ../../frontend/
    restart: always
    expose:
      - 4200 # Angular ng-serve port.
      - 49153 # Websocket port for live reloading.
    ports:
      - "49153:49153"
      - "4200:4200"
    volumes:
      - /home/node/node_modules
      - "../../frontend/:/home/node/"
    depends_on:
      - backend
networks:
  default:

I want to use Docker and not lose the benefit of using WebStorm as an IDE. It tells me that the tslint package is not installed, for example, and that I need to run npm install to install the node_modules folder. However, my node_modules folders only exist in the containers. Is there a way I can make it so that WebStorm works properly with this?

I know there's something called path mapping, and I've tried to configure it, but nothing happens; it still tells me to install packages.

Here's what I've done so far:

I've followed the tutorial here:

  • Went to Edit Configuration at the top-right of the window next to the debug buttons.
  • Clicked the + sign and chose Node.js.
  • Clicked ... next to Node interpreter.
  • Clicked the + sign, chose Add Remote....
  • Selected Docker Compose from the radio buttons, clicked the folder icon next to Configuration file(s), and chose the appropriate service from the dropdown Service.
  • Clicked OK on everything and just ran it. It would run, give an error that the command was not found.

When I run docker-compose up, everything runs smoothly. When I tried to add the run configuration Run > Edit Configurations... to add (+) a Docker-compose run, pointing it to the docker-compose.yml file, it works smoothly as well.

What I wish to accomplish is to use the features of the IDE while in Docker. Currently, it does not know where to look for tslint configurations, or node_modules.


Solution

  • TLDR;

    Trick is to update the path inside docker container from /your-path to /opt/project.

    Detail solution

    The issue with webstorm is they don't allow you to define the path from where you can pick node_modules. But they have a default path from which they pick it. I was facing the same issue while I wanted to integrate remote debugging for a backend node service running inside docker.

    You need to update your docker file. Suppose you were using Dockerfile was something like this

    # pull official base image
    FROM node:12.11-buster
    
    # set working directory
    WORKDIR /app
    
    COPY ./package.json ./package-lock.json /app
    
    RUN npm install
    
    

    Now this won't be detecting the node_modules for remote debugging or any other integration you need in webstorm.

    But if you update the dockerfile to something like this

    # pull official base image
    FROM node:12.11-buster
    
    # set working directory
    # This done particularly for enabling debugging in webstorm.
    WORKDIR /opt/project
    
    COPY ./package.json ./package-lock.json ./.npmrc /opt/project
    
    RUN npm install
    
    

    Then the webstorm is able to detect everything as expected.

    Trick is to update the path from /your-path to /opt/project

    And your docker-compose file should look something like this:

    version: "3.7"
    services:
      backend-service:
        build:
          dockerfile: ./Dockerfile.local
          context: ./
        command: nodemon app.js
        volumes:
          - ./:/opt/project
          - /opt/project/node_modules/
        ports:
          - 6060:6060
        
    

    You can read more about this in my blog here