最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

.net - Api inaccessible inside docker network despite having ports exposed - Stack Overflow

programmeradmin0浏览0评论

I am trying to set up a vds and everything was fine up until the point I had to connect my api to an app. The api cannot connect to anything, nor on localhost, nor over the network. I wanted to use traefik as a reverse proxy and connect through that, but no luck. I tried debugging, exposing the ports on the network but this didn't help either. I discovered that while my api is running fine, it connected to the database as well, it is impossible to connect to it and if I add a health check it simply fails.

Here is my docker file:

ARG DOTNET_SDK=mcr.microsoft/dotnet/sdk:8.0

FROM ${DOTNET_RUNTIME} AS base
WORKDIR /app
EXPOSE 5000

FROM ${DOTNET_SDK} AS build
WORKDIR /src

COPY ["CassiniConnect.API/CassiniConnect.API.csproj", "CassiniConnect.API/"]
COPY ["CassiniConnect.Application/CassiniConnect.Application.csproj", "CassiniConnect.Application/"]
COPY ["CassiniConnect.Core/CassiniConnect.Core.csproj", "CassiniConnect.Core/"]

RUN dotnet restore "CassiniConnect.API/CassiniConnect.API.csproj"

COPY . .

ENV ASPNETCORE_URLS="http://+:5000"

WORKDIR /src/CassiniConnect.API
RUN dotnet publish "CassiniConnect.API.csproj" -c Release --no-restore -o /app/publish

FROM base AS final
WORKDIR /app

ENV ASPNETCORE_ENVIRONMENT=Production

COPY --from=build /app/publish .

ENTRYPOINT [ "dotnet", "CassiniConnect.API.dll" ]

This is for the .Net api and it builds fine.

Here is the docker-compose.yaml as well.

Here the db and treafik are setup correctly,the frontend upp as well and it is running on the intended ip address. The only problem is the backend api...

services:
  proxy:
    image: traefik:latest
    container_name: traefik
    command: 
      - "--providers.docker"
      - "--providers.docker.exposedbydefault=false"
      - "--providers.dockerwork=traefik"
      - "--entrypoints.web.address=:80"
      - "--entrypoints.websecure.address=:443"
      - "--entrypoints.web.http.redirections.entrypoint.to=websecure"
      - "--entrypoints.web.http.redirections.entrypoint.scheme=https"
      - "--certificatesresolvers.myresolver.acme.tlschallenge=true"
      - "--certificatesresolvers.myresolver.acme.httpchallenge=true"
      - "--certificatesresolvers.myresolver.acme.email=ujfalusiabel@cassini-.info"
      - "--certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json"
      - "--api.dashboard=true"
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - letsencrypt:/letsencrypt
      - /var/run/docker.sock:/var/run/docker.sock
    networks:
      - traefik

  db:
    image: postgres:latest
    container_name: cassini-db
    restart: always
    env_file:
      - .env  
    environment:
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - traefik
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "postgres"]
      interval: 10s
      timeout: 5s
      retries: 5

  api:
    image: cassini-api:latest
    container_name: cassini-api
    restart: always
    depends_on:
      db:
        condition: service_healthy
    env_file:
      - .env  
    environment:
      DefaultConnection: ${CONNECTION_STRING}
      JwtSettings__SecretKey: ${JwtSettings__SecretKey}
      JwtSettings__Issuer: ${JwtSettings__Issuer}
      JwtSettings__Audience: ${JwtSettings__Audience}
      ASPNETCORE_ENVIRONMENT: ${ASPNETCORE_ENVIRONMENT}
    volumes:
      - storage_data:/app/storage
    networks:
      - traefik
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.api.rule=Host(`api.cassini-.info`)"
      - "traefik.http.routers.api.entrypoints=websecure"
      - "traefik.http.routers.api.tls.certresolver=myresolver"
      - "traefik.http.services.api.loadbalancer.server.port=5000"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:5000/health"]
      interval: 10s
      timeout: 5s
      retries: 5

  webapp:
    image: cassini-app:latest
    container_name: cassini-app
    restart: always
    depends_on:
      api:
        condition: service_healthy
    environment:
      - NEXT_APP_API_URL=
    volumes:
      - webapp_data:/app/.next
    networks:
      - traefik
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.webapp.rule=Host(`cassini-.info`)"
      - "traefik.http.routers.webapp.entrypoints=websecure"
      - "traefik.http.routers.webapp.tls.certresolver=myresolver"
      - "traefik.http.services.webapp.loadbalancer.server.port=3000"

volumes:
  postgres_data:
  storage_data:
  webapp_data:
  letsencrypt:

networks:
  traefik:
    name: traefik
    driver: bridge

Any help is really appreciated, I am at my wits end after over a full day's work of debugging without any luck. Thank you very much

发布评论

评论列表(0)

  1. 暂无评论