Skip to content

Commit

Permalink
Merge pull request #3 from usagiga/feat/use-docker-compose-watch
Browse files Browse the repository at this point in the history
Use Docker Compose Watch
  • Loading branch information
usagiga authored Jun 17, 2024
2 parents e526838 + ba77993 commit 2784245
Show file tree
Hide file tree
Showing 23 changed files with 395 additions and 126 deletions.
18 changes: 0 additions & 18 deletions .air-test.toml

This file was deleted.

20 changes: 0 additions & 20 deletions .air.toml

This file was deleted.

17 changes: 17 additions & 0 deletions .dockerignore
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
22 changes: 22 additions & 0 deletions .ecrc
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
}
}
2 changes: 1 addition & 1 deletion .editorconfig.d
Submodule .editorconfig.d updated 2 files
+22 −0 .ecrc
+1 −0 docs/install.sh
2 changes: 1 addition & 1 deletion .github/workflows/editorconfig.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:

jobs:
check-format:
name: "Run editorconfig-checker"
name: "EditorConfig (editorconfig-checker)"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ on:
pull_request:

jobs:
test:
build-test-lint:
name: "Go (Test / Vet / Build)"
runs-on: "ubuntu-latest"
steps:
- uses: "actions/checkout@v4"
- uses: "actions/setup-go@v4"
with:
go-version: "1.22.0"
go-version-file: "go.mod"
- name: "vet"
run: "make lint"
- name: "test"
Expand Down
21 changes: 13 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
# Binary
/go-template

# Logs
*.log

# Golang Boilerplate from GitHub Community
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
go-template

# Test binary, built with `go test -c`
*.test
Expand All @@ -13,11 +22,7 @@ go-template
*.out

# Dependency directories (remove the comment below to include it)
vendor/

# cosmtrek/air
/tmp

# Logs
*.log
# vendor/

# Go workspace file
go.work
1 change: 0 additions & 1 deletion .go-version

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/runConfigurations/Debug_Test___2346_.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .prototools
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go = "1.22.4"
160 changes: 160 additions & 0 deletions DEVELOPERS.md
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:
65 changes: 61 additions & 4 deletions Dockerfile
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"]
Loading

0 comments on commit 2784245

Please sign in to comment.