Why connect to Google Cloud SQL failed inside Docker container but success outside Docker container?

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

  • Configuring MongoDB replica set from docker-compose
  • What's eth0 used for in docker host machine
  • Docker - Container is not running
  • How to use forever within a Docker container
  • Why docker-storage-setup doesn't create thin pool for overlayfs driver?
  • Docker: Devices cgroup isn’t mounted (Ubuntu 16.04)
  • 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.

  • Running multiple instances of an image with docker-compose fails
  • What would be pros and cons of using Docker Vs Vanilla LXC?
  • Pushing an image into private registry doesn't work if I deleted this image before via Registry API v2 call
  • Docker echo environment variable
  • How to correlate Docker image list with tag list both as returned by API?
  • MongDB with Docker Authentication
  • 2 Solutions collect form web for “Why connect to Google Cloud SQL failed inside Docker container but success outside Docker container?”

    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.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.