我已经在Golang上写了一块代码,以测试Google Cloud SQL:
package main
import (
"database/sql"
"flag"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
var addr = flag.String("db", "", "The database address")
func main() {
flag.Parse()
db, err := sql.Open("mysql", *addr)
if err != nil {
fmt.Println("mysql open failed: ", err)
return
}
defer db.Close()
err = db.Ping()
if err != nil {
fmt.Println("mysql ping failed: ", err)
return
}
fmt.Println("mysql ping success")
}
我已经测试了上述代码,输出为 mysql ping Success
然后,我想在Docker容器中测试此功能, dockerfile 以下:
FROM golang
ADD . $GOPATH/src/github.com/pdu/gcloud-sql-test
RUN go install github.com/pdu/gcloud-sql-test
ENTRYPOINT ["gcloud-sql-test"]
CMD ["-db="user:passwd@tcp(gcloud.sql.ip.address:3306)/database""]
构建Docker映像并运行容器后,我得到了以下输出: mysql ping失败:错误1045:访问用户的访问''@@'my.local.ip.address'(使用密码:yes)
我已经配置了我的本地IP可以访问Google Cloud SQL。我不知道为什么它在Docker容器中不起作用,而是在Docker容器外工作。
更新,我已经解决了问题,因为Dockerfile错误
FROM golang
ADD . $GOPATH/src/github.com/pdu/gcloud-sql-test
RUN go install github.com/pdu/gcloud-sql-test
CMD ["gcloud-sql-test", "-db=user:passwd@tcp(gcloud.sql.ip.address:3306)/database"]
主要区别是在Dockerfile:CMD参数中删除引号,而当您从终端执行程序时,您需要 quotation mark 。
尝试
FROM golang
ADD . $GOPATH/src/github.com/pdu/gcloud-sql-test
RUN go install github.com/pdu/gcloud-sql-test
CMD ["gcloud-sql-test","-db="user:passwd@tcp(gcloud.sql.ip.address:3306)/database""]
cmd和入口点是不同的命令
我已经解决了问题,因为dockerfile错误
FROM golang
ADD . $GOPATH/src/github.com/pdu/gcloud-sql-test
RUN go install github.com/pdu/gcloud-sql-test
CMD ["gcloud-sql-test", "-db=user:passwd@tcp(gcloud.sql.ip.address:3306)/database"]
主要区别是在Dockerfile:CMD参数中删除引号,而当您从终端执行程序时,您需要 quotation mark 。