我有这个 golang 沙箱项目:https : //github.com/cflynn07/golang-db-gateway-example
当我尝试gateway/gateway.go在一个golang:1.6.0-alpine
~/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")
}
慕田峪4524236
潇潇雨雨
慕雪6442864
相关分类