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

∥☆過路亽.° 提交于 2021-02-10 15:30:38

问题


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.

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

Below is the dockerfile that Visual Studio generated for me.

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

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));
        }

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.

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

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

回答1:


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'

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.

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.

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.

Hope it helps!



来源:https://stackoverflow.com/questions/52676741/connect-to-mysql-container-from-web-api-net-core-container-how-to-get-ip-addre

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!