简体   繁体   中英

.net core can not connect to postgres (docker compose)

I have a docker compose file which should build:

Database (Postgres)

API (.net Core)

FrontEnd (Vue)

docker-compose.yml

    version: '3.8'

networks:
  production:
    driver: bridge
  test:
    driver: bridge

docker-compose.test.yml

version: '3.8'

services:
  postgres_test:
    image: postgres:11.4
    container_name: postgres_test
    environment:
      POSTGRES_USER: coinManager
      POSTGRES_PASSWORD: postgresTest
      POSTGRES_DB: coinManagerTest
    restart: always
    volumes:
      - /coinManager/postgres/test/data:/var/lib/postgresql/data
    ports:
      - '5433:5432'
    networks:
      - test

  web_test:
    build: ./papasmuenzenbackend/.
    container_name: api_test
    ports:
      - '50599:80'
    links:
      - postgres_test
    depends_on:
      - 'postgres_test'
    networks:
      - test

  client_test:
    build: ./papasmuenzenfrontend/.
    container_name: vue_test
    ports:
      - '8081:8080'
    networks:
      - test

Starting with this script:

docker-compose -p papasmuenzen -f docker-compose.yml -f docker-compose.test.yml up -d --build

It looks like the database is starting fine, because i can connect to it with pgAdminpgAdmin

When docker-compose starts the .net core project, it fails to start when trying to migrate the database

    #region Database
    using (var dbContext = serviceProvider.GetService<CoinContext>())
        dbContext.Database.Migrate();
    #endregion

Connection String:

   "CoinDatabaseConnection": "Server=postgres_test;Port=5433;Database=coinManagerTest;username=coinManager;password=postgresTest;Integrated Security=true;Pooling=true;Timeout=300"

Exception:

    System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (00000005, 0xFFFDFFFF): Name or service not known

at System.Net.Dns.InternalGetHostByName(String hostName)

at System.Net.Dns.GetHostAddresses(String hostNameOrAddress)

at Npgsql.NpgsqlConnector.Connect(NpgsqlTimeout timeout)

at Npgsql.NpgsqlConnector.RawOpen(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)

at Npgsql.NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)

at Npgsql.NpgsqlConnection.<>c__DisplayClass32_0.<<Open>g__OpenLong|0>d.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

at Npgsql.NpgsqlConnection.Open()

at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlDatabaseCreator.Exists()

at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()

at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)

at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.Migrate(DatabaseFacade databaseFacade)

at CoinManager.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider serviceProvider) in /app/CoinManager/Startup.cs:line 86

at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)

at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)

at Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>c__DisplayClass4_0.<Build>b__0(IApplicationBuilder builder)

at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass13_0.<UseStartup>b__2(IApplicationBuilder app)

at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>c__DisplayClass0_0.<Configure>g__MiddlewareFilterBuilder|0(IApplicationBuilder builder)

at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)

at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)

Unhandled exception. System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (00000005, 0xFFFDFFFF): Name or service not known

at System.Net.Dns.InternalGetHostByName(String hostName)

at System.Net.Dns.GetHostAddresses(String hostNameOrAddress)

at Npgsql.NpgsqlConnector.Connect(NpgsqlTimeout timeout)

at Npgsql.NpgsqlConnector.RawOpen(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)

at Npgsql.NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)

at Npgsql.NpgsqlConnection.<>c__DisplayClass32_0.<<Open>g__OpenLong|0>d.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

at Npgsql.NpgsqlConnection.Open()

at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlDatabaseCreator.Exists()

at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()

at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)

at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.Migrate(DatabaseFacade databaseFacade)

at CoinManager.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider serviceProvider) in /app/CoinManager/Startup.cs:line 86

at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)

at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)

at Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>c__DisplayClass4_0.<Build>b__0(IApplicationBuilder builder)

at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass13_0.<UseStartup>b__2(IApplicationBuilder app)

at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>c__DisplayClass0_0.<Configure>g__MiddlewareFilterBuilder|0(IApplicationBuilder builder)

at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)

at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)

at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)

at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)

at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)

at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)

at CoinManager.Program.Main(String[] args) in /app/CoinManager/Program.cs:line 17

I removed the network configurations. My docker compose file now looks like this and it works:

  version: '3.8'

        services:
          postgres_test_db:
            image: postgres:11.4
            container_

name: postgres_test
        environment:
          POSTGRES_USER: coinManager
          POSTGRES_PASSWORD: postgresTest
          POSTGRES_DB: coinManagerTest
        restart: always
        volumes:
          - /coinManager/postgres/test/data:/var/lib/postgresql/data
        ports:
          - '5433:5432'

      web_test:
        build: ./papasmuenzenbackend/.
        container_name: api_test
        ports:
          - '50599:80'
        depends_on:
          - postgres_test_db
        environment:
          - ASPNETCORE_ENVIRONMENT=Test

      client_test:
        build: ./papasmuenzenfrontend/.
        container_name: vue_test
        restart: always
        ports:
          - '8081:8080'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM