登录
首页 >  Golang >  Go问答

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 问题。

以下是各个文件中的代码:

ma​​in.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学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>