3
votes

Then I run debug on IIS Express all is going OK.

I make connection to DB over ssh by Ssh.NET library.

PasswordConnectionInfo connectionInfo = new PasswordConnectionInfo("here is remote ip", 22, "ssh login here", "password for ssh")
{
    Timeout = TimeSpan.FromSeconds(30)
};
var client = new SshClient(connectionInfo);
client.Connect();
ForwardedPortLocal portForward = new ForwardedPortLocal("127.0.0.1", 22, "127.0.0.1", 3306);
client.AddForwardedPort(portForward);
portForward.Start();

services.AddDbContext<WhetherContext>(options =>
            options.UseMySQL(Configuration.GetConnectionString("Server=127.0.0.1;Port=22;Database=MyDb;Uid=MySqlDatabaseLogin;Pwd=DbPassword;SslMode=none;")));

When I run it on docker-machine, it throws exception when try to use context.

enter image description here

enter image description here

I use Linux containers. What may cause that?

UPD: Dockerfile

FROM microsoft/aspnetcore:2.0 AS base
RUN curl -sL https://deb.nodesource.com/setup_8.x
RUN bash -
RUN apt-get -y update
RUN apt-get install -y build-essential nodejs
RUN ln -s /usr/bin/nodejs /usr/bin/node
WORKDIR /app
EXPOSE 80

FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY *.sln ./
COPY WhetherService/WhetherService.csproj WhetherService/
RUN dotnet restore
COPY . .
WORKDIR /src/WhetherService
RUN dotnet build -c Release -o /app


FROM build AS publish
RUN dotnet publish -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "WhetherService.dll"]

docker-compose.yml

version: '3'

services:
  whetherservice:
    image: alexandrs/whetherservice
    build:
     context: .
     dockerfile: WhetherService/Dockerfile

docker-compose.ci.build.yml

version: '3'

services:
  ci-build:
    image: microsoft/aspnetcore-build:1.0-2.0
    volumes:
       - .:/src
    working_dir: /src
    command: /bin/bash -c "dotnet restore ./WhetherService.sln && dotnet publish ./WhetherService.sln -c Release -o ./obj/Docker/publish"

/etc/mysql/my.cnf file:

The MySQL database server configuration file.

You can copy this to one of: - "/etc/mysql/my.cnf" to set global options, - "~/.my.cnf" to set user-specific options.

One can use all long options that the program supports. Run program with --help to get a list of available options and with --print-defaults to see which it would actually understand and use.

For explanations see http://dev.mysql.com/doc/mysql/en/server-system-variables.html

  • IMPORTANT: Additional settings that can override those from this file! The files must end with '.cnf', otherwise they'll be ignored.

!includedir /etc/mysql/conf.d/

!includedir /etc/mysql/mysql.conf.d/

netstat -tln :

enter image description here

1
have you checked to which ip address is binded mysql? 127.0.0.1 or docker instance ipTheOni
This may be a permission issue, have you granted access to the IP address of the docker container?Gusman
How are you running docker? Show the Dockerfile and if applicable command or docker compose fileMike Tung
@MikeTung addedAlexandr S
@KamranShahid I didn't find any solution. After some attempts, I on PostgreSQL... And haven't tried again MySQLAlexandr S

1 Answers

0
votes

From the info you have given, it seems you are just forgetting to connect your remote mysql to your image. The way to do that is to open a port on the remote server to listen on, then in the docker image where you run that program you'll need to connect using a conn string with the hostname or ip address of that remote server.