简体   繁体   English

在 docker 容器中调试 React 应用程序时,VSCode 断点未绑定

[英]VSCode breakpoints unbound when debugging a React app in a docker container

I'm having issues debugging with VSCode a React app created with Create React App and running in a Docker container.我在使用 VSCode 调试使用Create React App 创建并在 Docker 容器中运行的 React 应用程序时遇到问题。

The app runs fine with docker compose up and it correctly stops its execution until I attach with the debugger.该应用程序在docker compose up运行良好,并且在我附加调试器之前它会正确停止执行。 调试器已附加并在第一行暂停,下一行的断点处于活动状态

However, after that, only breakpoints in the start.js file behave correctly, while any other breakpoint in my source files stays unbound.但是,在那之后,只有start.js文件中的断点正常运行,而我的源文件中的任何其他断点都保持未绑定。 其他断点显得空洞,不起作用

Project Setup项目设置

package.json package.json

I defined a new script start:debug to run the application in inspect mode.我定义了一个新脚本start:debug以检查模式运行应用程序。

    {
    "name": "test",
    "version": "0.1.0",
    "private": true,
    "dependencies": {
        "@emotion/react": "^11.8.1",
        "@emotion/styled": "^11.8.1",
        "@mui/icons-material": "^5.4.2",
        "@mui/material": "^5.4.3",
        "@testing-library/jest-dom": "^5.16.1",
        "@testing-library/react": "^12.1.2",
        "@testing-library/user-event": "^13.5.0",
        "axios": "^0.26.0",
        "react": "^17.0.2",
        "react-dom": "^17.0.2",
        "react-scripts": "5.0.0",
        "web-vitals": "^2.1.3"
    },
    "scripts": {
        "start": "react-scripts start",
        "start:debug": "react-scripts --inspect-brk=0.0.0.0:9229 start",
        "build": "react-scripts build",
        "test": "react-scripts test",
        "eject": "react-scripts eject"
    },
    ...
    }

Dockerfile Dockerfile

FROM node:16-alpine

# Create app directory
WORKDIR /usr/src/app

# add `node_modules/.bin` to $PATH
ENV PATH /usr/src/app/node_modules/.bin:$PATH

# Install app dependencies
COPY package.json ./
COPY package-lock.json ./
RUN npm install

# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 3000

# Run app
CMD [ "npm", "run", "start:debug" ]

docker-compose.yml docker-compose.yml

version: '3'

services:
  web:
    image: test
    container_name: test
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - ./src:/usr/src/app/src
    env_file:
      - ./.envs/.local/.node
    ports:
      - "3000:3000"
      - "9229:9229"

.vscode/launch.json .vscode/launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Docker: Attach to Node",
            "type": "node",
            "request": "attach",
            "restart": true,
            "port": 9229,
            "address": "localhost",
            "trace": true,
            "localRoot": "${workspaceFolder}",
            "remoteRoot": "/usr/src/app",
            "sourceMaps": true
        },
    ]
}

I tried every configuration I could find but I did not manage to make it work.我尝试了所有我能找到的配置,但我没有设法让它工作。 Could anyone help me spot where the issue is?谁能帮我找出问题所在?


If you are working with a web-based react app - you will need to run a web browser to debug it.如果您正在使用基于 Web 的 React 应用程序 - 您将需要运行 web 浏览器来调试它。 Here is a setup that I did:这是我做的一个设置:

  1. set up a docker container for React with bind-mounts to my main system为 React 设置一个 docker 容器,并绑定到我的主系统
  2. enter container from terminal docker exec -ti react-ptc /bin/bash从终端docker exec -ti react-ptc /bin/bash
  3. create react app inside container in /app directory在 /app 目录中的容器内创建反应应用程序
  4. cd into app directory cd 进入应用目录
  5. run npm run eject - required to manage webpack config later运行npm run eject - 需要稍后管理 webpack 配置
  6. run npm run start inside docker container在 docker 容器内运行npm run start
  7. open react project folder with vscode from bind mount on host system.从主机系统上的绑定挂载中使用 vscode 打开反应项目文件夹。

Number 7 is necessary because otherwise VSCode will not be able to launch a web browser for debugging.数字 7 是必需的,否则 VSCode 将无法启动 web 浏览器进行调试。 Watch your permissions though.不过请注意您的权限。 You may need to set bind mounted /react directory to host user permissions.您可能需要设置绑定挂载的/react目录来托管用户权限。

Here is part of docker-compose.yml I used:这是我使用的docker-compose.yml的一部分:

version: '3'
services:
  react-ptc:
    image: 'dmitryr117/node:18.8.0-dev'
    container_name: react-ptc
    restart: unless-stopped
    volumes:
      - ./react:/app
    environment:
      - HOST=0.0.0.0
      - PORT=80
      - WDS_SOCKET_HOST=0.0.0.0
      - WDS_SOCKET_PORT=0
    expose:
      - 80
    ports:
      - 8080:80

I am however running my system with an nginx reverse proxy and using /etc/hosts to emulate real domain name behaviour on local environment.但是,我正在使用nginx反向代理运行我的系统,并使用/etc/hosts来模拟本地环境中的真实域名行为。

Now for launch.json and webpack.config.js现在launch.jsonwebpack.config.js

launch.json发射.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "chrome",
      "request": "launch",
      "name": "Brave Debug",
      "url": "http://client.ptc.test", // can set to http://localhost:8080
      "webRoot": "${workspaceFolder}",
      "timeout": 20000,
      "sourceMaps": true,
      "breakOnLoad": true,
      "runtimeExecutable": "/snap/bin/brave", // I am on Ubuntu using Brave web browser
      "runtimeArgs": [
        "--new-window",
        "--incognito",
        "--user-data-dir=${workspaceFolder}/chrome-profiles/ReactProfile",
        "--remote-debugging-port=9222"
      ],
      "sourceMapPathOverrides": {
        // requires inline-source-map in webpack.config.js for devtool
        // "browser-path": "web-root path"
        // IMPORTANT AREA:
        // Check sources in web browser DeveloperTools and change /app/ptc to 
        // your path above /src directory
        "/app/ptc/*": "${webRoot}/*",
      },
    }
  ]
}

In previously ejected config directory look for webpack.config.js and update as wollows:在之前弹出的config目录中查找webpack.config.js并更新为 wollos:

    ...
    devtool: 'inline-source-map',
    // isEnvProduction
    //   ? shouldUseSourceMap
    //     ? 'source-map'
    //     : false
    //   : isEnvDevelopment && 'cheap-module-source-map',
    // These are the "entry points" to our application.
    // This means they will be the "root" imports that are included in JS bundle.
    ...

That's it.而已。 Also update your .gitignore to avoid committing browser profile:还要更新您的.gitignore以避免提交浏览器配置文件:

...
# browser_profile
/chrome-profiles

Should work from here.应该从这里开始工作。 Hope this helps.希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 vscode 中的未绑定断点,用于在 docker 容器中调试 nodejs 应用程序 - Unbound breakpoint in vscode for debugging nodejs app in a docker container VS code 调试浏览器断点未绑定 - VS code debugging browser breakpoints unbound 在 VSCode 中使用“附加到节点功能”进行调试时无法在断点处停止 - Unable to halt at breakpoints when debugging using "Attach to Node Functions" in VSCode 一步即可使用VSCode调试Docker容器中的NodeJs程序 - Debugging NodeJs Program in Docker Container with VSCode in one step VS Code 未为在 Docker 容器中运行的 Node 应用程序设置断点 - VS Code not hitting breakpoints for Node app running in Docker Container 在 VS Code 中调试 Node JS 应用程序时没有命中断点 - Breakpoints are not hit when debugging Node JS app in VS Code WebStorm 2018.1:我无法从docker容器中查找运行远程调试打字稿的断点 - WebStorm 2018.1: I am not able to hit breakpoints running remote debugging typescript from docker container Docker node.js TypeScript 应用程序中的未绑定断点 - Unbound breakpoints in Docker node.js TypeScript application 使用 docker-compose 构建应用程序时,如何在 React 应用程序中为 URL 端点使用 Docker 容器名称? - How to use Docker container name for URL endpoint in React app when building app with docker-compose? VSCode调试(F5)React应用给出SyntaxError:意外标识符 - VSCode debugging (F5) react app gives SyntaxError: Unexpected identifier
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM