Live reloading Go with Docker Compose

A very simple example using CompileDaemon and a Docker volume to live reload Go code, entering from the ./cmd folder.

All of the many, many, articles/videos I found on the internet were demonstrating how to live reload with the main.go file in the project root. Moving the main.go into the ./cmd folder was easy enough to do without a Docker volume, but introducing the Docker volume messed up the structure and overwrote code. In the end the solution was very simple, but discovering it had many twists and turns. Here it is, for anybody else facing the same challenge.

// ./cmd/main.go

package main

import "fmt"

func main() {
	fmt.Println("Hello!")
}Code language: JavaScript (javascript)
// ./Dockerfile

FROM golang:latest

RUN mkdir /app
WORKDIR /app
ADD . /app

RUN go get github.com/githubnemo/CompileDaemon
RUN go install github.com/githubnemo/CompileDaemon

ENTRYPOINT CompileDaemon --build="go build -o tmp/main cmd/main.go" --command=./tmp/mainCode language: JavaScript (javascript)
// ./docker-compose.yml

version: "3"
services:
  app:
    build: .
    container_name: hot-reload-example
    volumes:
      - ./:/appCode language: JavaScript (javascript)

https://github.com/clintvidler/go-docker-live-reload

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.