├── .github ├── dependabot.yml └── workflows │ └── build.yml ├── .vscode ├── extensions.json └── settings.json ├── Dockerfile ├── LICENSE ├── README.md ├── build.sh └── reflex.conf /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # To get started with Dependabot version updates, you'll need to specify which 2 | # package ecosystems to update and where the package manifests are located. 3 | # Please see the documentation for all configuration options: 4 | # https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates 5 | 6 | version: 2 7 | updates: 8 | - package-ecosystem: 'github-actions' 9 | directory: '/' 10 | schedule: 11 | interval: 'daily' 12 | - package-ecosystem: 'docker' 13 | directory: '/' 14 | schedule: 15 | interval: 'daily' 16 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: build 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | pull_request: 8 | 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - name: Check out 14 | uses: actions/checkout@v4 15 | - name: Lint Dockerfile 16 | uses: hadolint/hadolint-action@v3.1.0 17 | with: 18 | dockerfile: Dockerfile 19 | - name: Set up QEMU 20 | uses: docker/setup-qemu-action@v3 21 | - name: Set up Docker Buildx 22 | id: buildx 23 | uses: docker/setup-buildx-action@v3.10.0 24 | - name: Inspect builder 25 | run: | 26 | echo "Name: ${{ steps.buildx.outputs.name }}" 27 | echo "Endpoint: ${{ steps.buildx.outputs.endpoint }}" 28 | echo "Status: ${{ steps.buildx.outputs.status }}" 29 | echo "Flags: ${{ steps.buildx.outputs.flags }}" 30 | echo "Platforms: ${{ steps.buildx.outputs.platforms }}" 31 | - name: Login to container registry 32 | uses: docker/login-action@v3.4.0 33 | with: 34 | username: ${{ github.actor }} 35 | password: ${{ secrets.DOCKERHUB_PASSWORD }} 36 | - name: Prepare tags 37 | id: prep 38 | run: | 39 | IMAGE=acim/go-reflex 40 | VERSION=$(grep FROM Dockerfile | awk '{ print $2 }' | cut -d ':' -f 2) 41 | TAGS="${IMAGE}:${VERSION},${IMAGE}:${GITHUB_SHA::8},${IMAGE}:latest" 42 | echo ::set-output name=version::${VERSION} 43 | echo ::set-output name=tags::${TAGS} 44 | echo ::set-output name=created::$(date -u +'%Y-%m-%dT%H:%M:%SZ') 45 | - name: Build and push image to container registry 46 | uses: docker/build-push-action@v6.18.0 47 | with: 48 | platforms: linux/amd64,linux/arm64 49 | push: ${{ github.event_name != 'pull_request' }} 50 | tags: ${{ steps.prep.outputs.tags }} 51 | cache-from: type=gha 52 | cache-to: type=gha,mode=max 53 | labels: | 54 | org.opencontainers.image.title=go-reflex 55 | org.opencontainers.image.description='Docker image for autorecompiling and autorestarting Golang server' 56 | org.opencontainers.image.url=https://github.com/acim/go-reflex 57 | org.opencontainers.image.source=${{ github.event.repository.clone_url }} 58 | org.opencontainers.image.version=${{ steps.prep.outputs.version }} 59 | org.opencontainers.image.created=${{ steps.prep.outputs.created }} 60 | org.opencontainers.image.revision=${{ github.sha }} 61 | org.opencontainers.image.licenses=${{ github.event.repository.license.spdx_id }} 62 | org.opencontainers.image.vendor=ectobit.com 63 | -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | // See http://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations. 3 | // Extension identifier format: ${publisher}.${name}. Example: vscode.csharp 4 | // List of extensions which should be recommended for users of this workspace. 5 | "recommendations": [ 6 | "ms-azuretools.vscode-docker" 7 | ], 8 | // List of extensions recommended by VS Code that should not be recommended for users of this workspace. 9 | "unwantedRecommendations": [] 10 | } 11 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "cSpell.words": [ 3 | "beerpay", 4 | "kubernetes" 5 | ] 6 | } -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1.24.4 2 | 3 | RUN go install github.com/cespare/reflex@latest 4 | 5 | COPY reflex.conf /usr/local/etc/ 6 | COPY build.sh /usr/local/bin/ 7 | 8 | WORKDIR /app 9 | 10 | VOLUME /go 11 | 12 | CMD ["reflex", "-d", "none", "-c", "/usr/local/etc/reflex.conf"] 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD-2-Clause Plus Patent License 2 | 3 | Copyright © 2021 Boban Acimovic All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without modification, 6 | are permitted provided that the following conditions are met: 7 | 8 | 1. Redistributions of source code must retain the above copyright notice, this 9 | list of conditions and the following disclaimer. 10 | 11 | 2. Redistributions in binary form must reproduce the above copyright notice, 12 | this list of conditions and the following disclaimer in the documentation 13 | and/or other materials provided with the distribution. 14 | 15 | Subject to the terms and conditions of this license, each copyright holder and 16 | contributor hereby grants to those receiving rights under this license a 17 | perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable 18 | (except for failure to satisfy the conditions of this license) patent license 19 | to make, have made, use, offer to sell, sell, import, and otherwise transfer 20 | this software, where such license applies only to those patent claims, already 21 | acquired or hereafter acquired, licensable by such copyright holder or 22 | contributor that are necessarily infringed by: 23 | 24 | (a) their Contribution(s) (the licensed copyrights of copyright holders and 25 | non-copyrightable additions of contributors, in source or binary form) 26 | alone; or 27 | 28 | (b) combination of their Contribution(s) with the work of authorship to which 29 | such Contribution(s) was added by such copyright holder or contributor, if, 30 | at the time the Contribution is added, such addition causes such 31 | combination to be necessarily infringed. The patent license shall not apply 32 | to any other combinations which include the Contribution. 33 | 34 | Except as expressly stated above, no rights or licenses from any copyright 35 | holder or contributor is granted under this license, whether expressly, 36 | by implication, estoppel or otherwise. 37 | 38 | DISCLAIMER 39 | 40 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 41 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 42 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 43 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE 44 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 45 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 46 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 47 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 48 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 49 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 50 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Docker image for auto-recompiling and auto-restarting Golang server application 2 | 3 | [![build](https://github.com/acim/go-reflex/actions/workflows/build.yml/badge.svg)](https://github.com/acim/go-reflex/actions/workflows/build.yml) 4 | [![Docker Pulls](https://img.shields.io/docker/pulls/acim/go-reflex?logo=docker&label=pulls)](https://hub.docker.com/r/acim/go-reflex) 5 | [![Docker Stars](https://img.shields.io/docker/stars/acim/go-reflex?logo=docker&label=stars)](https://hub.docker.com/r/acim/go-reflex) 6 | ![Docker Architecture](https://img.shields.io/badge/arch-amd64-blue?logo=docker) 7 | ![Docker Architecture](https://img.shields.io/badge/arch-arm64-blue?logo=docker) 8 | ![Docker Image Version (latest by date)](https://img.shields.io/docker/v/acim/go-reflex?logo=docker) 9 | 10 | ## Features 11 | 12 | - works with [docker-compose](https://github.com/docker/compose) and [kind](https://github.com/kubernetes-sigs/kind) 13 | - uses [cespare/reflex](https://github.com/cespare/reflex) to watch .go and .html files changes and recompile/restart your server application 14 | - optionally compiles with data race detector 15 | - supports amd64 and arm64 architectures 16 | 17 | ## Feature requests 18 | 19 | - please open a [new issue](https://github.com/acim/go-reflex/issues/new) 20 | 21 | ## How to use with docker-compose 22 | 23 | Place docker-compose.yml in your project root and run `docker-compose up --build`. 24 | 25 | ### docker-compose.yml example with main package in the root of the project 26 | 27 | ```yaml 28 | version: '3.8' 29 | 30 | services: 31 | myservice: 32 | image: acim/go-reflex 33 | environment: 34 | - RACE_DETECTOR=1 35 | volumes: 36 | - .:/app 37 | ports: 38 | - 3000:3000 39 | ``` 40 | 41 | Note: Replace port number with correct port number of your application. 42 | 43 | ### docker-compose.yml example with main package not in the root of the project 44 | 45 | ```yaml 46 | version: '3.8' 47 | 48 | services: 49 | myservice: 50 | image: acim/go-reflex 51 | environment: 52 | - RACE_DETECTOR=1 53 | - BUILD_ARGS=./cmd/server/server.go 54 | volumes: 55 | - .:/app 56 | ports: 57 | - 3000:3000 58 | ``` 59 | 60 | ### docker-compose.yml example with installation of external dependencies 61 | 62 | ```yaml 63 | version: '3.8' 64 | 65 | services: 66 | myservice: 67 | image: acim/go-reflex 68 | environment: 69 | - RACE_DETECTOR=1 70 | - APT_INSTALL=libraw-dev 71 | volumes: 72 | - .:/app 73 | ports: 74 | - 3000:3000 75 | ``` 76 | 77 | ## How to use with kind (Kubernetes) 78 | 79 | ### Install kind 80 | 81 | ```sh 82 | go install sigs.k8s.io/kind@latest 83 | ``` 84 | 85 | ### Create local cluster 86 | 87 | ```sh 88 | kind create cluster --config=config.yaml 89 | ``` 90 | 91 | ### config.yaml example 92 | 93 | ```yaml 94 | apiVersion: kind.x-k8s.io/v1alpha4 95 | kind: Cluster 96 | nodes: 97 | - role: control-plane 98 | extraMounts: 99 | - hostPath: /path/to/your/project/root 100 | containerPath: /app 101 | ``` 102 | 103 | ### Deploy your application 104 | 105 | ```sh 106 | kubectl apply -f deploy.yaml 107 | ``` 108 | 109 | ### deploy.yaml example 110 | 111 | ```yaml 112 | apiVersion: v1 113 | kind: Pod 114 | metadata: 115 | name: your-app-name 116 | namespace: default 117 | spec: 118 | containers: 119 | - image: acim/go-reflex 120 | name: your-app-name 121 | env: 122 | - name: RACE_DETECTOR 123 | value: '1' 124 | volumeMounts: 125 | - mountPath: /app 126 | name: app 127 | restartPolicy: Never 128 | volumes: 129 | - hostPath: 130 | path: /app 131 | name: app 132 | ``` 133 | 134 | ### Start port-forwarding 135 | 136 | ```sh 137 | kubectl port-forward your-app-name 3000:3000 138 | ``` 139 | 140 | Note: Replace port number with correct port number of your application. 141 | 142 | ## Optional environment variables 143 | 144 | - RACE_DETECTOR=1 - used to turn on data race detector to the compiled binary 145 | - RUN_ARGS - used to add subcommands and/or flags in the call of your binary (i.e. serve --verbose) 146 | - BUILD_ARGS - used to add flags to go build command (i.e. "./cmd/myapp/main.go") 147 | - APT_INSTALL - used to install additional packages (experimental and not efficient) 148 | -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | if [ -n "${APT_INSTALL}" ]; then 4 | apt-get update && apt-get install -y "${APT_INSTALL}" 5 | fi 6 | 7 | args=(-o /go/bin/app) 8 | 9 | if [[ ${RACE_DETECTOR} -eq "1" ]]; then 10 | CGO_ENABLED=1 11 | args+=(-race) 12 | fi 13 | 14 | cd /app 15 | go build "${args[@]}" ${BUILD_ARGS} 16 | /go/bin/app ${RUN_ARGS} 17 | -------------------------------------------------------------------------------- /reflex.conf: -------------------------------------------------------------------------------- 1 | -sr '(\.go|\.html)$' -- /usr/local/bin/build.sh 2 | --------------------------------------------------------------------------------