📚Dockerfile
bash
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY ["MyMvcApp.csproj", "./"]
RUN dotnet restore "MyMvcApp.csproj"
COPY . .
WORKDIR "/src/"
RUN dotnet build "MyMvcApp.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MyMvcApp.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyMvcApp.dll"]
✏️ Docker 镜像
bash
docker build -t mymvcapp:latest .
📝在 Docker Hub 上创建仓库
登录 Docker Hub (https://hub.docker.com)。
点击 "Create Repository"。
输入仓库名称,例如 mymvcapp,然后点击 "Create"。
💻将 Docker 镜像推送到 Docker Hub
首先,登录 Docker Hub:
bash
docker login
📅标记镜像
bash
docker tag mymvcapp:latest <your-dockerhub-username>/mymvcapp:latest
📊推送镜像到 Docker Hub
bash
docker push <your-dockerhub-username>/mymvcapp:latest
= Docker Hub 用户名
bash
docker run -d -p 80:80 <your-dockerhub-username>/mymvcapp:latest
🎓完整流程回顾
bash
#创建 .NET 8 MVC 应用程序。
#在项目根目录下创建 Dockerfile。
#构建 Docker 镜像:
docker build -t mymvcapp:latest .
#在 Docker Hub 上创建仓库。
#登录 Docker Hub:
docker login
#标记并推送 Docker 镜像到 Docker Hub:
docker tag mymvcapp:latest <your-dockerhub-username>/mymvcapp:latest
docker push <your-dockerhub-username>/mymvcapp:latest
#在本地或服务器上运行 Docker 镜像:
docker run -d -p 80:80 <your-dockerhub-username>/mymvcapp:latest
#通过这些步骤,将 .NET 8 MVC 部署到 Docker Hub 并运行它~~
err denied requested access to the resource is denied
bash
docker logout
docker login
err error response from deamon:get "https://registry-1.docket.io/v2/":unauthorized:incorrect username or password
用户密码错误
https://docs.docker.com/go/access-tokens
https://docs.docker.com/security/for-developers/access-tokens/#create-an-access-token
bash
docker login -u qxxx
dckr_pat_o0ZpPxxx
docker tag mymvcapp:latest xx/mymvcapp:dev
docker push <your-dockerhub-username>/mymvcapp:dev
推送成功后可以在hub仓库查看
运行
bash
docker run -d -p 18081:8080 -p 18082:8081 --name mywebmvcapp mymvcapp:latest
💼通过VS打包发布
项目右键-发布
用户名密码
执行发布
推送成功后可 docker run username/mymvcapp:latest 运行
END