简体   繁体   中英

.Net Core Console App not running in Docker container

My.Net Core 2.1 Console App works fine when debugging in Visual Studio. Logs are posting fine to Elastic Search and visible in Kabana. When I try and deploy this app in a container I am not getting any logs posted to ES. Also I can see no Docker logs when I type docker logs <container-id> - I have included my Program Class and Docker file below. I am new to Docker, is there anything obvious that I have missed? Do I need to open some ports or something? Is it OK using an appsettings.json file in Docker like this? Could this be causing issues?

using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using RizbotCore.BitmexTrader.Controllers;
using RizbotCore.BitmexTrader.Services;
using Serilog;
using Serilog.Sinks.Elasticsearch;

namespace RizbotCore.BitmexTrader
{
    class Program
    {
        static async Task Main(string[] args)
        {
            //setup our DI
            var serviceProvider = new ServiceCollection()
                .AddLogging()
                .AddSingleton<ITraderMain, TraderMain>()
                .BuildServiceProvider();

            // Adding JSON file into IConfiguration.
            IConfiguration config = new ConfigurationBuilder()
                 .AddJsonFile("appsettings.json", true, true)
                 .Build();

            // Read configuration
            var appSettings = config.GetSection("ElasticConfiguration");
            string esURL = appSettings["ES_URL"];

            // Set up Logging
            Log.Logger = new LoggerConfiguration()
             .MinimumLevel.Debug()
             .Enrich.FromLogContext()
             .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(esURL))  
             {
                 AutoRegisterTemplate = true,
             })
             .CreateLogger();

            Log.Information("Starting Up...");

            // Start Main Manager
            var traderMain = serviceProvider.GetService<ITraderMain>();
            await traderMain.Start(serviceProvider);

            Console.ReadLine();
        }
    }
}

Docker file:

WORKDIR /app

FROM mcr.microsoft.com/dotnet/core/sdk:2.1-stretch AS build
WORKDIR /src
COPY RizbotCore.Trader/RizbotCore.BitmexTrader.csproj RizbotCore.Trader/
COPY RizBotCore.DAL/RizBotCore.DAL.csproj RizBotCore.DAL/
RUN dotnet restore "RizbotCore.Trader/RizbotCore.BitmexTrader.csproj"
COPY . .
WORKDIR "/src/RizbotCore.Trader"
RUN dotnet build "RizbotCore.BitmexTrader.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "RizbotCore.BitmexTrader.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "RizbotCore.BitmexTrader.dll"]

You need to expose the required ports in the dockerfile for all the communications you need ie EXPOSE 80 . Then when performing docker run over the image you just created out of the dockerfile, you will need to map the exposed ports outside ie docker run -p 80:80 <yourImageName>

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