从Web Api .Net核心容器连接到MySQL容器?如何获得IP地址? [英] Connect to MySQL container from Web Api .Net Core Container? How to get Ip Address?

查看:82
本文介绍了从Web Api .Net核心容器连接到MySQL容器?如何获得IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个菜鸟问题,但是我在理解如何使.Net Core网站连接到MySql容器方面遇到困难.因此,在某些背景下,MySql和.Net核心网站都位于各自的容器中.我已经启动了MySql容器并设置了root帐户才能工作.我在.Net Core项目中使用实体框架.

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. So some background, both the MySql and the .Net core website are in their separate containers. I have already started the MySql container and setup the root account to work. I am using Entity Framework inside of .Net Core project.

我使用以下语句创建了MySql容器: docker run --name mysql_container -d -p 3306:3306

I created the MySql container using this statement: docker run --name mysql_container -d -p 3306:3306

下面是Visual Studio为我生成的dockerfile.

Below is the dockerfile that Visual Studio generated for me.

那么,如果IP可以更改,我要告诉我的.Net Core程序是MySql容器的IP地址吗?

So what do I tell my .Net Core program to is the IP address of the MySql container if the IP can change?

.Net Core程序内部:

Inside of .Net Core Program:

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

如果我编写GetDBAddress函数,那里面有什么?我不能简单地返回本地主机,因为它是另一个Docker容器?截至目前,我尝试使用localhost并拒绝连接.但是我可以使用工作台连接到MySql数据库.

If I write the GetDBAddress function what goes in there? I cannot simply return localhost because it's another docker container? As of right now I am trying to use localhost and I get connection refused. But I am able to connect to the MySql db using workbench.

我也不确定,但是可以将这两个设置组合到某个文件中吗,我认为它们可能被称为docker-compose文件?

Also I am not sure but can these two setups be combined into some file I think they're called docker-compose files maybe?

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"]

推荐答案

如果您启动了MySQL公开了端口,则您应该能够通过端口 localhost 连接到它3306 .否则,按照您的建议,可以设置 docker-compose文件.该文件通常包含您的应用程序需要运行的所有配置.因此,例如,适合您的应用程序的配置(注意:由于您尚未指定MySQL,因此我假设您使用的是MySQL 5.7)可能是:

If you've launched MySQL exposing the ports you should be able to reach it connecting from localhost, with the port 3306. Otherwise, as you suggested, there is the possibility to set up a 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:

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'

要启动和部署您的应用程序,现在您需要输入一个终端: docker-compose up 这将启动您的部署.请注意,此处未暴露任何端口:只有您的服务才能从 db:3306 访问数据库(您无需按IP进行引用,但是可以使用服务名称访问其他服务).为了进行调试,您仍然可以打开数据库端口,并在 image :

To launch and deploy your application, now you need to type in a terminal: docker-compose up This will bring up your deploy. 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). For debug purposes, you can still open your db ports adding this line under 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.

最后一点:由于docker-compose会在每次启动服务时都尽量避免构建映像,因此要强制其构建新映像,必须将-build 附加到 docker-compose up 命令.

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 down .要删除与部署相关的所有持久性数据(即从新数据库开始),请在上一个命令末尾附加 -v 标志.

To bring down your deploy just use docker-compose down. To delete all the persistent data related to your deploy (i.e. starting with a new db) append the -v flag at the end of the previous command.

希望有帮助!

这篇关于从Web Api .Net核心容器连接到MySQL容器?如何获得IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆