Linux docker映像上的多个dotnet核心版本

时间:2019-03-06 00:58:47

标签: docker .net-core

我想知道如何在Linux上的Docker容器中运行和安装dotnet核心框架2.1和1.1的多个版本。下面是我当前的dockerfile,我也想向其中添加dotnet 1.1

FROM microsoft/dotnet:2.1.403-sdk-bionic

ENV DOTNET_SKIP_FIRST_TIME_EXPERIENCE true
ENV DOTNET_CLI_TELEMETRY_OPTOUT true

RUN apt-get update && \
    apt-get install -y zip  

2 个答案:

答案 0 :(得分:8)

您可以简单地从每个图像复制运行时。

例如,使用此 Dockerfile 来拥有 .NET 5.0 + .NET Core 3.1 + .NET Core 2.1 + ASP.NET 运行时:

# Start with .NET 5.0 runtime.
FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim

# Add .NET Core 3.1 runtime.
COPY --from=mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim /usr/share/dotnet /usr/share/dotnet

# Add .NET Core 2.1 runtime.
COPY --from=mcr.microsoft.com/dotnet/core/aspnet:2.1 /usr/share/dotnet /usr/share/dotnet

构建并运行:

$ docker build -t my-image . && docker run --entrypoint dotnet my-image --list-runtimes
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim
 ---> 5f9a6a778eac
Step 2/3 : COPY --from=mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim /usr/share/dotnet /usr/share/dotnet
 ---> Using cache
 ---> c2878d523c56
Step 3/3 : COPY --from=mcr.microsoft.com/dotnet/core/aspnet:2.1 /usr/share/dotnet /usr/share/dotnet
 ---> Using cache
 ---> 467a61c51b45
Successfully built 467a61c51b45
Successfully tagged my-image:latest

Microsoft.AspNetCore.All 2.1.23 [/usr/share/dotnet/shared/Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.23 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.10 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.1 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.23 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.10 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.1 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

答案 1 :(得分:2)

这里是多个dotnet核心框架的示例。

FROM microsoft/dotnet:2.2-runtime-deps-stretch-slim

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

RUN apt-get update

# Install .NET Core Runtime v1.1.2
ENV DOTNET_VERSION 1.1.2
ENV DOTNET_DOWNLOAD_URL https://dotnetcli.blob.core.windows.net/dotnet/release/1.1.0/Binaries/$DOTNET_VERSION/dotnet-debian-x64.$DOTNET_VERSION.tar.gz

RUN curl -SL $DOTNET_DOWNLOAD_URL --output dotnet.tar.gz \
    && mkdir -p /usr/share/dotnet \
    && tar -zxf dotnet.tar.gz -C /usr/share/dotnet \
    && rm dotnet.tar.gz \
    && ln -s /usr/share/dotnet/dotnet /usr/bin/dotnet

# Install ASP.NET Core
ENV ASPNETCORE_VERSION 2.2.1
RUN curl -SL --output aspnetcore.tar.gz https://dotnetcli.blob.core.windows.net/dotnet/aspnetcore/Runtime/$ASPNETCORE_VERSION/aspnetcore-runtime-$ASPNETCORE_VERSION-linux-x64.tar.gz \
    && aspnetcore_sha512='e027a5dada5d139a44675f28090f996375e49fbd72f7897aa925e48803632d5bf187d4f22dc8225505ac33e6a7a05dcdd8ed19d8b6d5e46b22e628315cf13e3e' \
    && echo "$aspnetcore_sha512  aspnetcore.tar.gz" | sha512sum -c - \
    && mkdir -p /usr/share/dotnet \
    && tar -zxf aspnetcore.tar.gz -C /usr/share/dotnet \
    && rm aspnetcore.tar.gz \
    && ln -sf /usr/share/dotnet/dotnet /usr/bin/dotnet

这是实例中的结果

root@cb87fda4dfc7:/# dotnet --info

Host (useful for support):
  Version: 2.2.1
  Commit:  878dd11e62

.NET Core SDKs installed:
  No SDKs were found.

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.2.1 [/usr/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.2.1 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 1.1.2 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.1 [/usr/share/dotnet/shared/Microsoft.NETCore.App]