title: Access MySQL in one container from another container
body: <p><strong>I am trying the following:</strong></p>
  
  <ul>
  <li>spin MySQL 5.5 in one container with an exposed port, say 4200.</li>
  <li>spin MySQL 5.7 in one container with an exposed port, say 4300.</li>
  <li>spin a golang container to run my app.</li>
  </ul>
  
  <p>The idea is I need my tests to run against different database versions.</p>
  
  <p>For this I need to be able to talk to each of the sql containers from my golang container.</p>
  
  <p><strong>What I have tried:</strong></p>
  
  <p><strong>Method 1 - Using --link:</strong></p>
  
  <p><strong>MYSQL CONTAINER:</strong></p>
  
  <pre><code>docker run --name mysql55c -p 127.0.0.1:4200:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:5.5
  </code></pre>
  
  <p><strong>GO LANG APP CONTAINER</strong></p>
  
  <pre><code>docker run -w /go/src/app -it --link mysql55c -d --name golangapp -v $(pwd):/go/src/app golang bash -c "go get github.com/go-sql-driver/mysql;go build main.go; go test -v
  --config ./config.ini"
  </code></pre>
  
  <p><strong>Method 2 - Using --net (bridge network &amp;host):</strong>
  <strong>Create a bridge nw</strong></p>
  
  <blockquote>
    <p>docker network mynw</p>
  </blockquote>
  
  <p><strong>MYSQL CONTAINER:</strong></p>
  
  <pre><code>docker run --name mysql55c --net mynw -p 127.0.0.1:4200:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:5.5
  </code></pre>
  
  <p><strong>GO LANG APP CONTAINER</strong></p>
  
  <pre><code>docker run -w /go/src/app -it --net mynw -d --name golangapp -v $(pwd):/go/src/app golang bash -c "go get github.com/go-sql-driver/mysql;go build main.go; go test -v
  --config ./config.ini"
  </code></pre>
  
  <p>Instead of creating a bridge network of my own I also tired </p>
  
  <blockquote>
    <p>--net host</p>
  </blockquote>
  
  <p>I am able to connect to the sql containers from the host - mysql workbench.
  But, the golangapp is getting a connection refused error when trying to connect to the mysql servers inside the containers.</p>
  
  <p>All the containers are on the same host.</p>
  
  <p><strong>I am not using a dockerfile or docker compose as:</strong></p>
  
  <ul>
  <li>trying to get the above done with docker cli commands for easy jenkins integration using official docker images.</li>
  </ul>
  
  <p><strong>Go code to connect to the db using config url</strong></p>
  
  <pre><code>func dbconn() (*sql.DB, error) {
      opendb, err := sql.Open("mysql", *dsn + *dbname) //coming from flag
      if err != nil {
          return nil, err
      }
      return opendb, nil
  }
  </code></pre>
  
  <p><strong>dsn url combinations I have tried in config:</strong></p>
  
  <ul>
  <li>dsn = "root:root@tcp(localhost:4200)/"</li>
  <li><p>dsn = "root:root@tcp(172.17.0.0.x:4200)/" (docker0 ip)</p></li>
  <li><p>dbname="somename"</p></li>
  </ul>
  
  <p>How can we look at the above?
  Suggestions about best practices or new methods to achieve my objective are welcome :)
  Need Help!!
  Thank you :)</p>