1
votes

Here is the picture of what I am doing:

  1. I have local Go API code, I have built it into a docker image.
  2. I have pulled MySQL docker image from docker.
  3. I have DB in MySQL and Go API is accessing mysql.

Everything worked fine till my Go API was local and mysql was docker container. Now I have built local Go code as docker image and when I try to run this image using docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -d mysql:5.5 , Docker container starts and exits immediately. I tried Docker Start -a Container-ID to start container again, I get this error 'dial tcp 127.0.0.1:3306: getsockopt: connection refused'.

When I searched about this error I got this input - "After setting bind-address: 127.0.0.1 in mysql server config I was able to get the installation working with host localhost:3306."

But I am not aware how to set bind-address.

Any inputs regarding this will be helpful.

Thanks.

1
what is the db url that you used?Pratheesh M
If I understand correctly you try to reach from your Go container the MySQL with 127.0.0.1:3306?Daniel Müssig
@PratheeshM , Daniel, I am not using 127.0.0.1:3306 to connect Go container, instead using localhost. this is the exact connection string: var mysql_connect = "root:password@tcp(localhost:3306)/dbname"Sbk3824

1 Answers

0
votes

You should use Docker's network service discovery feature.

Put both your containers onto the same network and then they can discover eachother via DNS.

For example:

docker network create mynet
docker run --net mynet --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -d mysql:5.5
docker run --net mynet --name app -p 80:80 -d myappimage:latest

Both of these containers will be able to resolve 'mysql' or 'app' to the ip that each respective container has on the 'mynet' network. Configure your application to connect to mysql at 'mysql:3306'.