gpt4 book ai didi

golang 项目在 docker 容器中运行时找不到依赖项

转载 作者:IT王子 更新时间:2023-10-29 01:42:16 27 4
gpt4 key购买 nike

我有这个 golang 沙箱项目: https://github.com/cflynn07/golang-db-gateway-example

当我尝试在 golang:1.6.0-alpine 中运行 gateway/gateway.go

~/g/s/g/c/golang-db-gateway-example git:master ❯❯❯ docker-compose up gateway
mysql_server is up-to-date
Starting gateway
Attaching to gateway
gateway | gateway.go:7:2: cannot find package "github.com/go-sql-driver/mysql" in any of:
gateway | /usr/local/go/src/github.com/go-sql-driver/mysql (from $GOROOT)
gateway | /go/src/github.com/go-sql-driver/mysql (from $GOPATH)
gateway | gateway.go:8:2: cannot find package "github.com/gorilla/mux" in any of:
gateway | /usr/local/go/src/github.com/gorilla/mux (from $GOROOT)
gateway | /go/src/github.com/gorilla/mux (from $GOPATH)
gateway exited with code 1

为什么构建步骤没有检测到我的项目在 /example/vendor 文件夹中的依赖项?

当我从主机操作系统运行 go run gateway/gateway.go 时,该命令有效。

目录结构(安装在/example 的容器内)

~/g/s/g/c/golang-db-gateway-example git:master ❯❯❯ tree -L 3
.
├── README.md
├── client
│   └── client.go
├── docker-compose.yml
├── gateway
│   └── gateway.go
├── glide.lock
├── glide.yaml
├── tmp
└── vendor
└── github.com
├── go-sql-driver
└── gorilla


相关文件:

docker-compose.yml

mysql:
container_name: mysql_server
image: mysql:5.7.11
environment:
- MYSQL_ROOT_PASSWORD=root
ports:
- 3306
gateway:
container_name: gateway
image: golang:1.6.0-alpine
volumes:
- ./:/example
working_dir: /example/gateway
command: go run gateway.go
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=sandbox
links:
- mysql

网关/gateway.go

package main

import (
"database/sql"
"encoding/json"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
"net/http"
"os"
)

var db *sql.DB

func main() {
r := mux.NewRouter()

var e error
db, e = sql.Open(
"mysql", os.ExpandEnv("root:${MYSQL_SERVER_PASSWORD}@mysql_server:3306/${MYSQL_DATABASE}"))
fmt.Print("error is", e)

r.HandleFunc("/todos", getTodos).Methods("GET")

http.ListenAndServe(":8080", r)
fmt.Printf("gateway")
}

type todo struct{}

func getTodos(w http.ResponseWriter, r *http.Request) {
t := new(todo)
s, _ := json.Marshal(t)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
fmt.Fprint(w, string(s))
}



更新 1我更改了容器内的数据卷挂载路径,以将项目挂载在容器 $GOPATH

mysql:
container_name: mysql_server
image: mysql:5.7.11
environment:
- MYSQL_ROOT_PASSWORD=root
ports:
- 3306
gateway:
container_name: gateway
image: golang:1.6.0-alpine
volumes:
- ./:/go/src/github.com/cflynn07/golang-db-gateway-example
working_dir: /go/src/github.com/cflynn07/golang-db-gateway-example
command: go run gateway/gateway.go
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=sandbox
links:
- mysql

但是现在 docker 似乎挂起:

~/g/s/g/c/golang-db-gateway-example git:master ❯❯❯ docker-compose up gateway                                                         ✱
mysql_server is up-to-date
Recreating gateway
Attaching to gateway

最佳答案

实际上,您已成功运行 Go 服务器。它没有挂起,只是在等待连接。由于一些怪癖,没有输出:它没有尝试连接到数据库,并且缓冲了日志记录语句。

尝试修改 gateway.go main:

func main() {
log.Println("Starting main...")

conn := os.ExpandEnv("root:${MYSQL_SERVER_PASSWORD}@mysql_server:3306/${MYSQL_DATABASE}")

var err error
db, err = sql.Open("mysql", conn)
if err != nil {
log.Fatal(err)
}

log.Println("pinging", conn)
if err := db.Ping(); err != nil {
log.Fatal(err)
}

r := mux.NewRouter()
r.HandleFunc("/todos", getTodos).Methods("GET")

listen := ":8080"
log.Printf("Listening on %s\n", listen)
log.Fatal(http.ListenAndServe(listen, r))
}

运行这个版本会得到:

$ docker-compose up gateway
mysql_server is up-to-date
Starting gateway
Attaching to gateway
gateway | 2016/03/15 10:58:05 Starting main...
gateway | 2016/03/15 10:58:05 pinging root:@mysql_server:3306/sandbox
gateway | 2016/03/15 10:58:05 default addr for network 'mysql_server:3306' unknown
gateway | exit status 1
gateway exited with code 1

你应该从那里开始。注意:

  • docker-compose 似乎缓冲标准输出直到换行
  • log.Print 等日志函数添加换行符,fmt.Print 不添加新行
  • sql.Open 未连接到数据库,使用 sql.Ping(参见 wiki)
  • MYSQL_SERVER_PASSWORD 丢失
  • 缺少 mysql 连接字符串的网络类型(参见 examples)
  • 也启动mysql服务器
  • 需要创建新的或装载现有的数据库“沙箱”

希望对您有所帮助。

关于golang 项目在 docker 容器中运行时找不到依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36002205/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com