向CentOS8进击——自动构建支持.net core 3.1的CentOS8容器镜像

301 阅读3分钟

我一直在使用CentOS7的.net core镜像,最近忽然发现8已经出来了,就想升级下环境,毕竟有很多人已经反应7下面有些bug,那升级系统就能解决的事,应该不是啥事吧——因为系统又不是我写的,啊哈哈,bug遇到了也找不到我。

自己写Dockerfile吧

Dockerfile的格式非常简单,几乎没啥学习成本,并且构建起来特别轻松,那么我们就从这个文件写起吧。

# 升级到8,简单,直接抄袭centos7的镜像文件
FROM centos:8

# 构建自己的环境变量,常规的环境变量先加上,避免用的时候为空
# 例如 DOTNET_RUNNING_IN_CONTAINER,就可以用来检测程序是否运行在容器内
ENV DOTNET_CORE_VERSION=3.1 \
    DOTNET_FRAMEWORK=netcoreapp3.1 \
    # Microsoft's images set this to enable detecting when an app is running in a container.
    DOTNET_RUNNING_IN_CONTAINER=true \   
    TZ=Asia/Shanghai
# 时间区域添加上海的
# CentOS 8 添加中文字体编码,设置本地编码为中文utf-8
RUN yum install -y langpacks-zh_CN
RUN export LANG=zh_CN.UTF-8
RUN echo -e "export LANG=zh_CN.UTF-8" > /etc/locale.conf
RUN echo -e "export LANG=zh_CN.UTF-8" > /etc/profile
RUN source /etc/profile
ENV LANG zh_CN.UTF-8
ENV LC_ALL zh_CN.UTF-8

# 抄袭官网的安装
# Install .NET Core
RUN dotnet_version=3.1.10 \
        && curl -SL --output dotnet.tar.gz https://dotnetcli.azureedge.net/dotnet/Runtime/$dotnet_version/dotnet-runtime-$dotnet_version-linux-x64.tar.gz \
    # https://dotnetcli.blob.core.windows.net/dotnet/Runtime/3.1.10/dotnet-runtime-3.1.10-linux-x64.tar.gz  
    # download slow in china ========
    
    && dotnet_sha512='7ff09caf2cdff0ae1c75d0ac731267ffa624025137b079134eb9fda15e1ca386af8422ddd3e93ba084853006e7d58bc04bdf232ad3762580e94658ce3dc8f984' \
    && echo "$dotnet_sha512 dotnet.tar.gz" | sha512sum -c - \
    && mkdir -p /usr/share/dotnet \
    && tar -ozxf dotnet.tar.gz -C /usr/share/dotnet \
    && rm dotnet.tar.gz \
    && ln -s /usr/share/dotnet/dotnet /usr/bin/dotnet

# 安装gdiplus 库
# install libgdiplus
# install icu 库,否则提示Couldn't find a valid ICU package installed on the system. 
#   Set the configuration flag System.Globalization.Invariant to true if you want to run with no globalization support.
RUN yum install  -y epel-release && \
    yum install -y libgdiplus-devel && \   
    yum install -y icu && \  
    yum clean all -y && \
    rm -rf /var/cache/yum/*

# 加上中国时区
# chinese timezone
RUN ln -fs /usr/share/zoneinfo/${TZ} /etc/localtime \
    && echo ${TZ} > /etc/timezone

下载慢

微软官方的下载路径太慢了,2个地址都慢。我自己下载好后,修改为本地构建

# 从本地拷贝dotnet 到容器
COPY dotnet-runtime-3.1.10-linux-x64.tar.gz dotnet.tar.gz
# Install .NET Core
RUN dotnet_version=3.1.10 \
    # 我们不下载了,太慢,太慢~~~~
    #    && curl -SL --output dotnet.tar.gz https://dotnetcli.azureedge.net/dotnet/Runtime/$dotnet_version/dotnet-runtime-$dotnet_version-linux-x64.tar.gz \
    # https://dotnetcli.blob.core.windows.net/dotnet/Runtime/3.1.10/dotnet-runtime-3.1.10-linux-x64.tar.gz
    # download slow in china ========

构建

docker build -t centos8-dotnet31 .

Sending build context to Docker daemon 30.43 MB
Step 1/13 : FROM centos:8
 ---> 0d120b6ccaa8
Step 2/13 : ENV DOTNET_CORE_VERSION 3.1 DOTNET_FRAMEWORK netcoreapp3.1 DOTNET_RUNNING_IN_CONTAINER true DOTNET_SSL_CERT_DIR /opt/app-root/ssl_dir TZ Asia/Shanghai
 ---> Using cache
 ---> 84828b8e519d
Step 3/13 : RUN yum install -y langpacks-zh_CN
 ---> Using cache
 ---> 903049fcd58c
Step 4/13 : RUN export LANG=zh_CN.UTF-8
 ---> Running in 5a59ed12a87c

 ---> d64659f24088
Removing intermediate container 5a59ed12a87c
Step 5/13 : RUN echo -e "export LANG=zh_CN.UTF-8" > /etc/locale.conf
 ---> Running in fe1ec4111a72

 ---> a1ba5fea2b3b
Removing intermediate container fe1ec4111a72
Step 6/13 : RUN echo -e "export LANG=zh_CN.UTF-8" > /etc/profile
 ---> Running in 68aecf25b603

 ---> 108ddac88adf
Removing intermediate container 68aecf25b603
Step 7/13 : RUN source /etc/profile
 ---> Running in 97f728044a65

 ---> 1ab5ceaf7b82
Removing intermediate container 97f728044a65
Step 8/13 : ENV LANG zh_CN.UTF-8
 ---> Running in d28d25a03d5b
 ---> d8ed5480d450
Removing intermediate container d28d25a03d5b
Step 9/13 : ENV LC_ALL zh_CN.UTF-8
 ---> Running in dd17f73990f9
 ---> 9dd65bcfaf72
Removing intermediate container dd17f73990f9
Step 10/13 : COPY dotnet-runtime-3.1.10-linux-x64.tar.gz dotnet.tar.gz
 ---> e74ea72beaa6
Removing intermediate container 8ecb98769565
Step 11/13 : RUN dotnet_version=3.1.10     && dotnet_sha512='7ff09caf2cdff0ae1c75d0ac731267ffa624025137b079134eb9fda15e1ca386af8422ddd3e93ba084853006e7d58bc04bdf232ad3762580e94658ce3dc8f984'     && echo "$dotnet_sha512 dotnet.tar.gz" | sha512sum -c -     && mkdir -p /usr/share/dotnet     && tar -ozxf dotnet.tar.gz -C /usr/share/dotnet     && rm dotnet.tar.gz     && ln -s /usr/share/dotnet/dotnet /usr/bin/dotnet
 ---> Running in 7e2bcc123cf4

dotnet.tar.gz: OK
 ---> 66dd2f8d5681
Removing intermediate container 7e2bcc123cf4
Step 12/13 : RUN yum install  -y epel-release &&     yum install -y libgdiplus-devel &&     yum clean all -y &&     rm -rf /var/cache/yum/*
 ---> Running in ef8d63b1208c

Last metadata expiration check: 2:49:48 ago on 2020年11月13日 星期五 19时58分37秒.
Dependencies resolved.
================================================================================
 Package               Architecture    Version            Repository       Size
================================================================================
Installing:
 epel-release          noarch          8-8.el8            extras           23 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 23 k
Installed size: 32 k
Downloading Packages:
epel-release-8-8.el8.noarch.rpm                  39 kB/s |  23 kB     00:00    
--------------------------------------------------------------------------------
Total                                            10 kB/s |  23 kB     00:02     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1 
  Installing       : epel-release-8-8.el8.noarch                            1/1 
  Running scriptlet: epel-release-8-8.el8.noarch                            1/1 
  Verifying        : epel-release-8-8.el8.noarch                            1/1 

Installed:
  epel-release-8-8.el8.noarch                                                   

Complete!
Extra Packages for Enterprise Linux Modular 8 - 9.9 kB/s |  97 kB     00:09 
39 files removed
 ---> dfe64e392df6
Removing intermediate container 1efb1e7b5ee2
Step 13/13 : RUN ln -fs /usr/share/zoneinfo/${TZ} /etc/localtime     && echo ${TZ} > /etc/timezone
 ---> Running in ffc50b140330

 ---> c263956a0d45
Removing intermediate container ffc50b140330
Successfully built c263956a0d45

检查容器

打包下来 大小: 405 MB。自我感觉良好,因为我没有看见别人构建的Dockerfile,这个足够精简,哈哈~~ 进入容器查看dotnet信息

dotnet --info
  It was not possible to find any installed .NET Core SDKs
  Did you mean to run .NET Core SDK commands? Install a .NET Core SDK from:
      https://aka.ms/dotnet-download

Host (useful for support):
  Version: 3.1.10
  Commit:  1721e39439

.NET Core SDKs installed:
  No SDKs were found.

.NET Core runtimes installed:
  Microsoft.NETCore.App 3.1.10 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download