I'm new to Go, but having an annoying issue where changes in the code are not reflected on the page, unless I do another --build
when I bring up
the container. Is this normal? I'm running`Windows 10, Go 1.19, AMD, Docker Desktop/Compose.
If I change "Hello, World!"
to some other string, CTRL C the running app, and then run docker-compose up
, the changes are NOT reflected on the page, even after clearing browser cache and using an incognito window. HOWEVER, if I run docker-compose up --build
, the changes WILL be reflected.
Reminder I'm new to Go, but is this normal behaviour? Do I have to re-build the project in docker-compose
each time to see the changes? Or do you see anything "off" in my code? I'm following a few year old Udemy course, so of course every step there's a new "thing" to troubleshoot as it doesn't work as shown eye roll
They suggest using Air
for hot-reloading, which I'm also having an issue with as IT'S not working either, however I've opened a GitHub issue for that.
Here is the code from the various files:
main.go
package main
import (
"ambassador/src/database"
"github.com/gofiber/fiber/v2"
)
func main() {
// Connect to the database
database.Connect()
// Migrate tables in the database
database.AutoMigrate()
// Create a new fiber app, which is based on Express.js
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
})
app.Listen(":3000")
}
Dockerfile
FROM golang:1.19
WORKDIR /app
COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .
# Use air for live go hot-reloading
# This one doesn't work, use go install instead
# RUN curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin
# Air does not work for me. Opening github issue. Skip for now
# RUN go install github.com/cosmtrek/air@latest
# CMD ["air"]
CMD ["go", "run", "main.go"]
docker-compose.yaml
version: '3.9'
services:
backend:
build: .
ports:
- 8000:3000
# volumes:
# - .:/app
depends_on:
- db
db:
image: mysql:5.7.22
restart: always
environment:
MYSQL_DATABASE: ambassador
MYSQL_USER: root
MYSQL_PASSWORD: root
MYSQL_ROOT_PASSWORD: root
volumes:
- .dbdata:/var/lib/mysql
ports:
- 33066:3306
src > database > db.go
package database
import (
"ambassador/src/models"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
var DB *gorm.DB
func Connect() {
var err error
DB, err = gorm.Open(mysql.Open("root:root@tcp(db:3306)/ambassador"), &gorm.Config{})
if err != nil {
panic("Could not connect with the database!")
}
}
func AutoMigrate() {
DB.AutoMigrate(models.User{})
}
src > models > user.go
package models
type User struct {
Id uint
FirstName string
LastName string
Email string
Password string
IsAmbassador bool
}
go.mod
module ambassador
go 1.19
require github.com/gofiber/fiber/v2 v2.36.0
require (
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/klauspost/compress v1.15.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.38.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect
gorm.io/driver/mysql v1.3.5 // indirect
gorm.io/gorm v1.23.8 // indirect
)
The same code is included in this screenshot of my IDE.
CodePudding user response:
Go isn’t a script language and needs in rebuild and restart application to apply changes
You can use Golang fresh for rebuild and restart your app
https://github.com/gravityblast/fresh
Add this to your Dockerfile
RUN go install github.com/pilu/fresh@latest
...
CMD [ "fresh" ]
CodePudding user response:
You are not mounting any files into your container, only copying them once on image build. This is why you are not seeing anychanges unless you build, or copy new files into the container.
You've already commented out a volume from your docker-compose.yaml, but if you uncomment those lines you should see that changes are reflected without rebuilding.