How can I use .Net Core 3.0 image in Google Cloud?

こ雲淡風輕ζ 提交于 2020-04-13 17:41:46

问题


I'm using Google Cloud to host my .Net Core 2.2 application, but I want to update it to 3.0. My app.yaml looks like this

service: api
runtime: aspnetcore
env: flex

I know that I can specify the .Net Core version in runtime section. But Google Cloud Container Registry doesn't have .Net Core 3.0. I've checked it here.

Should I make a custom container then? I have zero experience with docker. Maybe there is a ready-to-go container somehow.

I didn't found any roadmap for update .Net Core images in public Container Registry.

@update

It's working!

My dockerfile looks like this:

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /app

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

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

# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY --from=build-env /app/out/ .
EXPOSE 8080
ENV ASPNETCORE_URLS=http://*:8080
ENV ASPNETCORE_ENVIRONMENT=production
ENV TAPTAKE_SEED=false
ENTRYPOINT ["dotnet", "MyProject.dll"]

And my new app.yaml

service: api
runtime: custom
env: flex
env_variables:
  ASPNETCORE_ENVIRONMENT: "production"

# Note this manual scaling is only for development
manual_scaling:
  instances: 1
resources:
  cpu: 1
  memory_gb: 4
  disk_size_gb: 10

And my cloudbuild.yaml:

steps:

# Build
- name: 'gcr.io/cloud-builders/dotnet'
  args: [ 'publish', '-c', 'Release' ]
  dir: 'MyProject'

# Migrations
- name: 'gcr.io/cloud-builders/dotnet'
  args: [ 'ef', 'database', 'update' , '--configuration', 'Production']
  dir: 'MyProject'
  env:
    - 'ASPNETCORE_ENVIRONMENT=production'

# DEPLOY
- name: 'gcr.io/cloud-builders/gcloud'
  args: ['app','deploy','MyProject/bin/Release/netcoreapp3.1/publish/app.yaml', '--verbosity=debug']
timeout: '1200s'


回答1:


Since you are already using the Flexible environment, one possible solution would be to set the runtime to custom and then use a Dockerfile to specify the image that you would like, which in this case is .Net Core 3.0.

As an example, the app.yaml file can be re-written like such:

service: api
runtime: custom
env: flex

We could also write the Dockerfile on top of the example given in the official docker docs

FROM mcr.microsoft.com/dotnet/core/sdk:3.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/core/aspnet:3.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "aspnetapp.dll"]

Again, this is only one of the possible solutions. Google has an article that describes 4 ways you can deploy a .Net Core application. Granted, it is a few years old, but concepts should still apply. As an overview, you can:

  • Deploy from Visual Studio directly using the Cloud Tools for Visual Studio extension
  • Deploy a Framework Dependent Deployment bundle with "dotnet publish"
  • Deploy to App Engine flexible environment with a custom Dockerfile
  • Deploy to Container Engine with a custom Dockerfile


来源:https://stackoverflow.com/questions/58505180/how-can-i-use-net-core-3-0-image-in-google-cloud

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!