From dce2b495b931f82fbb8d1ff71ef661d77817307c Mon Sep 17 00:00:00 2001 From: Matthew Lemon Date: Wed, 29 May 2024 15:23:15 +0100 Subject: Some tidying Configured to run dlv in the container See https://golangforall.com/en/post/go-docker-delve-remote-debug.html Haven't actually go the interactive dlv working - need to explore docker attach to do that, maybe. Slimmed down image in play - Also adds a new make rule to run debug container --- .dockerignore | 3 +++ Dockerfile | 16 +++++++++------- Makefile | 3 +++ compose.yaml | 7 +++---- debug/docker-compose-debug.yaml | 26 ++++++++++++++++++++++++++ debug/dockerfile-debug | 14 ++++++++++++++ 6 files changed, 58 insertions(+), 11 deletions(-) create mode 100644 .dockerignore create mode 100644 debug/docker-compose-debug.yaml create mode 100644 debug/dockerfile-debug diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..5d6bfcf --- /dev/null +++ b/.dockerignore @@ -0,0 +1,3 @@ +.git +Dockerfile +Dockerfile_postgres diff --git a/Dockerfile b/Dockerfile index d5f86a1..bc5197e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,12 @@ -FROM golang:alpine +FROM golang:alpine as build -WORKDIR /app +WORKDIR / -COPY go.mod ./ -RUN go mod download && go mod verify COPY . . -RUN go build -v -o /usr/local/bin/app ./cmd/dbasik-api -CMD ["app"] -EXPOSE 5000 + +RUN go build -o ./app ./cmd/dbasik-api + +FROM scratch +COPY --from=build /app /app +COPY --from=build /.env /.env +CMD ["/app"] diff --git a/Makefile b/Makefile index 1821068..9aa00bf 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,9 @@ build: run-container: @docker run -it --rm -p 4000:4000 dbasik:latest +debug: + @docker compose -f ./debug/dockerfile-debug up + run: @docker compose up -d diff --git a/compose.yaml b/compose.yaml index d1544a0..38a3ba9 100644 --- a/compose.yaml +++ b/compose.yaml @@ -13,11 +13,10 @@ services: app: build: context: . - volumes: - - .:/app + # volumes: + # - .:/app restart: "unless-stopped" - command: > - sh -c "go run ./cmd/dbasik-api" + command: "./app" ports: - 5000:5000 depends_on: diff --git a/debug/docker-compose-debug.yaml b/debug/docker-compose-debug.yaml new file mode 100644 index 0000000..fe81fc7 --- /dev/null +++ b/debug/docker-compose-debug.yaml @@ -0,0 +1,26 @@ +services: + db: + build: + context: . + dockerfile: Dockerfile.postgres + ports: + - 5432:5432 + environment: + - POSTGRES_PASSWORD=secret + volumes: + - dbasik_data:/var/lib/postgresql/data + - ./migrations:/dbasik + app: + build: + context: . + dockerfile: Dockerfile + # volumes: + # - .:/app + restart: "unless-stopped" + command: "/dlv --listen=:2345 --headless=true --log=true --log-output=debugger,debuglineerr,gdbwire,lldbout,rpc --accept-multiclient --api-version=2 exec ./app" + ports: + - 2345:2345 + depends_on: + - db +volumes: + dbasik_data: diff --git a/debug/dockerfile-debug b/debug/dockerfile-debug new file mode 100644 index 0000000..296ab31 --- /dev/null +++ b/debug/dockerfile-debug @@ -0,0 +1,14 @@ +FROM golang:alpine as build + +WORKDIR / + +COPY . . + +RUN CGO_ENABLED=0 go install github.com/go-delve/delve/cmd/dlv@latest +#RUN CGO_ENABLED=0 go get -ldflags "-s -w -extldflags '-static'" github.com/go-delve/delve/cmd/dlv +RUN CGO_ENABLED=0 go build -gcflags "all=-N -l" -o ./app ./cmd/dbasik-api + +FROM scratch +COPY --from=build /go/bin/dlv /dlv +COPY --from=build /app /app +CMD ["/dlv"] -- cgit v1.2.3