操作步骤
Asp.Net Core 运行在Linux的Docker容器中
基础命令参考
https://juejin.cn/post/7154440096219922462
自定义注册中心参考
https://www.cnblogs.com/anliven/p/14515266.html
一、创建带有Dockerfile的Asp.Net Core Web API项目
配置如下:
二、配置项目参数以及Docker运行的相关参数
根据需要修改三个文件
为保证设置生效前两个文件属性中 复制到输出目录 设置为 始终复制
1、Dockerfile
源文件:
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["UseDockerDemo/UseDockerDemo.csproj", "UseDockerDemo/"]
RUN dotnet restore "UseDockerDemo/UseDockerDemo.csproj"
COPY . .
WORKDIR "/src/UseDockerDemo"
RUN dotnet build "UseDockerDemo.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "UseDockerDemo.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "UseDockerDemo.dll"]
修改后的文件
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
ENV DOTNET_URLS=http://+:2080
EXPOSE 2080
#EXPOSE 2443
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["UseDockerDemo/UseDockerDemo.csproj", "UseDockerDemo/"]
RUN dotnet restore "UseDockerDemo/UseDockerDemo.csproj"
COPY . .
WORKDIR "/src/UseDockerDemo"
RUN dotnet build "UseDockerDemo.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "UseDockerDemo.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "UseDockerDemo.dll"]
参数说明:Dockerfile文件默认监听http为80端口,https为443端口
指定环境变量DOTNET_URLS可以更改http监听端口
因此EXPOSE的暴露端口应改为与监听端口一致
2、项目运行设置文件launchSettings.json的Docker部分
源文件
"Docker": {
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
"environmentVariables": {
"ASPNETCORE_URLS": "https://+:443;http://+:80"
},
"publishAllPorts": true,
"useSSL": true
}
修改后文件
"Docker": {
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
"environmentVariables": {
"ASPNETCORE_URLS": "http://+:2080"
},
"publishAllPorts": true,
"useSSL": false
}
参数说明:目前只使用http协议在2080端口,因此删去https协议端口
3.发布配置文件registry.hub.docker.com_jackconer.pubxml
发布到公共镜像注册中心
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
<PropertyGroup>
<WebPublishMethod>Custom</WebPublishMethod>
<DockerPublish>true</DockerPublish>
<RegistryUrl>https://registry.hub.docker.com/username</RegistryUrl>
<UserName>username</UserName>
<PublishImageTag>latest</PublishImageTag>
<PublishProvider>ContainerRegistry</PublishProvider>
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<ProjectGuid>bd778299-6ebc-456c-99d5-888bd2bf4d88</ProjectGuid>
<_TargetId>DockerContainerRegistry</_TargetId>
</PropertyGroup>
</Project>
发布到私人注册中心
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
<PropertyGroup>
<WebPublishMethod>Custom</WebPublishMethod>
<DockerPublish>true</DockerPublish>
<RegistryUrl>http://192.168.58.135:9090/jack</RegistryUrl>
<UserName>jack</UserName>
<PublishImageTag>latest</PublishImageTag>
<PublishProvider>ContainerRegistry</PublishProvider>
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<ProjectGuid>bd778299-6ebc-456c-99d5-888bd2bf4d88</ProjectGuid>
<_TargetId>DockerContainerRegistry</_TargetId>
</PropertyGroup>
</Project>
三、发布到注册中心
1.发布到公共注册中心
安装本地Docker后登录账号密码即可发布
参考:
2.发布到私人注册中心
- 在"C:\Users{用户名}.docker"下,修改daemon.json,添加"insecure-registries"项,否则会报证书之类的错误
{
"builder": {
"gc": {
"defaultKeepStorage": "20GB",
"enabled": true
}
},
"experimental": false,
"insecure-registries": [
"192.168.58.135:9090"
]
}
发布到私人注册中心的发布文件参考第二章第3小节第二项
配置参考:
四、Linux下的Docker容器从注册中心拉取镜像并启动
Docker管理面包是Portainer
- 添加私人注册中心到Environment registries中
访问http://192.168.58.135:9090/v2/_catalog
获取已存在的镜像
{
"repositories":["jack/usedockerdemo"]
}
下图填写拉取仓库
启动容器时外部端口随意,内部端口要与监听端口匹配