skip to Main Content

After many hours of frustrating attempts I’m asking help:

I have a .NET Core App that regardless of my searches online doesn’t seem to start in Docker.

It should be working because is the final assignment of a Docker course so I’m pretty sure that I’m miscomputing my Dockerfile. Here it is:

# syntax=docker/dockerfile:1
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:3.1
WORKDIR /app
COPY --from=build-env /app/out .
EXPOSE 5432
ENTRYPOINT ["dotnet", "backend.dll"]

and then my appsettings.json:

{
  "ConnectionStrings": {
    "DefaultConnection": "Host=host.docker.internal;database=message;Username=postgres;Password=6AX3PwqM47fZAHw9;"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

The latter was already compiled in the course.

The error that the console is throwing is:

fail: backend.MessageDbContext[0]
      An error occurred while migrating the database.
Npgsql.NpgsqlException (0x80004005): Exception while connecting
 ---> System.Net.Sockets.SocketException (111): Connection refused

And then many lines of output.

Any help is really appreciated, the final assignment of this course is dockerizing this app and put it in connection with a Postgres database and a Nginx web server.

EDIT:

This is the docker-compose.yml:

version: '3.9'

services:
  db:
    container_name: 'postgres'
    image: "postgres"
    env_file:
      - database.env
    ports:
      - "5432"
    networks:
      - marco-network  

  backend:
    container_name: 'backend'
    depends_on:
      - db
    build: 
      context: ./backend/
      dockerfile: ./Dockerfile
    volumes:
      - ./src:/usr/share/nginx/html
    environment:
      - ConnectionStrings:DefaultConnection=Host=db;database=message;Username=postgres;Password=6AX3PwqM47fZAHw9
    ports:
      - "5000"
    networks:
      - marco-network
    
  frontend:
    container_name: 'frontend'
    depends_on:
      - db
    build:
      context: ./frontend/
      dockerfile: ./Dockerfile
    ports:
      - 8080:80
    networks:
      - marco-network
  
  

networks:
  marco-network:
    # driver: bridge

EDIT # 2:

New docker-compose.ymlconfiguration:

version: '3.9'

services:
  db:
    container_name: 'message'
    build: 
      context: ./postgres/
      dockerfile: ./Dockerfile
    ports:
      - 5432:5432
    networks:
      - marco-network

  backend:
    container_name: 'backend'
    depends_on:
      - message
    build: 
      context: ./backend/
      dockerfile: ./Dockerfile
    volumes:
      - ./src:/usr/share/nginx/html
    environment:
      - ConnectionStrings__DefaultConnection=Host=db;database=message;Username=postgres;Password=6AX3PwqM47fZAHw9
    networks:
      - marco-network
    
  frontend:
    container_name: 'frontend'
    depends_on:
      - backend
    build:
      context: ./frontend/
      dockerfile: ./Dockerfile
    ports:
      - 8080:80
    networks:
      - marco-network
  
  
networks:
  marco-network:
    name: marco-network
    driver: bridge

No success.

2

Answers


  1. You have two connection strings defined that aren’t the same. The one in your docker compose file looks correct (‘host=db’).

    However, you use a colon to separate the identifiers. You should use double underscores according to this.

    Try changing your docker-compose file environment variable to

      - ConnectionStrings__DefaultConnection=Host=db;database=message;Username=postgres;Password=6AX3PwqM47fZAHw9
    
    Login or Signup to reply.
  2. You need to modify port settings for the backend app.

    Ports:
     - 5000:80
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search