ps:刚刚学习Docker和K8s,记录自己遇到的坑以供分享

环境

  • Centos7.7
  • Docker 19.03.5
  • dotnetCore 3.0

开始

创建工程

新建一个asp.netAPI项目,启用Docker(主要是想少些一点Dockerfile)
在这里插入图片描述
在这里插入图片描述

修改appsetting.json文件

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  //这里配置服务端口
  "Kestrel": {
    "EndPoints": {
      "Http": {
        "Url": "http://0.0.0.0:5000" //端口
      }
    }
  },
  "AllowedHosts": "*"
}

修改Dockerfile

注释内容为生成的内容,上面的我修改之后的内容,主要就是修改几个目录
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base 
WORKDIR /app
EXPOSE 80
EXPOSE 5000
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
WORKDIR /src
COPY ["./MyHelloWorld.csproj", "./"]
RUN dotnet restore "./MyHelloWorld.csproj"
COPY . .
WORKDIR /src
RUN dotnet build "MyHelloWorld.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "MyHelloWorld.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyHelloWorld.dll"]

##See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
#
#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 ["MyHelloWorld/MyHelloWorld.csproj", "MyHelloWorld/"]
#RUN dotnet restore "MyHelloWorld/MyHelloWorld.csproj"
#COPY . .
#WORKDIR "/src/MyHelloWorld"
#RUN dotnet build "MyHelloWorld.csproj" -c Release -o /app/build
#
#FROM build AS publish
#RUN dotnet publish "MyHelloWorld.csproj" -c Release -o /app/publish
#
#FROM base AS final
#WORKDIR /app
#COPY --from=publish /app/publish .
#ENTRYPOINT ["dotnet", "MyHelloWorld.dll"]

上传源码

这里需要说一下,我采用的是上传源码到linxu,然后在linux中编译生成docker镜像,
当然也可以在本地编译后到linxu中生成镜像或者是直接在windows上生成镜像上传到仓库后在linux上获取运行。

上传后运行命令生成镜像
docker build -t myhelloworld .
注意:1命令最后一个 . 2名称必须全小写
出现如下命令表示成功

[root@ly aspnet]# sudo docker build -t myhelloworld .
Sending build context to Docker daemon  24.06kB
Step 1/18 : FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base
 ---> 56b2d9c3874c
Step 2/18 : WORKDIR /app
 ---> Using cache
 ---> 6bac62daaefa
Step 3/18 : EXPOSE 80
 ---> Using cache
 ---> 095721768930
Step 4/18 : EXPOSE 5003
 ---> Running in 8bc96d6a2a46
Removing intermediate container 8bc96d6a2a46
 ---> 55b29d07026d
Step 5/18 : EXPOSE 443
 ---> Running in a7182238404a
Removing intermediate container a7182238404a
 ---> b77265a9f8cd
Step 6/18 : FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
 ---> ca45dd244135
Step 7/18 : WORKDIR /src
 ---> Using cache
 ---> 757d62c50914
Step 8/18 : COPY ["./MyHelloWorld.csproj", "./"]
 ---> Using cache
 ---> 60535b1636ab
Step 9/18 : RUN dotnet restore "./MyHelloWorld.csproj"
 ---> Using cache
 ---> 3052da84ab90
Step 10/18 : COPY . .
 ---> 670a7280dca2
Step 11/18 : WORKDIR /src
 ---> Running in 8a52710d72ad
Removing intermediate container 8a52710d72ad
 ---> 48c338d48ab0
Step 12/18 : RUN dotnet build "MyHelloWorld.csproj" -c Release -o /app/build
 ---> Running in 7752db8f1777
Microsoft (R) Build Engine version 16.3.2+e481bbf88 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restore completed in 25.57 ms for /src/MyHelloWorld.csproj.
  MyHelloWorld -> /app/build/MyHelloWorld.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:04.38
Removing intermediate container 7752db8f1777
 ---> 4610cc6750f2
Step 13/18 : FROM build AS publish
 ---> 4610cc6750f2
Step 14/18 : RUN dotnet publish "MyHelloWorld.csproj" -c Release -o /app/publish
 ---> Running in 29be737f5792
Microsoft (R) Build Engine version 16.3.2+e481bbf88 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restore completed in 38.27 ms for /src/MyHelloWorld.csproj.
  MyHelloWorld -> /src/bin/Release/netcoreapp3.0/MyHelloWorld.dll
  MyHelloWorld -> /app/publish/
Removing intermediate container 29be737f5792
 ---> 872704084867
Step 15/18 : FROM base AS final
 ---> b77265a9f8cd
Step 16/18 : WORKDIR /app
 ---> Running in 2315caf7cbb8
Removing intermediate container 2315caf7cbb8
 ---> 7d888b5868ee
Step 17/18 : COPY --from=publish /app/publish .
 ---> a607be58dc10
Step 18/18 : ENTRYPOINT ["dotnet", "MyHelloWorld.dll"]
 ---> Running in 41f5ea65e2c4
Removing intermediate container 41f5ea65e2c4
 ---> 18eaacf21e9a
Successfully built 18eaacf21e9a
Successfully tagged myhelloworld:latest

  • dockerfile文件目录不正确会出现各种找不到文件的错误,注意修改路径
  • docker版本坑,一定要用17.0 以上的版本

运行容器

sudo docker run --name myhelloworld -p 80:5000 myhelloworld:latest
80:外部端口
5000 内部端口

效果

在这里插入图片描述
我发布到自己的阿里云服务器了,所以有外网地址,如果没用就用内网地址测试。

Docker安装引导地址
网友力量

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐