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() {
}Code language: JavaScript (javascript)
// ./Dockerfile

FROM golang:latest

RUN mkdir /app
ADD . /app

RUN go get
RUN go install

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

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

Leave a Reply

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