-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from usagiga/feat/use-docker-compose-watch
Use Docker Compose Watch
- Loading branch information
Showing
23 changed files
with
395 additions
and
126 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# To build images safer and faster, | ||
# - Ignore large directories in `.dockerignore` (Docker can ignore directory recursively) | ||
# - Avoid using '!' in `.dockerignore` (prevent from O(kN) directory travarsal) | ||
# - Files required by images are written in `Dockerfile` (keep image small and secure) | ||
# - As of Dockerfile 1.7-lab, use `COPY --parents ["**/*.go", "./"]` | ||
# - Files not required when using Compose Watch, write ignore rules in `compose.yml` (prevent from rebuilding redundantly) | ||
# - Unlike Dockerfile, we cannot write whitelist because Compose Watch is incompatible with glob pattern and --parents flag | ||
|
||
**/.git | ||
**/.idea | ||
**/.vscode | ||
|
||
.editorconfig.d | ||
.github | ||
docs | ||
etc | ||
scripts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"Verbose": false, | ||
"IgnoreDefaults": false, | ||
"Exclude": [ | ||
"^\\.ecrc$", | ||
"\\.sln$", | ||
"\\.vcxproj$", | ||
"\\.vcxproj.filters$", | ||
"Resource\\.rc$", | ||
"go.mod", | ||
"go.sum" | ||
], | ||
"SpacesAfterTabs": false, | ||
"Disable": { | ||
"EndOfLine": false, | ||
"Indentation": false, | ||
"IndentSize": false, | ||
"InsertFinalNewline": false, | ||
"TrimTrailingWhitespace": false, | ||
"MaxLineLength": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
.idea/runConfigurations/delve_on_docker.xml → ...a/runConfigurations/Debug_Run___2345_.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
go = "1.22.4" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
# ${REPO_NAME} Development Guide | ||
|
||
This document aims to prepare your development environment for ${REPO_NAME}. | ||
|
||
## Setup | ||
|
||
## Install Dependencies | ||
|
||
- Go >= 1.22.4 | ||
- (Recommended) [proto](https://moonrepo.dev/proto) for managing runtime | ||
- Docker | ||
- Ensure some features | ||
- [Compose Spec](https://www.compose-spec.io/) | ||
- `compose watch` | ||
- `sync+restart` | ||
- `COPY --parents` with Dockerfile 1.7-lab | ||
- (Optional) BuildKit | ||
- Git | ||
|
||
<details> | ||
|
||
<summary>Detailed Docker Install Instruction</summary> | ||
|
||
In ArchLinux, install these packages: | ||
|
||
- https://archlinux.org/packages/extra/x86_64/docker/ | ||
- https://archlinux.org/packages/extra/x86_64/docker-compose/ | ||
- https://archlinux.org/packages/extra/x86_64/docker-buildx/ | ||
|
||
As a shorthand, run it: | ||
|
||
```sh | ||
$ pacman -S docker docker-compose docker-buildx | ||
|
||
$ sudo systemctl enable docker | ||
$ sudo systemctl start docker | ||
``` | ||
|
||
See details: [Docker - ArchWiki](https://wiki.archlinux.org/title/Docker) | ||
|
||
In the other distros/platforms, please ensure that Docker toolchains are installed enabled features described above. | ||
|
||
For example, the author is using this versions. | ||
Set up these versions or higher :pray: | ||
|
||
```sh | ||
# Docker CLI / Engine | ||
$ docker version | ||
Client: | ||
Version: 26.1.4 | ||
API version: 1.45 | ||
Go version: go1.22.3 | ||
Git commit: 5650f9b102 | ||
Built: Thu Jun 6 18:42:55 2024 | ||
OS/Arch: linux/amd64 | ||
Context: default | ||
|
||
Server: | ||
Engine: | ||
Version: 26.1.4 | ||
API version: 1.45 (minimum version 1.24) | ||
Go version: go1.22.3 | ||
Git commit: de5c9cf0b9 | ||
Built: Thu Jun 6 18:42:55 2024 | ||
OS/Arch: linux/amd64 | ||
Experimental: false | ||
containerd: | ||
Version: v1.7.18 | ||
GitCommit: ae71819c4f5e67bb4d5ae76a6b735f29cc25774e.m | ||
runc: | ||
Version: 1.1.12 | ||
GitCommit: | ||
docker-init: | ||
Version: 0.19.0 | ||
GitCommit: de40ad0 | ||
|
||
# Docker Compose | ||
$ docker compose version | ||
Docker Compose version 2.27.1 | ||
|
||
# BuildKit | ||
$ docker buildx inspect | ||
Name: default | ||
Driver: docker | ||
Last Activity: 2024-06-17 08:26:09 +0000 UTC | ||
|
||
Nodes: | ||
Name: default | ||
Endpoint: default | ||
Status: running | ||
BuildKit version: v0.13.2 | ||
Platforms: linux/amd64, linux/amd64/v2, linux/amd64/v3, linux/386 | ||
Labels: | ||
org.mobyproject.buildkit.worker.moby.host-gateway-ip: 172.17.0.1 | ||
``` | ||
|
||
</details> | ||
|
||
### Optional Dependencies | ||
|
||
- GNU Make | ||
- EditorConfig | ||
- Frontend for [delve](https://github.com/go-delve/delve) (i.e. IDEs) | ||
- Additional Linter / Vulnerability Checker | ||
- hadolint | ||
- Dockle | ||
- Trivy | ||
- actionlint | ||
- (Recommended) GoLand (Jetbrains IDE) | ||
- (TBU) Several run configurations are available | ||
- 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) | ||
|
||
# Build Binary | ||
|
||
```sh | ||
$ make build | ||
``` | ||
|
||
# Build Production Image | ||
|
||
```sh | ||
$ make build_image | ||
``` | ||
|
||
# Run a Development Build Locally with Debugger | ||
|
||
```sh | ||
$ make watch_dev | ||
``` | ||
|
||
This command prepares containers based on distroless:debug, | ||
then build/run binary with [delve](https://github.com/go-delve/delve) on `:2345`. | ||
And then, enabled hot reload using Compose Watch (rebuild mode), | ||
so that ensure to prune dangling images / networks / build caches manually. | ||
|
||
# Run a Go Test with Debugger | ||
|
||
```sh | ||
$ make watch_test | ||
``` | ||
|
||
This command prepares containers based on golang official image, | ||
then run `go test -v ./...` with [delve](https://github.com/go-delve/delve) on `:2346`. | ||
And then, enabled hot reload using Compose Watch (`sync+restart` mode). | ||
|
||
:warning: This instruction uses the port **2346**. It is not default of delve. | ||
|
||
If you want to change arguments of `go test`, override it using Docker `CMD` instruction. Such as: | ||
|
||
- `docker compose run [SERVICE] [COMMAND] [ARGS]` | ||
- `commands` in `compose.yml` | ||
- [Extend your Compose file | Docker Docs](https://docs.docker.com/compose/multiple-compose-files/extends/) | ||
|
||
# How to Use This Template | ||
|
||
- Replace variables | ||
- `${REPO_NAME}` -> Repository Name | ||
- `${REPO_DESC}` -> Repository Description | ||
- `go-template` -> Name of binary / package / repository | ||
- NOW GO :running: :dash: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,67 @@ | ||
FROM golang:1.22 | ||
#syntax=docker/dockerfile:1.7-labs | ||
|
||
ARG BUILD_BASE="golang" | ||
ARG BUILD_BASE_VERSION="1.22.4-bookworm" | ||
|
||
ARG PROD_BASE="gcr.io/distroless/base-debian12" | ||
ARG PROD_BASE_VERSION="nonroot" | ||
|
||
ARG DEV_BASE="${PROD_BASE}" | ||
ARG DEV_BASE_VERSION="debug-nonroot" | ||
|
||
# Prepare Sources | ||
FROM ${BUILD_BASE}:${BUILD_BASE_VERSION} as src | ||
WORKDIR /src | ||
|
||
# go.mod / go.sum | ||
COPY ["go.*", "./"] | ||
RUN go mod download | ||
|
||
# hadolint ignore=DL3021 | ||
COPY --parents ["**/*.go", "Makefile", "./"] | ||
|
||
# Build (Development) | ||
FROM src as build_dev | ||
|
||
# To build faster, installing delve partially | ||
RUN go install github.com/go-delve/delve/cmd/dlv@latest | ||
RUN go install github.com/cosmtrek/air@latest | ||
|
||
COPY . /src | ||
# to use delve, disable optimizations/inlining | ||
ENV GCFLAGS="all=-N -l" | ||
RUN make build | ||
|
||
# Build (Production) | ||
FROM src as build_prod | ||
RUN make build | ||
|
||
# Runner (Development) | ||
FROM ${DEV_BASE}:${DEV_BASE_VERSION} as dev | ||
|
||
WORKDIR /src | ||
|
||
EXPOSE 2345 | ||
|
||
COPY --from=build_dev ["/go/bin/dlv", "/usr/bin/"] | ||
COPY --from=build_dev ["/src/go-template", "./"] | ||
|
||
ENTRYPOINT ["dlv", "--listen=:2345", "--headless=true", "--api-version=2", "--accept-multiclient", "exec", "./go-template"] | ||
|
||
# Runner (Go Test) | ||
FROM build_dev as test | ||
|
||
WORKDIR /src | ||
|
||
EXPOSE 2346 | ||
|
||
# Run all tests | ||
# To test as you need, overwrite CMD | ||
ENTRYPOINT ["dlv", "--listen=:2346", "--headless=true", "--api-version=2", "--accept-multiclient", "test", "--"] | ||
CMD ["-test.v", "./..."] | ||
|
||
# Runner (Production) | ||
FROM ${PROD_BASE}:${PROD_BASE_VERSION} as prod | ||
WORKDIR /src | ||
|
||
COPY --from=build_prod ["/src/go-template", "./"] | ||
|
||
CMD ["air"] | ||
ENTRYPOINT ["./go-template"] |
Oops, something went wrong.