donglu5041 2017-02-26 09:33
浏览 187

为什么在Docker容器内连接到Google Cloud SQL失败而在Docker容器外连接成功?

I've written a piece of code in Golang to test 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")
}

I've tested the above code, the output is mysql ping success

Then I want to test this function inside Docker container, the Dockerfile following:

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\""]

After building the Docker image, and run the container, I got the following output: mysql ping failed: Error 1045: Access denied for user '"user'@'my.local.ip.address' (using password: YES)

I've already configured that my local IP can access Google Cloud SQL. I don't know why it doesn't work inside Docker container but works outside Docker container.

Updates, I've fixed the issue because of Dockerfile error

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"]

The main difference is to remove the quotation mark in the Dockerfile:CMD parameter, while you need the quotation mark when you execute the program from Terminal.

  • 写回答

2条回答 默认 最新

  • dongmei2351 2017-02-26 09:41
    关注

    Try

    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 and ENTRYPOINT are different commands

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集