I’m trying to make my application work with Docker Compose but every time I run docker-compose up -d
, my API crashes at startup (I am TRYING to migrate my database with EF Core in case I am not in production).
I get this error:
2024-05-15 17:24:18 Unhandled exception. Npgsql.NpgsqlException (0x80004005): Failed to connect to 192.168.208.2:5432
2024-05-15 17:24:18 ---> System.Net.Sockets.SocketException (111): Connection refused
2024-05-15 17:24:18 at Npgsql.Internal.NpgsqlConnector.Connect(NpgsqlTimeout timeout)
2024-05-15 17:24:18 at Npgsql.Internal.NpgsqlConnector.Connect(NpgsqlTimeout timeout)
2024-05-15 17:24:18 at Npgsql.Internal.NpgsqlConnector.RawOpen(SslMode sslMode, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken, Boolean isFirstAttempt)
2024-05-15 17:24:18 at Npgsql.Internal.NpgsqlConnector.<Open>g__OpenCore|213_1(NpgsqlConnector conn, SslMode sslMode, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken, Boolean isFirstAttempt)
2024-05-15 17:24:18 at Npgsql.Internal.NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
2024-05-15 17:24:18 at Npgsql.UnpooledDataSource.Get(NpgsqlConnection conn, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
2024-05-15 17:24:18 at Npgsql.NpgsqlConnection.<Open>g__OpenAsync|42_0(Boolean async, CancellationToken cancellationToken)
2024-05-15 17:24:18 at Npgsql.NpgsqlConnection.Open()
2024-05-15 17:24:18 at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection(Boolean errorsExpected)
2024-05-15 17:24:18 at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternal(Boolean errorsExpected)
2024-05-15 17:24:18 at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
2024-05-15 17:24:18 at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlDatabaseCreator.Exists(Boolean async, CancellationToken cancellationToken)
2024-05-15 17:24:18 at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlDatabaseCreator.Exists(Boolean async, CancellationToken cancellationToken)
2024-05-15 17:24:18 at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlDatabaseCreator.Exists()
2024-05-15 17:24:18 at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
2024-05-15 17:24:18 at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.GetAppliedMigrations()
2024-05-15 17:24:18 at Npgsql.EntityFrameworkCore.PostgreSQL.Migrations.Internal.NpgsqlMigrator.Migrate(String targetMigration)
2024-05-15 17:24:18 at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.Migrate(DatabaseFacade databaseFacade)
2024-05-15 17:24:18 at Program.<Main>$(String[] args) in /src/QuizyZunaAPI.Api/Program.cs:line 31
This is my Docker Compose:
version: '3.4'
services:
quizyzuna-db:
container_name: Database
image: postgres:alpine
restart: always
environment:
- POSTGRES_DB=quizyzunadb
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
volumes:
- ./.containers/database:/var/lib/postgressql/data
ports:
- 5432:5432
quizyzuna-api:
container_name: API
image: ${DOCKER_REGISTRY-}quizyzunaapi
build:
context: src/
depends_on:
- quizyzuna-db
And the docker compose override :
version: '3.4'
services:
quizyzuna-api:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_HTTP_PORTS=8080
- ASPNETCORE_HTTPS_PORTS=8081
- ConnectionStrings__Database=Host=Database;Username=postgres;Password=postgres;Database=quizyzunadb;
ports:
- "8080"
- "8081"
volumes:
- ${APPDATA}/Microsoft/UserSecrets:/home/app/.microsoft/usersecrets:ro
- ${APPDATA}/ASP.NET/Https:/home/app/.aspnet/https:ro
In my compose i try to erase my connection string to give it the docker built one and it seems? Like it works.
I can connect through my localhost with DBeaver on the database.
I tried to search for common mistakes but most of them are developers writing localhost as host in the environment.
My connection string seems like it’s ok and it seems like the connection string is correctly replaced but I don’t what to search for. Do you have a clue?
2
Answers
Is your quizyzuna-api using the correct host in its connection string.
Try to set the host in the connection string to quizyzuna-db
Also, your log indicates that the api is trying to connect to 192.168.208.2. You can use docker inspect to see the IP address of the postgres container to verify if that is 192.168.208.2
depends_on
only waits for the database container to be started. Not for it to be ready to accept connections. The database does some work at startup, so it takes a short time from the container startsuntil it’s ready to accept connections.
You can add a healthcheck to the database container and let your api container wait for it to enter a healthy state and then it should accept the connection.
Something like this: