title: Can't connect to MySQL container from Golang container
body: <p>I have 2 containers: a simple golang program and mysql.
  I can start both but the golang application can not connect to mysql. I receive <strong>Connection Refused</strong>.</p>
  <p>If I <code>docker exec</code> into the <strong>mysql</strong> container and try to connect locally with <code>mysql -u root -p</code> all works well.</p>
  <p>If on the the host I try to connect to the container with <code>mysql -h 0.0.0.0 -P 10000 -u root -p</code> all works well.</p>
  <p>I added <code>MYSQL_ROOT_HOST: '%'</code> in <code>docker-compose</code> and <code>bind-address = 0.0.0.0</code> into <code>mysqld.cnf</code> but still not working.</p>
  <p>What I'm doing wrong?</p>
  <p><strong>docker-compose</strong></p>
  <pre class="lang-yml prettyprint-override"><code>    version: '3'
      services:
        web:
          build:
            context: .
            dockerfile: docker/web/Dockerfile
          ports:
            - &quot;8081:8081&quot; # http
            - &quot;443:443&quot; # https
          links:
            - db_private
          volumes:
           - ../../../../.:/go
        db_private:
          image: mysql:5.7
          restart: always
          environment:
            MYSQL_DATABASE: ${MYSQL_DATABASE}
            MYSQL_USER: ${MYSQL_USER}
            MYSQL_PASSWORD: ${MYSQL_PASSWORD}
            MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
            MYSQL_ROOT_HOST: '%'
          ports:
            - '10000:3306'
          expose:
            - '3306'
          volumes:
            - ./mysql-entry-point.sql:/docker-entrypoint-initdb.d
            - private-db:/var/lib/mysql
            - ./mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf
      # Names our volume
      volumes:
        private-db:
  </code></pre>
  <p><strong>Web container Dockerfile</strong></p>
  <pre><code>    FROM golang:alpine
      RUN mkdir /app
      ADD . /app/
      
      WORKDIR /app
      
      RUN apk update &amp;&amp; apk upgrade &amp;&amp; apk add --no-cache bash git openssh
      
      RUN go get github.com/jinzhu/gorm
      RUN go get github.com/jinzhu/gorm/dialects/mysql
      RUN go get github.com/gin-gonic/gin
      RUN go get github.com/gin-contrib/cors
      
      
      RUN go build -o main .
      RUN adduser -S -D -H -h /app appuser
      USER appuser
      CMD [&quot;./main&quot;]
  </code></pre>
  <p><strong>Golang file</strong></p>
  <pre class="lang-golang prettyprint-override"><code>    package main
      
      import (
          &quot;fmt&quot;
          &quot;github.com/gin-gonic/gin&quot;
          &quot;github.com/jinzhu/gorm&quot;
          _ &quot;github.com/jinzhu/gorm/dialects/mysql&quot;
          &quot;net/http&quot;
          &quot;time&quot;
      )
      
      var db *gorm.DB
      
      type (
          FooModel struct {
              Id        int64 `gorm:&quot;primary_key&quot;`
              name      string
          }
      )
      
      func init() {
          //open a db connection
          var err error
          db, err = gorm.Open(&quot;mysql&quot;, &quot;user:pwd@(0.0.0.0:10000)/myDB?charset=utf8&amp;parseTime=true&quot;)
          if err != nil {
              fmt.Println(err)
              panic(&quot;failed to connect database&quot;)
          }
          //Migrate the schema
          db.AutoMigrate(&amp;FooModel{})
      
          db.LogMode(true)
      }
      
      // main inits routes
      func main() {
      
          router := gin.Default()
      
          router.GET(&quot;/getExample&quot;, getExample)
          
          router.Run(&quot;0.0.0.0:8081&quot;)
      }
      
      
      func getExample(c *gin.Context) {
      
          c.JSON(http.StatusOK, gin.H{&quot;status&quot;: http.StatusOK, &quot;data&quot;: &quot;bye&quot;})
      }
  </code></pre>
  <p><strong>I copy the <code>mysqld.cnf</code> in the container with this</strong></p>
  <pre><code>    [mysqld]
      pid-file    = /var/run/mysqld/mysqld.pid
      socket      = /var/run/mysqld/mysqld.sock
      datadir     = /var/lib/mysql
      #log-error  = /var/log/mysql/error.log
      # By default we only accept connections from localhost
      #bind-address   = 127.0.0.1
      bind-address   = 0.0.0.0
      # Disabling symbolic-links is recommended to prevent assorted security risks
      symbolic-links=0
  </code></pre>