Skip to content

Commit 2784245

Browse files
authored
Merge pull request #3 from usagiga/feat/use-docker-compose-watch
Use Docker Compose Watch
2 parents e526838 + ba77993 commit 2784245

23 files changed

+395
-126
lines changed

.air-test.toml

Lines changed: 0 additions & 18 deletions
This file was deleted.

.air.toml

Lines changed: 0 additions & 20 deletions
This file was deleted.

.dockerignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# To build images safer and faster,
2+
# - Ignore large directories in `.dockerignore` (Docker can ignore directory recursively)
3+
# - Avoid using '!' in `.dockerignore` (prevent from O(kN) directory travarsal)
4+
# - Files required by images are written in `Dockerfile` (keep image small and secure)
5+
# - As of Dockerfile 1.7-lab, use `COPY --parents ["**/*.go", "./"]`
6+
# - Files not required when using Compose Watch, write ignore rules in `compose.yml` (prevent from rebuilding redundantly)
7+
# - Unlike Dockerfile, we cannot write whitelist because Compose Watch is incompatible with glob pattern and --parents flag
8+
9+
**/.git
10+
**/.idea
11+
**/.vscode
12+
13+
.editorconfig.d
14+
.github
15+
docs
16+
etc
17+
scripts

.ecrc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"Verbose": false,
3+
"IgnoreDefaults": false,
4+
"Exclude": [
5+
"^\\.ecrc$",
6+
"\\.sln$",
7+
"\\.vcxproj$",
8+
"\\.vcxproj.filters$",
9+
"Resource\\.rc$",
10+
"go.mod",
11+
"go.sum"
12+
],
13+
"SpacesAfterTabs": false,
14+
"Disable": {
15+
"EndOfLine": false,
16+
"Indentation": false,
17+
"IndentSize": false,
18+
"InsertFinalNewline": false,
19+
"TrimTrailingWhitespace": false,
20+
"MaxLineLength": false
21+
}
22+
}

.editorconfig.d

.github/workflows/editorconfig.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ on:
99

1010
jobs:
1111
check-format:
12-
name: "Run editorconfig-checker"
12+
name: "EditorConfig (editorconfig-checker)"
1313
runs-on: ubuntu-latest
1414
steps:
1515
- uses: actions/checkout@v4

.github/workflows/go.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ on:
55
pull_request:
66

77
jobs:
8-
test:
8+
build-test-lint:
99
name: "Go (Test / Vet / Build)"
1010
runs-on: "ubuntu-latest"
1111
steps:
1212
- uses: "actions/checkout@v4"
1313
- uses: "actions/setup-go@v4"
1414
with:
15-
go-version: "1.22.0"
15+
go-version-file: "go.mod"
1616
- name: "vet"
1717
run: "make lint"
1818
- name: "test"

.gitignore

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1+
# Binary
2+
/go-template
3+
4+
# Logs
5+
*.log
6+
7+
# Golang Boilerplate from GitHub Community
8+
# If you prefer the allow list template instead of the deny list, see community template:
9+
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
10+
#
111
# Binaries for programs and plugins
212
*.exe
313
*.exe~
414
*.dll
515
*.so
616
*.dylib
7-
go-template
817

918
# Test binary, built with `go test -c`
1019
*.test
@@ -13,11 +22,7 @@ go-template
1322
*.out
1423

1524
# Dependency directories (remove the comment below to include it)
16-
vendor/
17-
18-
# cosmtrek/air
19-
/tmp
20-
21-
# Logs
22-
*.log
25+
# vendor/
2326

27+
# Go workspace file
28+
go.work

.go-version

Lines changed: 0 additions & 1 deletion
This file was deleted.

.idea/runConfigurations/delve_on_docker.xml renamed to .idea/runConfigurations/Debug_Run___2345_.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations/Debug_Test___2346_.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.prototools

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
go = "1.22.4"

DEVELOPERS.md

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# ${REPO_NAME} Development Guide
2+
3+
This document aims to prepare your development environment for ${REPO_NAME}.
4+
5+
## Setup
6+
7+
## Install Dependencies
8+
9+
- Go >= 1.22.4
10+
- (Recommended) [proto](https://moonrepo.dev/proto) for managing runtime
11+
- Docker
12+
- Ensure some features
13+
- [Compose Spec](https://www.compose-spec.io/)
14+
- `compose watch`
15+
- `sync+restart`
16+
- `COPY --parents` with Dockerfile 1.7-lab
17+
- (Optional) BuildKit
18+
- Git
19+
20+
<details>
21+
22+
<summary>Detailed Docker Install Instruction</summary>
23+
24+
In ArchLinux, install these packages:
25+
26+
- https://archlinux.org/packages/extra/x86_64/docker/
27+
- https://archlinux.org/packages/extra/x86_64/docker-compose/
28+
- https://archlinux.org/packages/extra/x86_64/docker-buildx/
29+
30+
As a shorthand, run it:
31+
32+
```sh
33+
$ pacman -S docker docker-compose docker-buildx
34+
35+
$ sudo systemctl enable docker
36+
$ sudo systemctl start docker
37+
```
38+
39+
See details: [Docker - ArchWiki](https://wiki.archlinux.org/title/Docker)
40+
41+
In the other distros/platforms, please ensure that Docker toolchains are installed enabled features described above.
42+
43+
For example, the author is using this versions.
44+
Set up these versions or higher :pray:
45+
46+
```sh
47+
# Docker CLI / Engine
48+
$ docker version
49+
Client:
50+
Version: 26.1.4
51+
API version: 1.45
52+
Go version: go1.22.3
53+
Git commit: 5650f9b102
54+
Built: Thu Jun 6 18:42:55 2024
55+
OS/Arch: linux/amd64
56+
Context: default
57+
58+
Server:
59+
Engine:
60+
Version: 26.1.4
61+
API version: 1.45 (minimum version 1.24)
62+
Go version: go1.22.3
63+
Git commit: de5c9cf0b9
64+
Built: Thu Jun 6 18:42:55 2024
65+
OS/Arch: linux/amd64
66+
Experimental: false
67+
containerd:
68+
Version: v1.7.18
69+
GitCommit: ae71819c4f5e67bb4d5ae76a6b735f29cc25774e.m
70+
runc:
71+
Version: 1.1.12
72+
GitCommit:
73+
docker-init:
74+
Version: 0.19.0
75+
GitCommit: de40ad0
76+
77+
# Docker Compose
78+
$ docker compose version
79+
Docker Compose version 2.27.1
80+
81+
# BuildKit
82+
$ docker buildx inspect
83+
Name: default
84+
Driver: docker
85+
Last Activity: 2024-06-17 08:26:09 +0000 UTC
86+
87+
Nodes:
88+
Name: default
89+
Endpoint: default
90+
Status: running
91+
BuildKit version: v0.13.2
92+
Platforms: linux/amd64, linux/amd64/v2, linux/amd64/v3, linux/386
93+
Labels:
94+
org.mobyproject.buildkit.worker.moby.host-gateway-ip: 172.17.0.1
95+
```
96+
97+
</details>
98+
99+
### Optional Dependencies
100+
101+
- GNU Make
102+
- EditorConfig
103+
- Frontend for [delve](https://github.com/go-delve/delve) (i.e. IDEs)
104+
- Additional Linter / Vulnerability Checker
105+
- hadolint
106+
- Dockle
107+
- Trivy
108+
- actionlint
109+
- (Recommended) GoLand (Jetbrains IDE)
110+
- (TBU) Several run configurations are available
111+
- See also: [GoLand blog](https://blog.jetbrains.com/go/2019/02/06/debugging-with-goland-getting-started/#debugging-a-running-application-on-a-remote-machine)
112+
113+
# Build Binary
114+
115+
```sh
116+
$ make build
117+
```
118+
119+
# Build Production Image
120+
121+
```sh
122+
$ make build_image
123+
```
124+
125+
# Run a Development Build Locally with Debugger
126+
127+
```sh
128+
$ make watch_dev
129+
```
130+
131+
This command prepares containers based on distroless:debug,
132+
then build/run binary with [delve](https://github.com/go-delve/delve) on `:2345`.
133+
And then, enabled hot reload using Compose Watch (rebuild mode),
134+
so that ensure to prune dangling images / networks / build caches manually.
135+
136+
# Run a Go Test with Debugger
137+
138+
```sh
139+
$ make watch_test
140+
```
141+
142+
This command prepares containers based on golang official image,
143+
then run `go test -v ./...` with [delve](https://github.com/go-delve/delve) on `:2346`.
144+
And then, enabled hot reload using Compose Watch (`sync+restart` mode).
145+
146+
:warning: This instruction uses the port **2346**. It is not default of delve.
147+
148+
If you want to change arguments of `go test`, override it using Docker `CMD` instruction. Such as:
149+
150+
- `docker compose run [SERVICE] [COMMAND] [ARGS]`
151+
- `commands` in `compose.yml`
152+
- [Extend your Compose file | Docker Docs](https://docs.docker.com/compose/multiple-compose-files/extends/)
153+
154+
# How to Use This Template
155+
156+
- Replace variables
157+
- `${REPO_NAME}` -> Repository Name
158+
- `${REPO_DESC}` -> Repository Description
159+
- `go-template` -> Name of binary / package / repository
160+
- NOW GO :running: :dash:

Dockerfile

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,67 @@
1-
FROM golang:1.22
1+
#syntax=docker/dockerfile:1.7-labs
22

3+
ARG BUILD_BASE="golang"
4+
ARG BUILD_BASE_VERSION="1.22.4-bookworm"
5+
6+
ARG PROD_BASE="gcr.io/distroless/base-debian12"
7+
ARG PROD_BASE_VERSION="nonroot"
8+
9+
ARG DEV_BASE="${PROD_BASE}"
10+
ARG DEV_BASE_VERSION="debug-nonroot"
11+
12+
# Prepare Sources
13+
FROM ${BUILD_BASE}:${BUILD_BASE_VERSION} as src
314
WORKDIR /src
415

16+
# go.mod / go.sum
17+
COPY ["go.*", "./"]
18+
RUN go mod download
19+
20+
# hadolint ignore=DL3021
21+
COPY --parents ["**/*.go", "Makefile", "./"]
22+
23+
# Build (Development)
24+
FROM src as build_dev
25+
26+
# To build faster, installing delve partially
527
RUN go install github.com/go-delve/delve/cmd/dlv@latest
6-
RUN go install github.com/cosmtrek/air@latest
728

8-
COPY . /src
29+
# to use delve, disable optimizations/inlining
30+
ENV GCFLAGS="all=-N -l"
31+
RUN make build
32+
33+
# Build (Production)
34+
FROM src as build_prod
35+
RUN make build
36+
37+
# Runner (Development)
38+
FROM ${DEV_BASE}:${DEV_BASE_VERSION} as dev
39+
40+
WORKDIR /src
41+
42+
EXPOSE 2345
43+
44+
COPY --from=build_dev ["/go/bin/dlv", "/usr/bin/"]
45+
COPY --from=build_dev ["/src/go-template", "./"]
46+
47+
ENTRYPOINT ["dlv", "--listen=:2345", "--headless=true", "--api-version=2", "--accept-multiclient", "exec", "./go-template"]
48+
49+
# Runner (Go Test)
50+
FROM build_dev as test
51+
52+
WORKDIR /src
53+
54+
EXPOSE 2346
55+
56+
# Run all tests
57+
# To test as you need, overwrite CMD
58+
ENTRYPOINT ["dlv", "--listen=:2346", "--headless=true", "--api-version=2", "--accept-multiclient", "test", "--"]
59+
CMD ["-test.v", "./..."]
60+
61+
# Runner (Production)
62+
FROM ${PROD_BASE}:${PROD_BASE_VERSION} as prod
63+
WORKDIR /src
64+
65+
COPY --from=build_prod ["/src/go-template", "./"]
966

10-
CMD ["air"]
67+
ENTRYPOINT ["./go-template"]

0 commit comments

Comments
 (0)