From cba88d61db443b692ae2c45cca4ec1554c667d3b Mon Sep 17 00:00:00 2001 From: Marcel Dempers Date: Thu, 18 Jul 2019 21:11:55 +1000 Subject: [PATCH] debugging in go --- .vscode/launch.json | 21 +++++++++++++++++++ docker-compose.yaml | 13 ++++++------ golang/dlv.sh | 3 +++ golang/dockerfile | 50 +++++++++++++++++++++++++++++---------------- golang/src/main.go | 4 +--- 5 files changed, 63 insertions(+), 28 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 golang/dlv.sh diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 000000000..0cc4dba0d --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,21 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Remote Docker", + "type": "go", + "request": "launch", + "mode": "remote", + "remotePath":"/go/src/work/", + "port": 2345, + "host": "127.0.0.1", + "program": "${workspaceFolder}/golang/src/", + "args": [], + "trace" : "verbose", + "env" : {} + } + ] +} \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index 528e6e116..029b3fe3d 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -14,20 +14,19 @@ services: - ./c#/src/:/work/ ports: - 5000:5000 - golang: #docker run -it -v ${PWD}:/work -w /work -p 5001:5000 aimvector/golang:1.0.0 /bin/sh + golang: #docker run -it -v ${PWD}:/go/src/work -p 5001:5000 -p 2345:2345 --security-opt "seccomp:unconfined" aimvector/golang:1.0.0 container_name: golang image: aimvector/golang:1.0.0 build: context: ./golang - target: prod - #working_dir: /work #comment out for build.target:prod - #entrypoint: /bin/sh #comment out for build.target:prod - #stdin_open: true #comment out for build.target:prod - #tty: true #comment out for build.target:prod + target: debug volumes: - - ./golang/src/:/work + - ./golang/src/:/go/src/work/ ports: - 5001:5000 + - 2345:2345 + security_opt: + - "seccomp:unconfined" nodejs: #docker run -it -v ${PWD}:/work -w /work -p 5002:5000 aimvector/nodejs:1.0.0 /bin/sh container_name: nodejs image: aimvector/nodejs:1.0.0 diff --git a/golang/dlv.sh b/golang/dlv.sh new file mode 100644 index 000000000..319e7ffbe --- /dev/null +++ b/golang/dlv.sh @@ -0,0 +1,3 @@ +#!/bin/sh +cd /go/src/work +dlv debug --headless --log -l 0.0.0.0:2345 --api-version=2 \ No newline at end of file diff --git a/golang/dockerfile b/golang/dockerfile index acaf07a13..dc23af457 100644 --- a/golang/dockerfile +++ b/golang/dockerfile @@ -1,18 +1,32 @@ -FROM golang:1.12.5-alpine3.9 as dev - -# installing git -RUN apk update && apk upgrade && \ - apk add --no-cache git - -RUN go get github.com/sirupsen/logrus -RUN go get github.com/buaazp/fasthttprouter -RUN go get github.com/valyala/fasthttp - -WORKDIR /work -COPY ./src /work/ -RUN go build -o app -###########START NEW IMAGE################### - -FROM alpine:3.9 as prod -COPY --from=dev /work/app / -CMD ./app +FROM golang:1.12.5-alpine3.9 as debug + +# installing git +RUN apk update && apk upgrade && \ + apk add --no-cache git \ + dpkg \ + gcc \ + git \ + musl-dev + +ENV GOPATH /go +ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH + +RUN go get github.com/sirupsen/logrus +RUN go get github.com/buaazp/fasthttprouter +RUN go get github.com/valyala/fasthttp +RUN go get github.com/go-delve/delve/cmd/dlv + +WORKDIR /go/src/work +COPY ./src /go/src/work/ + +RUN go build -o app +### Run the Delve debugger ### +COPY ./dlv.sh / +RUN chmod +x /dlv.sh +ENTRYPOINT [ "/dlv.sh"] + +###########START NEW IMAGE################### + +FROM alpine:3.9 as prod +COPY --from=debug /go/src/work/app / +CMD ./app diff --git a/golang/src/main.go b/golang/src/main.go index d9e6e7883..ba981c0fe 100644 --- a/golang/src/main.go +++ b/golang/src/main.go @@ -8,14 +8,12 @@ import ( "github.com/valyala/fasthttp" ) -var searchMock []byte - func Response(ctx *fasthttp.RequestCtx) { fmt.Fprintf(ctx, "Hello") } func main() { - fmt.Println("starting.") + fmt.Println("starting...") router := fasthttprouter.New() router.GET("/", Response)