go-reflex icon indicating copy to clipboard operation
go-reflex copied to clipboard

Docker image for auto-recompiling and auto-restarting Golang server application

Docker image for auto-recompiling and auto-restarting Golang server application

GitHub Workflow Status Docker Pulls Docker Stars Docker Architecture Docker Architecture Docker Image Version (latest by date)

Features

  • works with docker-compose and kind
  • uses cespare/reflex to watch .go and .html files changes and recompile/restart your server application
  • optionally compiles with data race detector
  • supports amd64 and arm64 architectures

Feature requests

How to use with docker-compose

Place docker-compose.yml in your project root and run docker-compose up --build.

docker-compose.yml example with main package in the root of the project

version: '3.8'

services:
  myservice:
    image: acim/go-reflex
    environment:
      - RACE_DETECTOR=1
    volumes:
      - .:/app
    ports:
      - 3000:3000

Note: Replace port number with correct port number of your application.

docker-compose.yml example with main package not in the root of the project

version: '3.8'

services:
  myservice:
    image: acim/go-reflex
    environment:
      - RACE_DETECTOR=1
      - BUILD_ARGS=./cmd/server/server.go
    volumes:
      - .:/app
    ports:
      - 3000:3000

docker-compose.yml example with installation of external dependencies

version: '3.8'

services:
  myservice:
    image: acim/go-reflex
    environment:
      - RACE_DETECTOR=1
      - APT_INSTALL=libraw-dev
    volumes:
      - .:/app
    ports:
      - 3000:3000

How to use with kind (Kubernetes)

Install kind

go install sigs.k8s.io/kind@latest

Create local cluster

kind create cluster --config=config.yaml

config.yaml example

apiVersion: kind.x-k8s.io/v1alpha4
kind: Cluster
nodes:
  - role: control-plane
    extraMounts:
      - hostPath: /path/to/your/project/root
        containerPath: /app

Deploy your application

kubectl apply -f deploy.yaml

deploy.yaml example

apiVersion: v1
kind: Pod
metadata:
  name: your-app-name
  namespace: default
spec:
  containers:
    - image: acim/go-reflex
      name: your-app-name
      env:
        - name: RACE_DETECTOR
          value: '1'
      volumeMounts:
        - mountPath: /app
          name: app
  restartPolicy: Never
  volumes:
    - hostPath:
        path: /app
      name: app

Start port-forwarding

kubectl port-forward your-app-name 3000:3000

Note: Replace port number with correct port number of your application.

Optional environment variables

  • RACE_DETECTOR=1 - used to turn on data race detector to the compiled binary
  • RUN_ARGS - used to add subcommands and/or flags in the call of your binary (i.e. serve --verbose)
  • BUILD_ARGS - used to add flags to go build command (i.e. "./cmd/myapp/main.go")
  • APT_INSTALL - used to install additional packages (experimental and not efficient)