Go 应用程序(在 Docker 容器中)没有反映页面上的更改?
来源:stackoverflow
时间:2024-04-02 13:24:37 381浏览 收藏
一分耕耘,一分收获!既然打开了这篇文章《Go 应用程序(在 Docker 容器中)没有反映页面上的更改?》,就坚持看下去吧!文中内容包含等等知识点...希望你能在阅读本文后,能真真实实学到知识或者帮你解决心中的疑惑,也欢迎大佬或者新人朋友们多留言评论,多给建议!谢谢!
我是 go 新手,但有一个恼人的问题,即代码中的更改不会反映在页面上,除非我在将 up 容器引入时执行另一个 --build 。这是正常的吗?我正在运行`windows 10、go 1.19、amd、docker desktop/compose。
如果我将 "hello, world!" 更改为其他字符串,ctrl+c 正在运行的应用程序,然后运行 docker-compose up,即使在清除浏览器缓存并使用后,更改也不会反映在页面上隐身窗口。但是,如果我运行 docker-compose up --build,更改将得到反映。
提醒我是 go 新手,但这是正常行为吗?每次我都必须在 docker-compose 中重新构建项目才能看到更改吗?或者您在我的代码中看到任何“关闭”的内容吗?我正在学习几年前的 udemy 课程,所以当然,每一步都有一个新的“东西”需要排除故障,因为它不起作用,如图所示翻白眼
他们建议使用 air 进行热重载,我也遇到了问题,因为它也不起作用,但我已经为此打开了一个 github 问题。
以下是各个文件中的代码:
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 > 数据库 > 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 > 模型 > 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
)
我的 ide 的屏幕截图中包含相同的代码。
正确答案
go 不是脚本语言,需要重建和重新启动应用程序以应用更改
您可以使用 golang fresh 来重建并重新启动您的应用
https://github.com/gravityblast/fresh
将其添加到您的 dockerfile
RUN go install github.com/pilu/fresh@latest ... CMD [ "fresh" ]
到这里,我们也就讲完了《Go 应用程序(在 Docker 容器中)没有反映页面上的更改?》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
Golang · Go问答 | 17小时前 | channel · goroutine · pprof · Go问答 · 线上排查 · pprof Go问答 Go channel阻塞 goroutine堆积 channel发送205 收藏
-
Golang · Go问答 | 18小时前 | 连接池 · database/sql · Go问答 · 线上排查 · DBStats · MaxOpenConns Go数据库连接池 DBStats Go问答 sql.DB WaitCount355 收藏
-
467 收藏
-
Golang · Go问答 | 18小时前 | EventSource · sse · Go问答 · http.Flusher · 实时推送 · EventSource FLUSH text/event-stream Go问答 http.Flusher Go SSE333 收藏
-
Golang · Go问答 | 19小时前 | 并发安全 · RWMutex · sync.Map · Go问答 · map并发 · RWMutex sync.Map Go问答 Go map并发读写 concurrent map writes102 收藏
-
166 收藏
-
Golang · Go问答 | 5天前 | 并发 · channel · select · 性能排查 · Go问答 · select Go channel context default CPU飙高 忙等循环 ticker438 收藏
-
Golang · Go问答 | 5天前 | pprof · trace · 性能排查 · Go问答 · 服务安全 · Go pprof 生产环境 trace 安全入口 net/http/pprof 性能排障349 收藏
-
Golang · Go问答 | 5天前 | channel · 并发编程 · Go问答 · 背压 · 容量规划 · Goroutine channel 缓冲区 背压 Go问答 buffered channel 并发容量377 收藏
-
Golang · Go问答 | 5天前 | interface · 单元测试 · 架构设计 · repository · Go问答 · 单元测试 架构设计 interface 接口设计 Go问答 调用方定义 Repository212 收藏
-
Golang · Go问答 | 5天前 | JSON · time.Time · 接口设计 · Go问答 · encoding/json · encoding/json API响应 JSON序列化 time.Time omitempty Go问答 omitzero315 收藏
-
Golang · Go问答 | 5天前 | HTTP · Cookie · 浏览器 · cors · Go问答 · SameSite · cookie cors Secure SameSite Go问答 Set-Cookie 跨站请求 credentials246 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习