为什么在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.

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

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.