Skip to content

Commit dc77fdd

Browse files
migrate Dockerfiles
1 parent e31c7df commit dc77fdd

File tree

6 files changed

+84
-26
lines changed

6 files changed

+84
-26
lines changed

Dockerfile-debug

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
# Compile stage
2-
FROM golang:1.10.1-alpine3.7 AS build-env
3-
ENV CGO_ENABLED 0
4-
ADD . /go/src/hello
5-
6-
# The -gcflags "all=-N -l" flag helps us get a better debug experience
7-
RUN go build -gcflags "all=-N -l" -o /server hello
8-
9-
# Compile Delve
10-
RUN apk add --no-cache git
11-
RUN go get github.com/go-delve/delve/cmd/dlv
12-
2+
FROM golang:1.21.1-alpine3.18 AS build-env
3+
#ENV CGO_ENABLED 0
4+
WORKDIR /usr/src/app
5+
6+
# Download and install the Delve debugger.
7+
RUN go install github.com/go-delve/delve/cmd/dlv@latest
8+
9+
# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change
10+
COPY ./sample_app/go.mod ./sample_app/go.sum ./
11+
RUN go mod download && go mod verify
12+
13+
COPY ./sample_app .
14+
## The -gcflags "all=-N -l" flag helps us get a better debug experience
15+
RUN go build -gcflags "all=-N -l" -v -o /usr/local/bin/app ./...
16+
1317
# Final stage
14-
FROM alpine:3.7
15-
18+
FROM alpine:3.18
1619
# Port 8080 belongs to our application, 40000 belongs to Delve
1720
EXPOSE 8080 40000
18-
19-
# Allow delve to run on Alpine based containers.
20-
RUN apk add --no-cache libc6-compat
21-
2221
WORKDIR /
23-
24-
COPY --from=build-env /server /
25-
COPY --from=build-env /go/bin/dlv /
26-
22+
23+
COPY --from=build-env /go/bin/dlv /bin/dlv
24+
COPY --from=build-env /usr/local/bin/app /
25+
26+
# TODO: use env var and conditional run.sh
27+
# https://github.com/ccampo133/go-docker-alpine-remote-debug/blob/main/Dockerfile
2728
# Run delve
28-
CMD ["/dlv", "--listen=0.0.0.0:40000", "--headless=true", "--api-version=2", "debug", "go/src/hello", "--log"]
29+
CMD ["dlv", "--listen=0.0.0.0:40000", "--headless=true", "--api-version=2", "debug", "/app", "--log"]

Dockerfile-dlv-debug

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Compile stage
2+
FROM golang:1.21.1-alpine3.18 AS build-env
3+
ENV CGO_ENABLED 0
4+
WORKDIR /usr/src/app
5+
6+
# Download and install the Delve debugger.
7+
RUN go install github.com/go-delve/delve/cmd/dlv@latest
8+
9+
# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change
10+
COPY ./sample_app/go.mod ./sample_app/go.sum ./
11+
RUN go mod download && go mod verify
12+
13+
COPY ./sample_app .
14+
15+
CMD ["/bin/sh"]
16+
ENTRYPOINT ["/bin/sh"]

Dockerfile-dlv-exec

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Compile stage
2+
FROM golang:1.21.1-alpine3.18 AS build-env
3+
ENV CGO_ENABLED 0
4+
WORKDIR /usr/src/app
5+
6+
# Download and install the Delve debugger.
7+
RUN go install github.com/go-delve/delve/cmd/dlv@latest
8+
9+
# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change
10+
COPY ./sample_app/go.mod ./sample_app/go.sum ./
11+
RUN go mod download && go mod verify
12+
13+
COPY ./sample_app .
14+
RUN go build -gcflags "all=-N -l" -v -o /usr/local/bin/app ./...
15+
16+
# Final stage
17+
FROM alpine:3.18
18+
EXPOSE 8080
19+
WORKDIR /
20+
21+
COPY --from=build-env /go/bin/dlv /bin/dlv
22+
COPY --from=build-env /usr/local/bin/app /
23+
CMD ["/app"]

Makefile

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
build-sample-app: ## Build docker image for sample-app
33
docker build -f Dockerfile -t asoldatenko/sample-app .
44

5-
.PHONY: build-sample-app
6-
build-sample-app: ## Download goimports locally if necessary.
7-
docker build -f Dockerfile -t asoldatenko/sample-app .
5+
kind-setup:
6+
kind create cluster --name=debug-kind
7+
kubectl namespace create demo
8+
9+
kind-dlv-debug:
10+
docker build -f Dockerfile-dlv-debug -t asoldatenko/my-golang-app:new-tag .
11+
kind load docker-image asoldatenko/my-golang-app:new-tag --name=debug-kind
12+
kubectl apply -f ./config/samples/sample.yaml
13+
kubectl exec -n demo sample-app -it -- /bin/sh

config/samples/sample.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: sample-app
5+
namespace: demo
6+
spec:
7+
containers:
8+
- name: sample-debug
9+
image: asoldatenko/my-golang-app:new-tag
10+
command: [ "/bin/sh", "-c", "--" ]
11+
args: [ "while true; do sleep 300; done;" ]
12+

start.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ docker run \
33
-p 8080:8080 \
44
-p 40000:40000 \
55
--security-opt="apparmor=unconfined" \
6-
--cap-add=SYS_PTRACE hello-debug
6+
--cap-add=SYS_PTRACE t

0 commit comments

Comments
 (0)