简体   繁体   English

从Web Api .Net核心容器连接到MySQL容器? 如何获得IP地址?

[英]Connect to MySQL container from Web Api .Net Core Container? How to get Ip Address?

I know this is such a noob problem but I am having trouble understanding how to get my .Net Core website to connect to my MySql container. 我知道这是一个菜鸟问题,但是我在理解如何使.Net Core网站连接到MySql容器方面遇到困难。 So some background, both the MySql and the .Net core website are in their separate containers. 因此,在某些背景下,MySql和.Net核心网站都位于各自的容器中。 I have already started the MySql container and setup the root account to work. 我已经启动了MySql容器并设置了root帐户来工作。 I am using Entity Framework inside of .Net Core project. 我在.Net Core项目中使用实体框架。

I created the MySql container using this statement: docker run --name mysql_container -d -p 3306:3306 我使用以下语句创建了MySql容器: docker run --name mysql_container -d -p 3306:3306

Below is the dockerfile that Visual Studio generated for me. 以下是Visual Studio为我生成的dockerfile。

So what do I tell my .Net Core program to is the IP address of the MySql container if the IP can change? 那么,如果IP可以更改,我该告诉.Net Core程序是MySql容器的IP地址吗?

Inside of .Net Core Program: 在.Net Core程序内部:

public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            var connection = $"Server={GetDBAddress()};Database=myDataBase;Uid=root;Pwd=root;";
            services.AddDbContext<ToDoContext>(options => options.UseMySQL(connection));
        }

If I write the GetDBAddress function what goes in there? 如果我编写GetDBAddress函数,那里面有什么? I cannot simply return localhost because it's another docker container? 我不能简单地返回本地主机,因为它是另一个Docker容器? As of right now I am trying to use localhost and I get connection refused. 截至目前,我尝试使用localhost并拒绝连接。 But I am able to connect to the MySql db using workbench. 但是我可以使用工作台连接到MySql数据库。

Also I am not sure but can these two setups be combined into some file I think they're called docker-compose files maybe? 另外我不确定,但是可以将这两个设置组合到某个文件中吗,我认为它们可能被称为docker-compose文件?

Dockerfile Dockerfile

FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY ["ToDoService/ToDoService.csproj", "ToDoService/"]
RUN dotnet restore "ToDoService/ToDoService.csproj"
COPY . .
WORKDIR "/src/ToDoService"
RUN dotnet build "ToDoService.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "ToDoService.csproj" -c Release -o /app

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

If you've launched MySQL exposing the ports you should be able to reach it connecting from localhost , with the port 3306 . 如果启动了MySQL公开了端口,则应该可以从localhost连接3306端口来访问它。 Otherwise, as you suggested, there is the possibility to set up a docker-compose file . 否则,按照您的建议,可以设置docker-compose file This file usually contains all the configuration your application needs to run. 该文件通常包含您的应用程序需要运行的所有配置。 So, for example, a suitable configuration for your application (note: I'm assuming you're using MySQL 5.7 since you haven't specified one) could be: 因此,例如,适合您的应用程序的配置(请注意:由于您尚未指定MySQL,因此我假设您使用的是MySQL 5.7)可能是:

version: '3.3'

services: # list of services composing your application
   db: # the service hosting your MySQL instance
     image: mysql:5.7 # the image and tag docker will pull from docker hub
     volumes: # this section allows you to configure persistence within multiple restarts
       - db_data:/var/lib/mysql
     restart: always # if the db crash somehow, restart it
     environment: # env variables, you usually set this to override existing ones
       MYSQL_ROOT_PASSWORD: root
       MYSQL_DATABASE: todoservice
       MYSQL_USER: root
       MYSQL_PASSWORD: root
   todoservice: # you application service
     build: ./ # this tells docker-compose to not pull from docker hub, but to build from the Dockerfile it will find in ./
     restart: always
     depends_on: # set a dependency between your service and the database: this means that your application will not run if the db service is not running, but it doesn't assure you that the dabase will be ready to accept incoming connection (so your application could crash untill the db initializes itself)
       - db

volumes:
    db_data: # this tells docker-compose to save your data in a generic docker volume. You can see existing volumes typing 'docker volume ls'

To launch and deploy your application, now you need to type in a terminal: docker-compose up This will bring up your deploy. 要启动和部署您的应用程序,现在您需要输入一个终端: docker-compose up这将启动您的部署。 Note that no ports are exposed here: only your service will be able to access the database from db:3306 (you don't need to refer by IP, but you can reach other services using the service name). 请注意,这里没有暴露任何端口:只有您的服务才能从db:3306访问数据库(您不需要按IP进行引用,但是可以使用服务名称访问其他服务)。 For debug purposes, you can still open your db ports adding this line under image : 为了进行调试,您仍然可以打开数据库端口,在image下添加以下行:

ports:
  - "3306:3306"

Note that this port has to be free (no other system services are using it), otherwise the entire deployment will fail. 请注意,该端口必须是空闲的(没有其他系统服务在使用它),否则整个部署将失败。

Final note: since docker-compose will try to avoid to build your images every time you bring up the service, to force it to build a new one you have to append --build to the docker-compose up command. 最后说明:由于docker-compose会在每次启动服务时都尽量避免构建映像,因此要强制其构建新映像,必须将--build附加到docker-compose up命令中。

To bring down your deploy just use docker-compose down . 要停止部署,只需使用docker-compose down To delete all the persistent data related to your deploy (ie starting with a new db) append the -v flag at the end of the previous command. 要删除与部署相关的所有持久性数据(即从新的db开始),请在上一个命令的末尾附加-v标志。

Hope it helps! 希望能帮助到你!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 来自 .net 项目的 Docker 容器 IP 地址 - Docker container IP address from .net project 无法连接到 Asp.Net Core 上的 mysql 容器 - Unable to connect to mysql container on Asp.Net Core How to send a Label Print Job to a network CUPS/Raspberry Pi print server from a .NET Core Web API server docker container? - How to send a Label Print Job to a network CUPS/Raspberry Pi print server from a .NET Core Web API server docker container? 无法从 .NET Core WebAPI 容器连接到 MongoDB 容器,但可以正常连接 - Unable to connect to MongoDB container from .NET Core WebAPI container, but can connect normally 如何解决 ASP.NET Core Web API 项目中类库的 Windsor 容器依赖项? - How to resolve Windsor container dependencies for a class library in ASP.NET Core Web API project? 使用Web API获取IP地址 - Get IP Address with Web API 如何在ASP.NET Core 2.1中获取API模型中的用户IP地址? - How to get user IP address in API model in ASP.NET Core 2.1? Asp Net Web API 2.1 获取客户端 IP 地址 - Asp Net Web API 2.1 get client IP address 如何从在容器中运行的 ASP.NET 核心应用程序连接到具有集成安全性的 Windows 服务器上的 SQL 服务器 - How to connect from ASP.NET Core application running in a container to SQL Server on Windows Server with Integrated Security 无法从带有 ASP.NET Core 的 Docker 容器连接到 SQL Server 容器 - Can't connect from the Docker container with ASP.NET Core to SQL Server container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM