一、系统环境
windows10
Visual Studio 2019
.ASP.Net core 3.1
Docker Desktop
二、步骤
1、创建ASP.NET core新项目,Visual Studio 2019->创建新项目->ASP.NET Core Web 应用程序->选中 启用Docker支持 ->创建
启用Docker支持后,可以选择目标平台是Linux或windows,决定生成docker镜像的基本内核。
创建方案后,可以看到包含了dockerfile文件,其内容如下,描述创建镜像过程 - FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
- WORKDIR /app
- EXPOSE 80
- EXPOSE 443
- FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
- WORKDIR /src
- COPY ["MvcMovie.csproj", ""]
- RUN dotnet restore "./MvcMovie.csproj"
- COPY . .
- WORKDIR "/src/."
- RUN dotnet build "MvcMovie.csproj" -c Release -o /app/build
- FROM build AS publish
- RUN dotnet publish "MvcMovie.csproj" -c Release -o /app/publish
- FROM base AS final
- WORKDIR /app
- COPY --from=publish /app/publish .
- ENTRYPOINT ["dotnet", "MvcMovie.dll"]
复制代码
2、创建镜像
编码测试完成后,可以选择 Docker 运行
Visual Studio 2019会自动生成目标镜并在本地docker中运行
3、将docker 镜像发布至hub.docker.com
在解决方案上右键->发布,根据提示输入hub.docker.com上注册的用户名和密码,设置tag标签默认为latest,然后按发布,Visual Studio会自动将镜像上传至hub.docker.com
4、使用
在Docker中使用,直接pull 就可以使用
在Kubernetes中使用需先编辑.yaml文件
一个Web MvcMovie的.yam示例l文件如下 - ---
- apiVersion: apps/v1
- kind: Deployment
- metadata:
- namespace: hrsoft-web
- name: web-mvcmovie
- annotations:
- k8s.eip.work/workload: web-mvcmovie
- k8s.eip.work/displayName: web-mvcmovie
- k8s.eip.work/service: ClusterIP
- k8s.eip.work/ingress: 'true'
- labels:
- k8s.eip.work/layer: web
- k8s.eip.work/name: web-mvcmovie
- spec:
- selector:
- matchLabels:
- k8s.eip.work/layer: web
- k8s.eip.work/name: web-mvcmovie
- template:
- metadata:
- labels:
- k8s.eip.work/layer: web
- k8s.eip.work/name: web-mvcmovie
- spec:
- imagePullSecrets:
- - {}
- initContainers: []
- containers:
- - image: 'zhoujy2hrsoft/mvcmovie'
- imagePullPolicy: Always
- name: web-mvcmovie
- volumeMounts: []
- resources: {}
- env: []
- volumes: []
- replicas: 1
- ---
- apiVersion: v1
- kind: Service
- metadata:
- namespace: hrsoft-web
- name: web-mvcmovie
- annotations:
- k8s.eip.work/workload: web-mvcmovie
- k8s.eip.work/displayName: web-mvcmovie
- labels:
- k8s.eip.work/layer: web
- k8s.eip.work/name: web-mvcmovie
- spec:
- selector:
- k8s.eip.work/layer: web
- k8s.eip.work/name: web-mvcmovie
- type: ClusterIP
- ports:
- - port: 80
- targetPort: 80
- protocol: TCP
- name: mawfrp
- nodePort: 0
复制代码
其在Kubernetes中的运行情况如下
参考文档:
https://docs.microsoft.com/zh-cn/aspnet/core/host-and-deploy/docker/?view=aspnetcore-3.1 来源:https://blog.csdn.net/zhujisoft/article/details/106160596 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |