First time using GCP and I'm trying to upload a .NET project.
I cannot manage to publish the app using the integrated tools from Visual Studio, "Google Cloud Tools" and I get this message "Failed to deploy project to App Engine Flex".
I also tried using Cloud Run or the Google Cloud SDK Shell using gcloud app deploy app.yml but i get this error now:
ERROR
ERROR: build step 0 "gcr.io/cloud-builders/docker" failed: step exited with non-zero status: 1
I know it's coming from the Dockerfile but I don't know how to use and write it. Here it is:
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
COPY./sims/sims.csproj.sims/sims.csproj
COPY .sln./
RUN dotnet restore
COPY . ./
RUN dotnet publish -c Release -o build --no-restore
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from= build./ build.
ENV ASPNETCORE_URLS = https://:8080
EXPOSE 8080
ENTRYPOINT ["dotnet", "sims.dll"]
Here's my .yaml file in case it might come from here:
runtime: aspnetcore
env: flex
# This sample incurs costs to run on the App Engine flexible environment.
# The settings below are to reduce costs during testing and are not appropriate
# for production use. For more information, see:
# https://cloud.google.com/appengine/docs/flexible/python/configuring-your-app-with-app-yaml
manual_scaling:
instances: 1
resources:
cpu: 1
memory_gb: 0.5
disk_size_gb: 10
Do you have any advice ?
Also, concerning these two files, I don't even know if they're located in the right repository: project tree
Thanks for reading
I got stuck on this for quite some time and i don't know why it doesn't work.
I looked into posts similair to mine but i can't fix it, (tried checking the firewall settings, using different ports).
I have a Dockerfile that looks like this:
# syntax=docker/dockerfile:1
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
COPY bin/Release/net6.0/publish/ App/
WORKDIR /App
ENTRYPOINT ["dotnet", "MonitorApplication1.dll"]
The application is a simple REST API with C# .NET Core 6. The database is a MS SQL Server Database.
The image and container seem fine but when i try to view it in the browser (using Chrome as standard, but also tried Edge) i get the ERR_EMPTY_RESPONSE error.
The log of the container (using Docker Desktop) only shows this:
{"EventId":14,"LogLevel":"Information","Category":"Microsoft.Hosting.Lifetime","Message":"Now listening on: http://[::]:80","State":{"Message":"Now listening on: http://[::]:80","address":"http://[::]:80","{OriginalFormat}":"Now listening on: {address}"}}
Update
I tried to create a new .NET 6 API with Docker Enabled but when i tried to run the container i still get the Error Empty Response.
I solved a similar problem by adding the ASPNETCORE_URLS environment variable, like this:
ENV ASPNETCORE_URLS=http://*:5000
First of all, I am sorry if I misinterpret what I get, I'm completly new to Docker.
So I'm developping a software to process csv files into a database, and everything runs in GCP. To run my code, I have access to a Cloud Run that can use Docker images.
Today I added 3 new files :
'BusinessLogic/Repository/AcademyRepository.cs'
'BusinessLogic/Repository/Interfaces/ISourceRepository.cs'
'BusinessLogic/Repository/SourceRepository.cs'
Prior to these modifications, I had to run 3 commands to publish my code on the cloud run :
dotnet build
gcloud builds submit --tag gcr.io/myproject/mytag
gcloud run deploy myservice gcr.io/myproject/mytag--platform managed
But since I added these files, I get the following error :
/usr/share/dotnet/sdk/3.1.401/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.DefaultItems.targets(295,5): error NETSDK1022: Duplicate 'Compile' items were included. The .NET SDK includes 'Compile' items from your project directory by default. You can either remove these items from your project file, or set the 'EnableDefaultCompileItems' property to 'false' if you want to explicitly include them in your project file. For more information, see https://aka.ms/sdkimplicititems. The duplicate items were: 'BusinessLogic/Repository/AcademyRepository.cs'; 'BusinessLogic/Repository/Interfaces/ISourceRepository.cs'; 'BusinessLogic/Repository/SourceRepository.cs' [/app/MyProject.csproj]
I saw on multiple threads to check .csproj files for weird ... entries but there was nothing like this in mine.
And here is my dockerfile :
# Use Microsoft's official build .NET image.
# https://hub.docker.com/_/microsoft-dotnet-core-sdk/
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-alpine AS build
WORKDIR /app
# Install production dependencies.
# Copy csproj and restore as distinct layers.
COPY *.csproj ./
RUN dotnet restore
# Copy local code to the container image.
COPY . ./
WORKDIR /app
# Build a release artifact.
RUN dotnet publish -c Release -o out # This step fails
# Use Microsoft's official runtime .NET image.
# https://hub.docker.com/_/microsoft-dotnet-core-aspnet/
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-alpine AS runtime
RUN echo 'http://dl-cdn.alpinelinux.org/alpine/v3.8/main' >> /etc/apk/repositories && apk update --no-cache && apk add --no-cache bash libc6-compat=1.1.19-r11
WORKDIR /app
COPY --from=build /app/out ./
# Run the web service on container startup.
ENTRYPOINT ["dotnet", "MyDll.dll"]
I tried running the command that fails outside of the gcloud builds submit command but nothing wrong happened
If I delete the files and comment the related code, the problem vanishes ... but I kinda need those files
EDIT :
Editing a csproj file from visual studio doesn't show you the actual file, it shows you a modified version. I edited the csproj with notepad, cleaned then recompiled the project but the problem is still here.
Found a workaround :
Here is a part of my architecture :
BusinessLogic
--- Repositories
--- Interface
The problem resides in the "Interface" folder. By deleting it and adding my interfaces in the parent folder (Repositories), the problem disappears. Still no idea how to solve the real problem though
Good day everyone,
I'm trying to containerize my ASP.NET Core Application. In my previous projects, this sample docker file works perfectly fine.
FROM microsoft/dotnet:sdk AS build-env
COPY . /app
WORKDIR /app/MyApp
RUN dotnet build
RUN dotnet publish -c Release -o out
FROM microsoft/dotnet:2.2-aspnetcore-runtime
EXPOSE 80
WORKDIR /app
COPY --from=build-env /app/MyApp/out .
ENTRYPOINT [ "dotnet","MyApp.dll" ]
But the problem is, I have a project application that uses a 3rd party assembly or dll (Covered by red marker).
In my local project solution, it compiles and runs alright. But when I'm trying to build my project image, the C# files that use that assembly return an error, it says The type or namespace name 'the_assembly' could not be found. This happens when the docker step is in RUN dotnet build,RUN dotnet restore or RUN dotnet publish...
Any help please?
Check your csproj file if it holds that dll reference as realitve location somewhere in your solution. Maybe you have it in some other place, so this file is not included in docker copy.
I am running a .net core app in a docker container. Here is my docker file (just to build a dev environment):
FROM microsoft/dotnet:1.0.1-sdk-projectjson
ENV ASPNET_ENV Development
COPY bin/Debug/netcoreapp1.0/publish/ /root/
EXPOSE 5000/tcp
ENTRYPOINT dotnet /root/AVP.WebApi.dll
I have an appSettings.Development.json file in the /publish/ folder. If I build and run this Docker file, I end up with a strange issue where the .NET app can't find the appsettings it needs to start (they're in the appSettings.Development.json file).
I have confirmed that from the command line in windows if I run dotnet publish/AVP.WebAPI.dll the app throws the same exceptions for missing configuration settings. However if I cd to /publish and run dotnet AVP.WebAPI.dll the app starts and runs just fine.
Any ideas how to change my docker file so it will run the app properly with the appSettings.Development.json values? (I've also tried to run the app with all the values copied into the regular appSettings.json files with no luck)
I've also tried running a the COPY command to / instead of /root and doing dotnet AVP.WebApi.dll as the entry point, but that results in the project being unable to find dependencies.
Try replacing this line:
ENV ASPNET_ENV Development
With this:
ENV ASPNETCORE_ENVIRONMENT Development
Your original Environment Variable name was used in older .NET Core, but has been changed. It can be a pain finding tutorials, etc. for .NET Core because of all of the changes that have happened since it first started!
Don't get me started on project.json files!
More info:
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments
As a follow up to everyone (I posted this is a comment originally), this is what ended up fixing it:
From what I can tell it looks like dotnet expects the appsettings files to be in the same directory it is run from. So I added COPY bin/Debug/netcoreapp1.0/publish/appsettings.json /appsettings.json to the dockerfile (this line copies the appsettings file to the directory below /root/ where I copied the publish folder to). Everything started working at this point. It appears that the dotnet executable runs from the directory below /root/ so it couldn't find it before, now that appsettings is in the same folder, it's all happy.
It late, but i think my solution will help other.
Step 1. Put appseting in folder "Settings/appsettings.json".
Here is my appsettings.json
{
"ConnectionString": "Data Source=local;Initial Catalog=mydb;User Id=username;Password=myStr0ngPassword#;"
}
Step 2. Edit code from asp netcore.
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
namespace Repos.Configs
{
public static class ConfigurationManager
{
public static string currentPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
public static IConfiguration AppSetting { get; }
static ConfigurationManager()
{
AppSetting = new ConfigurationBuilder()
.SetBasePath(currentPath)
.AddJsonFile("Settings/appsettings.json") // your path here
.Build();
}
}
}
And use AppSetting.
var connectionString = ConfigurationManager.AppSetting["ConnectionString"];
Step 3. now u must config your dockerfile, in my case, create by visual studio in linux container.
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
WORKDIR /src
COPY ["Api/Api.csproj", "Api/"]
COPY ["Repos/Repos.csproj", "Repos/"]
COPY ["DataContext/DataContext.csproj", "DataContext/"]
RUN dotnet restore "Api/Api.csproj"
COPY . .
WORKDIR "/src/Api"
RUN dotnet build "Api.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Api.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Api.dll"]
Step 4. Build image.
docker build -t tagForProject .
Step 5. Map volume and run your container from image.
docker run -p 44382:80 --volume c:\Docker\Volumes\MyProjectNetCore:/app/Settings --name nameWillDisplayInDockerDashboard -d tagForProject
OK, one problem here, because docker will override c:\Docker\Volumes\MyProjectNetCore to /app/Settings. So, you must put appseting.json to c:\Docker\Volumes\MyProjectNetCore.
If not, your app cant read appsetting because it not exist in docker volume.
Step 6. Restart your app in docker dashboard and see it work.
There are three problems i can think of why it can't find the appsettings:
They are not in the right folder in the container (did you copy the publish folder and does the publish folder contain the appsetting
You did not define using appsettings for the environment in the StartupClass: appSettings.${Environment}.json
It works locally because windows filesystem is case-insensitive and linux is case sensitive and thus it can't find the file. (check your capitalization).
I use docker-compose with .net 5, I put this on my yaml :
container_name: heroapi
environment:
- ASPNETCORE_ENVIRONMENT=alpha
for use appsettings.alpha.json file and it's work
Just came across this issue myself and the problem was the file is not being copied by default.
This was fixed by editing the .csproj file and adding :
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
This can also be done through UI by right clicking the appsettings file within visual studio, selecting properties and setting sopy to directory to "Always".
My problem was, through all this docker container stuff.. I never built the project in Release Mode, so the appsettings.json file in /bin/release/publish was very old.
I just hit this issue myself, and Sam's answer did solve it. Just to propose a different solution, you can also set the WORKDIR /root directive in the Dockerfile and then use ENTRYPOINT dotnet AVP.WebApi.dll which will call dotnet in the context of the root directory.
Full Dockerfile:
FROM microsoft/dotnet:1.0.1-sdk-projectjson
ENV ASPNET_ENV Development
COPY bin/Debug/netcoreapp1.0/publish/ /root/
WORKDIR /root
EXPOSE 5000/tcp
ENTRYPOINT dotnet AVP.WebApi.dll
for compose:
environment:
- ASPNETCORE_ENVIRONMENT=Production
For Docker:
ENV ASPNETCORE_ENVIRONMENT: "Production"
there is a quick and easy way to Update/edit the AppSettings.json file:
for example my AppSettings.json file :
{
"GrpcOption": {
"ServerAddress": "oldurl"
},
}
now i can modify it by -e command
docker run -e "GrpcOption:ServerAddress=newurl" [imagename]