Kailash P
Kailash P

Reputation: 81

Asp .NET MVC Core application not running in Windows Docker container

I am trying to deploy an asp.net core mvc application in the docker container, when i run the application i get the following error in the docker logs

fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware1 An unhandled exception has occurred while executing the request. System.ArgumentNullException: Value cannot be null. Parameter name: connectionString

I have mentioned the connection string in my appsettings.json file but still it does not fetch it correctly.

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "DefaultConnection": "<<< AZURE-SQL CONNECTION STRING >>>"
  }
}

Startup.cs

public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });


            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            services.AddMvc().AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());

            services.AddScoped<IBusinessLogic, BusinessLogic>();
            services.AddScoped<IDataRepository, DataRepository>();

var connection = Configuration.GetConnectionString("DefaultConnection");
            services.AddDbContext<DataContext>(options => options.UseSqlServer(connection));
            services.AddScoped<DataContext>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Mvc}/{action=Design}/{id?}");
            });
        }

Published folder

Published folder

Dockerfile

FROM microsoft/dotnet:2.1-aspnetcore-runtime
EXPOSE 80

COPY  /publish/ /app

ENTRYPOINT ["dotnet", "MVC_Module_Designer.dll"]

Docker commands used to publish

docker build -t webapp .

docker run -p 8080:80 webapp

Am i missing out anything for deployment of asp.net mvc core application in docker container ?.

Upvotes: 0

Views: 1215

Answers (1)

Kailash P
Kailash P

Reputation: 81

I have found the issue that i have made, its in the Docker file, after copying the published folder to app directory, did not change it to that. Hence it resulted in that error.

Correct docker file

FROM microsoft/dotnet:2.1-aspnetcore-runtime
EXPOSE 80

WORKDIR /app

COPY  /publish .

ENTRYPOINT ["dotnet", "MVC_Module_Designer.dll"]

Upvotes: 0

Related Questions