2

I've a docker installed in a VM in VirtualBox and I'm attempting to run a container with a dot Net Core application that connects to a MySQL database on the hosts machine. So I've configured the forwarding port for both mysql and my application on Virtual Box. I'm able to access my service through "http://localhost:3131/api/users/login" in the host machine but it throws an error saying that couldn't connect with the MySQL data base. I'm also able to run the app in the host machine when I'm not using docker. I've looked in other threads on the internet but nothing that enlightened me exactly except the last command shown below but I can't run since the MySQL authentication are configured is hard coded in the application not with a config file. The general configuration is as follows:

Program.cs

var host = new WebHostBuilder()
    .UseKestrel()
    .UseContentRoot(Directory.GetCurrentDirectory())
    .UseIISIntegration()
    .UseStartup<Startup>()
    .UseApplicationInsights()
    .UseUrls("http://*:80")
    .Build();

Dockerfile

FROM microsoft/aspnetcore

WORKDIR /app

COPY bin/Release/PublishOutput/ .

EXPOSE 80

ENTRYPOINT ["dotnet", "UsersApi.dll"]

Docker Run Command

docker run -d -p 3000:80 user_api
// and also tried
docker run -d -p 3000:80 user_api --net=host
// and also tried
docker run -d -p 3000:80 user_api --add-host localhost:127.0.0.1

VirtualBox fowarding ports:

NAT 3131 -> 3000    tcp
NAT 3306 -> 3306    tcp
NAT 2415 -> 22      

localhost (that I thought it would appear the port 3131, but it calls the service anyway.)

Starting Nmap 7.40 ( https://nmap.org ) at 2017-05-22 11:23 E. South America Standard Time

Nmap scan report for localhost (127.0.0.1)

Host is up (0.0013s latency).

Other addresses for localhost (not scanned): ::1

rDNS record for 127.0.0.1: rinaldipc.com

Not shown: 994 closed ports

PORT     STATE SERVICE

22/tcp   open  ssh

135/tcp  open  msrpc

445/tcp  open  microsoft-ds

2179/tcp open  vmrdp

3306/tcp open  mysql

5357/tcp open  wsdapi

RUN command in Dockfile that I think I need to add but I'm not sure of the proceedings.

RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/my.cnf

https://stackoverflow.com/questions/33827342/how-to-connect-mysql-workbench-to-running-mysql-inside-docker/33827463#33827463
Rinaldi Segecin
  • 449
  • 8
  • 23

1 Answers1

1

Since you're running inside VirtualBox, there's another layer between the VirtualBox host machine and docker. You have a machine hosting VirtualBox (1) -> Linux in VirtualBox (2) -> docker (3).

"localhost" for docker (3) means (2) so it expects mysql to be on (2). In your case, you have mysql on (1).

The only way to access (1) from (3) is by explicitly using the IP of (1) and not the "localhost" alias.

Victor Hurdugaci
  • 28,177
  • 5
  • 87
  • 103
  • So how am I able to access (3) from (1) typing localhost:3131 doesn't connection go both ways? Wouldn't it be possible to access the mysql in (1) from (3) then? (Thank you Victor for your patience) – Rinaldi Segecin May 24 '17 at 14:30