title: ASP.Net Core Docker container can not find Postgres database
body: <p>I'm trying to Dockerize my entire web app, and I'm starting with the backend. The server is an ASP.Net Core 2.2 web API, with a PostgreSQL database. However, try as I might, I cannot get the backend up and running.</p>
  
  <p>As far as I can tell, the PostgreSQL db is not the issue. I can access it on the local machine once it's up, and I can enter it to ensure the correct db and user are there. But I keep getting the following error from the ASP server after waiting for docker-compose to run:</p>
  
  <pre><code>Application startup exception: System.AggregateException: One or more errors occurred. (Connection refused) ---&gt; System.Net.Sockets.SocketException: Connection refused
  </code></pre>
  
  <p>I've found next to no information on this error, though someone did suggest that it is due to the server not being able to find anything on the port provided, but I can't see how it can't.</p>
  
  <p>Docker-compose.yml</p>
  
  <pre><code>version: '3.7'
  
  services:
  
    webapi:
      image: 'webapi'
      container_name: 'webapi'
      build:
        context: .
        dockerfile: Dockerfile
      environment:
        DB_CONNECTION: "Server=postgres;Port=5432;Database=asg;User Id=asgwebapi;Password=admin;"
      ports:
       - "5000:5000"
      depends_on:
       - postgres
      networks:
       - webapp-network
  
    postgres:
      image: postgres
      container_name: 'postgres'
      environment:
        POSTGRES_USER: "asgwebapi"
        POSTGRES_PASSWORD: "admin"
        POSTGRES_DB: "asg"
      ports:
        - "5432:5432"
      networks:
        - webapp-network
  
  networks:
    webapp-network:
      driver: bridge
  </code></pre>
  
  <p>Dockerfile for the server:</p>
  
  <pre><code>FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build
  WORKDIR /app
  
  # Copy csproj and restore as distinct layers
  COPY Core/Core.csproj ./
  RUN dotnet restore
  
  # Copy everything else and build
  COPY . ./
  RUN dotnet publish Core/Core.csproj -c Release -o out
  
  # Build runtime image
  FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS runtime
  WORKDIR /app
  COPY --from=build /app/Core/out .
  ENTRYPOINT ["dotnet", "Core.dll"]
  </code></pre>
  
  <p>I'm using Npgsql on the server, which is why those connections strings are set like that. I'm still a bit confused as towards what the "Server" field should be set to. Most examples have it set to be the same as the name for the Postgres container, but that fixes no issues for me. And while I don't necessarily need to connect to the Postgres container from the host, I know that I can't unless "Server" is set to localhost.</p>
  
  <p>The server immediately fails upon start, as it looks for the database to populate with test data and run migrations, but it always has the same "Connection refused" error that I cannot get around. Currently, I can use Postgres through the cli with:</p>
  
  <pre><code>docker exec -it postgres psql dbname=asg asgwebapi
  </code></pre>
  
  <p>and I can also connect to the db and run the server locally, without issue. So not sure why the Docker container for the server can't find it, if that's even the issue.</p>