我是第一次使用 Docker 设置 Nodejs 项目,但在将调试器附加到应用程序时遇到了问题。
这是我的 docker 文件:
FROM node:latest
LABEL Name=graphql Version=0.1.0
# Create app directory
RUN mkdir -p /graphql
WORKDIR /graphql
# Install app dependencies
COPY package.json /graphql
RUN npm install -g gulp
RUN npm install
# Bundle app source
COPY . /graphql
EXPOSE 3000
EXPOSE 5858
EXPOSE 8000
CMD gulp
和组成一个:
version: '2'
services:
graphql:
image: graphql:latest
container_name: apollo-graphql
build:
context: .
dockerfile: dockerfile
environment:
NODE_ENV: development
tty: true
ports:
- 3000:3000
- 5858:5858
- 8000:8000
volumes:
- .:/graphql
## set your startup file here
command: gulp
gulp 任务当前正在像这样启动 nodemon:
gulp.task('serve', ['bundle'], () => {
stream = nodemon({
execMap: {
js: 'node --debug=5858'
},
script: 'build/server.bundle.js',
verbose: true
})
这似乎运行一切都很好,包括输出说明:Debugger listening on 127.0.0.1:5858
但是我似乎无法连接到它。我正在使用 VScode,我的启动配置文件如下:
"name": "Attach",
"type": "node",
"request": "attach",
"address": "127.0.0.1",
"port": 5858,
"localRoot": "${workspaceRoot}/graphql/",
"remoteRoot": "/graphql/",
即使我将 --debug 标志更改为 --inspect 以使用新协议(protocol),我仍然无法在 Chrome devtools 中打开它。
我看过一些教程,这些教程与我在这里所做的差不多,但我是否遗漏了什么? 我还在 StackOverflow 上看到了一些关于使用 Node 检查运行第二个容器的建议,但我不想走那条路,因为我试图让事情变得简单。事实上,这种设置似乎适用于那些教程,那么为什么它对我不起作用?
提前致谢!
最佳答案
我已经多次验证此配置,无法重现问题
这是我的整个 .vscode/launch.json 文件
{
// Use IntelliSense to learn about possible Node.js debug attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach to Docker container",
"port": 5858,
"address": "127.0.0.1",
"localRoot": "${workspaceRoot}",
"remoteRoot": "/var/app"
}
]
}
我唯一能想到的就是确保您在 VS Code 中运行“附加” Debug模式。
否则,我是无能为力的。它似乎对我有用。
关于javascript - 在 Docker 容器中调试 nodejs 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42769780/