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:
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
.
Docker inspect provides detailed information on constructs controlled by Docker. By default, docker inspect will render results in a JSON array. For example uses of this command, refer to the examples section below.
In order to list the Docker containers, we can use the “docker ps” or “docker container ls” command.
WebStorm creates a Dockerfile run configuration that runs the docker build command. In the Services tool window, select the image that you want to upload and click or select Push Image from the context menu. Select the Docker registry and specify the repository and tag (name and version of the image, for example, my-app:v2 ).
Docker containers are runtime instances of the corresponding images. WebStorm uses run configurations to execute the commands that build Docker images and run containers. There are three types of Docker run configurations: Docker Image: Created automatically when you run a container from an existing image.
In the Package manager field, specify the location of the package manager to use. The default location for npm executable is /usr/local/lib/node_modules/npm. With WebStorm, you can manage project dependencies and run scripts in a Docker container just in the same way as you do with local projects.
Node.js developers are embracing Docker for repeatable builds, and WebStorm supports Docker-oriented workflows: Quickly bootstrap your Node.js app with a Dockerfile, then run and debug your app in Docker, from the WebStorm IDE. Let’s take a look. Note: WebStorm’s Docker support is for server-side Node.js applications.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With