nla*_*and 5 go docker visual-studio-code
我正在尝试使用 Docker/VScode 远程调试 Go 应用程序。我的文件看起来像:
package main
import (
"log"
"net"
"net/http"
"strings"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
message := r.URL.Path
message = strings.TrimPrefix(message, "/")
message = "Hello, " + message + "!"
w.Write([]byte(message))
})
log.Print("starting web server")
listener, err := net.Listen("tcp", ":8080")
if err != nil {
log.Fatal(err)
}
log.Printf("Start listening: %v", listener.Addr().String())
if err := http.Serve(listener, nil); err != nil {
log.Fatal(err)
}
}
Run Code Online (Sandbox Code Playgroud)
FROM golang:1.12
ENV GOPATH /go
ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH
RUN go get github.com/go-delve/delve/cmd/dlv
# set the working directory to /go/src
WORKDIR $GOPATH/src
# Copy everything from the current directory to the working directory inside the container
# (as set by WORKDIR)
COPY . .
# 8080 is for the web application
EXPOSE 8080 2345
Run Code Online (Sandbox Code Playgroud)
version: "3.0"
services:
web:
container_name: go-delve-docker-vscode-example
build: "./"
ports:
- "8080:8080"
- "2345:2345"
security_opt:
- "seccomp:unconfined"
tty: true
stdin_open: true
# command: go run main.go
command: dlv debug --headless --listen=:2345 --api-version=2 --log main.go
Run Code Online (Sandbox Code Playgroud)
{
"version": "0.2.0",
"configurations": [
{
"name": "Delve into Docker",
"type": "go",
"request": "launch",
"mode": "remote",
"remotePath": "/go/src/",
"port": 2345,
"host": "127.0.0.1",
"program": "${workspaceRoot}",
"showLog": true
}
]
}
Run Code Online (Sandbox Code Playgroud)
我可以看到我的应用程序的 docker 日志,我知道它正在监听2345. 但无论出于何种原因,我似乎无法调试该应用程序。我看到的另一个问题是,我看不到任何尝试运行launch.json.
编辑:我在 Mac OSX 上使用 Docker Desktop 运行它。
| 归档时间: |
|
| 查看次数: |
956 次 |
| 最近记录: |