[英]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: 但是,我不知道该主机名使用什么值:
dotnet ef database ...
calls executed on my local shell can connect to the database - until my IP changes dotnet ef database ...
调用都可以连接到数据库-直到我的IP更改为止 localhost
then only dotnet ef database ...
commands executed on my local shell can connect to the database localhost
则只有在本地shell上执行的dotnet ef database ...
命令可以连接到数据库 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.