简体   繁体   English

对于带有docker-compose的ASP.NET应用程序,我应该使用哪个数据库主机名?

[英]What database hostname should I use for ASP.NET apps with docker-compose?

I am trying to set up an ASP.NET project (based on this tutorial) using Visual Studio for Mac, and need to run Docker containers for the web app and the SQL Server database. 我正在尝试使用Visual Studio for Mac设置一个ASP.NET项目(基于教程),并且需要为Web应用程序和SQL Server数据库运行Docker容器。

Using the following docker-compose.yaml , both containers run successfully and I can load the web app landing page at http://localhost:5000 : 使用以下docker-compose.yaml ,两个容器均成功运行,并且我可以在http:// localhost:5000加载Web应用程序登录页面:

version: '3.4'

services:
  razorpagesmovie:
    image: ${DOCKER_REGISTRY-}razorpagesmovie
    build:
      context: .
      dockerfile: RazorPagesMovie/Dockerfile
    ports:
      - "5000:5000"
    depends_on:
      - mssql

  mssql:
    image: microsoft/mssql-server-linux
    ports:
      - "1433:1433"
    environment:
      SA_PASSWORD: "L0ngpassword"
      ACCEPT_EULA: "Y"

The connection string in appsettings.json is appsettings.json的连接字符串为

Server=<host>,1433;Database=Movies;User=sa;Password=L0ngpassword;

However, I don't know what value to use for the hostname: 但是,我不知道该主机名使用什么值:

  1. If I hardcode my machine's IP address, then both the web app AND dotnet ef database ... calls executed on my local shell can connect to the database - until my IP changes 如果我对机器的IP地址进行硬编码,则在本地外壳程序上执行的Web应用程序和dotnet ef database ...调用都可以连接到数据库-直到我的IP更改为止
  2. If I use localhost then only dotnet ef database ... commands executed on my local shell can connect to the database 如果我使用localhost则只有在本地shell上执行的dotnet ef database ...命令可以连接到数据库
  3. If I use mssql , then only the web app container can connect to the database 如果我使用mssql ,则只有Web应用程序容器可以连接到数据库

Is there a more robust approach than hardcoding my IP? 有没有比对我的IP进行硬编码更健壮的方法?

Assuming that your appsettings.json file has json key ConnectionString . 假设您的appsettings.json文件具有json键ConnectionString

You can add an environment variable for connection string to that container. 您可以将连接字符串的环境变量添加到该容器。 Use DB_IP or put service name mssql of the DB container. 使用DB_IP或放置数据库容器的服务名称mssql

version: '3.4'

services:
  razorpagesmovie:
    image: ${DOCKER_REGISTRY-}razorpagesmovie
    build:
      context: .
      dockerfile: RazorPagesMovie/Dockerfile
    ports:
      - "5000:5000"
    environment:
      - ConnectionString=${DB_IP:-Server=mssql;Database=Movies;User=sa;Password=L0ngpassword} 
    depends_on:
      - mssql

  mssql:
    image: microsoft/mssql-server-linux
    ports:
      - "1433:1433"
    environment:
      SA_PASSWORD: "L0ngpassword"
      ACCEPT_EULA: "Y"

暂无
暂无

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

相关问题 使用 docker-compose 将带有 MySQL 持久化数据库的 ASP.NET Core Web 应用 Docker 化为两个单独的容器 - Dockerize an ASP.NET Core Web App with MySQL persisting Database as two separate containers with docker-compose 我应该使用哪种模式从Asp.Net应用程序中的远程源加载数据? - What pattern should I use to load data from remote sources in Asp.Net apps? 我的docker-compose asp.net核心项目未正确运行,我得到ERR_CONNECTION_REFUSED - My docker-compose asp.net core project is not properly running, I get ERR_CONNECTION_REFUSED Docker-Compose YML 与 ASP.NET Core 2.2 Web API 和 SQL Server 2017 - Docker-Compose YML with ASP.NET Core 2.2 Web API and SQL Server 2017 如何解决 docker-compose 环境变量不起作用 ASP.Net Core MVC - How to solve docker-compose environment variables not working ASP.Net Core MVC System.ArgumentNullException:值不能为 null。(参数“connectionString”)asp.net 核心 Docker-compose - System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString') asp.net Core Docker-compose ASP.NET docker-compose 应用程序未出现在分配的端口上 - ASP.NET docker-compose app not coming up on assigned PORT Docker-compose:如何为从内部和外部有效的 docker 实例使用主机名? - Docker-compose: how to use a hostname for a docker instance valid both from inside and outside? 如何在 docker-compose 命令中将 appsettings.json 值替换为 .env 变量以启动 ASP.NET 核心 MVC 应用程序? - How do I replace appsettings.json values with .env variable in docker-compose command to launch an ASP.NET Core MVC app? Docker-Compose 连接被 ASP-NET Core 5 拒绝 - Docker-Compose Connection Refused with ASP-NET Core 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM