dotnet7容器的生成以及api服务的部署

文章目录

  • 前言
  • 一、本地生成dotnet7镜像
    • 1. 下载linux环境下的dotnet7运行环境安装包
    • 2. 使用Dockerfile生成dotnet7镜像
  • 二、部署API服务


前言

记录docker搭建dotnet7环境,以及api服务的部署。


一、本地生成dotnet7镜像

1. 下载linux环境下的dotnet7运行环境安装包

下载dotnet7安装包

2. 使用Dockerfile生成dotnet7镜像

  1. 创建文件夹D:\Docker\dotnet7
  2. 将第一步下载的安装包dotnet-sdk-7.0.410-linux-x64.tar拷贝到文件夹中
  3. 创建Dockerfile文件,Dockerfile不需要扩展名,可右键重命名删除掉.txt后缀,复制下面的内容到Dockerfile文件夹中
ARG REPO=mcr.microsoft.com/dotnet/aspnet
FROM $REPO:7.0.20-jammy-amd64

COPY dotnet-sdk-7.0.410-linux-x64.tar.gz	dotnet.tar.gz 

ENV \
    # Unset ASPNETCORE_URLS from aspnet base image
    ASPNETCORE_URLS= \
    # Do not generate certificate
    DOTNET_GENERATE_ASPNET_CERTIFICATE=false \
    # Do not show first run text
    DOTNET_NOLOGO=true \
    # SDK version
    DOTNET_SDK_VERSION=7.0.410 \
    # Enable correct mode for dotnet watch (only mode supported in a container)
    DOTNET_USE_POLLING_FILE_WATCHER=true \
    # Skip extraction of XML docs - generally not useful within an image/container - helps performance
    NUGET_XMLDOC_MODE=skip \
    # PowerShell telemetry for docker image usage
    POWERSHELL_DISTRIBUTION_CHANNEL=PSDocker-DotnetSDK-Ubuntu-22.04

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        curl \
        git \
        wget \
    && rm -rf /var/lib/apt/lists/*

# Install .NET SDK
RUN mkdir -p /usr/share/dotnet \
    && tar -oxzf dotnet.tar.gz -C /usr/share/dotnet ./packs ./sdk ./sdk-manifests ./templates ./LICENSE.txt ./ThirdPartyNotices.txt \
    && rm dotnet.tar.gz \
    # Trigger first run experience by running arbitrary cmd
    && dotnet help

# Install PowerShell global tool
RUN powershell_version=7.3.12 \
    && curl -fSL --output PowerShell.Linux.x64.$powershell_version.nupkg https://powershellinfraartifacts-gkhedzdeaghdezhr.z01.azurefd.net/tool/$powershell_version/PowerShell.Linux.x64.$powershell_version.nupkg \
    && powershell_sha512='7a0fee63dd1ac0f7c2825ebcef38a9a244f820c4b4eb2c48923be50083370f89df966251049470fba17cd785ef71f651449b0c2a3b91b141a88d6c40358858d7' \
    && echo "$powershell_sha512  PowerShell.Linux.x64.$powershell_version.nupkg" | sha512sum -c - \
    && mkdir -p /usr/share/powershell \
    && dotnet tool install --add-source / --tool-path /usr/share/powershell --version $powershell_version PowerShell.Linux.x64 \
    && dotnet nuget locals all --clear \
    && rm PowerShell.Linux.x64.$powershell_version.nupkg \
    && ln -s /usr/share/powershell/pwsh /usr/bin/pwsh \
    && chmod 755 /usr/share/powershell/pwsh \
    # To reduce image size, remove the copy nupkg that nuget keeps.
    && find /usr/share/powershell -print | grep -i '.*[.]nupkg$' | xargs rm
  1. D:\Docker\dotnet7文件夹下打开cmd或者powershell,执行命令docker build -t dotnet7 . 生成dotnet7镜像,可通过docker images确认dotnet7镜像是否成功生成

二、部署API服务

  1. 创建文件夹D:\Docker\api,在文件夹中新建publish文件,并将服务文件拷贝到publish文件夹下
  2. 创建Dockerfile文件,将服务文件拷贝进dotnet7镜像,生成新的镜像文件,Dockerfile内容如下
FROM dotnet7:latest

RUN chmod 755 /usr/local

COPY ./publish /usr/local/api

RUN chmod 755 /usr/local/api

EXPOSE 80

WORKDIR /usr/local/api

ENTRYPOINT ["dotnet","/usr/local/api/XXXWebApi.dll"]

** 注意将服务的appsettings.json文件中的urls设置改为http://0.0.0.0:80,不能使用localhost或者127.0.0.1否则本机将无法通过80端口访问到服务

  1. D:\Docker\api下打开cmd或者powershell执行下面的命令创建以及启动镜像,其中api是镜像名称,启动后就可以在本机使用http://localhost:18888/访问服务了
docker build -t api .
docker run --name api -p 18888:80 api:latest
  • 在 Linux 系统中,可以使用特殊的 DNS 名称 host.docker.internal 来指代宿主机,比如要访问本机的服务,可以直接将ip替换成 host.docker.internal

你可能感兴趣的:(docker,docker)