CentOS 8 reports an error when executing the yum command: Failed to set locale, defaulting to C.UTF-8

Today, Docker created a new CentOS image. When running a container based on this image and executing the yum command, I encountered the following error:

[root@GC Administrator]# yum install -y yum-utils
Failed to set locale, defaulting to C.UTF-8
CentOS Linux 8 - AppStream 41 B/s | 38 B 00:00
Error: Failed to download metadata for repo 'appstream': Cannot prepare internal mirrorlist: No URLs in mirrorlist

Currently, docker pull centos is used without specifying the tag. The latest version pulled is 8.4.2105.

[root@GC Administrator]# cat /etc/redhat-release
CentOS Linux release 8.4.2105

1. Yum mirror source problem

Among them, Error: Failed to download metadata for repo 'appstream': Cannot prepare internal mirrorlist: No URLs in mirrorlist is a problem with the yum mirror source. I have summarized and solved this problem in a previous article. plan.
CentOS reports an error when executing the yum command [Error: Failed to download metadata for warehouse appstream: …]

You can refer to the above article to solve the problem, or you can directly run the following command to solve the problem (one of the solutions mentioned in the above article)

# Centos version before 8
# Take CentOS 7.9.2009 as an example
minorver=7.9.2009
sudo sed -e "s|^mirrorlist=|#mirrorlist=|g" \
         -e "s|^#baseurl=http://mirror.centos.org/centos/\$releasever|baseurl=https://mirrors.aliyun.com/centos-vault/$minorver|g" \
         -i.bak \
         /etc/yum.repos.d/CentOS-*.repo

# CentOS 8 version
# Take CentOS 8.5.2111 as an example
minorver=8.5.2111
sudo sed -e "s|^mirrorlist=|#mirrorlist=|g" \
         -e "s|^#baseurl=http://mirror.centos.org/\$contentdir/\$releasever|baseurl=https://mirrors.aliyun.com/centos-vault/$minorver|g" \
         -i.bak \
         /etc/yum.repos.d/CentOS-*.repo

2. Language environment issues

Among them, Failed to set locale, defaulting to C.UTF-8 is a system language environment problem.
Here is the solution:

1) View locally installed language packs

[root@GC ~]# locale -a
C
C.utf8
POSIX

2) Install language pack

Install Chinese language pack: yum install glibc-langpack-zh
Install the English language pack: yum install glibc-langpack-en

I only installed the Chinese language pack here

[root@GC Administrator]# locale -a
C
C.utf8
POSIX
zh_CN
zh_CN.gb18030
zh_CN.gbk
zh_CN.utf8
zh_HK
zh_HK.utf8
zh_SG
zh_SG.gbk
zh_SG.utf8
zh_TW
zh_TW.euctw
zh_TW.utf8

3) Set the locale

echo "export LC_ALL=zh_CN.utf8" >> /etc/profile
source /etc/profile

From the languages you have installed, select the language you want to set. I chose zh_CN.utf8 here.

4) Verify whether the language is set successfully

[root@GC Administrator]# yum install -y yum-utils
Last metadata expiration check: 0:17:50 ago on Friday, August 25, 2023 14:06:57.
Dependencies resolved.
================================================== ================================================== ====================
 Package Architecture Version Repository Size
================================================== ================================================== ====================
Installing:
 yum-utils noarch 4.0.18-4.el8 baseos 71k
Installing dependencies:
 dbus-glib x86_64 0.110-2.el8 baseos 127k
 dnf-plugins-core noarch 4.0.18-4.el8 baseos 69k
 python3-dateutil noarch 1:2.6.1-6.el8 baseos 251k
 python3-dbus x86_64 1.2.4-15.el8 baseos 134k
 python3-dnf-plugins-core noarch 4.0.18-4.el8 baseos 234k
 python3-six noarch 1.11.0-8.el8 baseos 38k

Transaction Summary
================================================== ================================================== ====================
Install 7 Packages

Total download size: 925k
Installed size: 2.3M
Downloading Packages:
(1/7): dnf-plugins-core-4.0.18-4.el8.noarch.rpm 145 kB/s | 69 kB 00:00
(2/7): dbus-glib-0.110-2.el8.x86_64.rpm 256 kB/s | 127 kB 00:00
(3/7): python3-dateutil-2.6.1-6.el8.noarch.rpm 363 kB/s | 251 kB 00:00
(4/7): python3-dbus-1.2.4-15.el8.x86_64.rpm 404 kB/s | 134 kB 00:00
(5/7): python3-six-1.11.0-8.el8.noarch.rpm 225 kB/s | 38 kB 00:00
(6/7): python3-dnf-plugins-core-4.0.18-4.el8.noarch.rpm 536 kB/s | 234 kB 00:00
(7/7): yum-utils-4.0.18-4.el8.noarch.rpm 299 kB/s | 71 kB 00:00
-------------------------------------------------- -------------------------------------------------- --------------------
Total 879 kB/s | 925 kB 00:01
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing: 1/1
  Installing: python3-six-1.11.0-8.el8.noarch 1/7
  Installing: python3-dateutil-1:2.6.1-6.el8.noarch 2/7
  Installing: dbus-glib-0.110-2.el8.x86_64 3/7
  Running scriptlet: dbus-glib-0.110-2.el8.x86_64 3/7
  Installing: python3-dbus-1.2.4-15.el8.x86_64 4/7
  Installing: python3-dnf-plugins-core-4.0.18-4.el8.noarch 5/7
  Installing: dnf-plugins-core-4.0.18-4.el8.noarch 6/7
  Installing: yum-utils-4.0.18-4.el8.noarch 7/7
  Running scriptlet: yum-utils-4.0.18-4.el8.noarch 7/7
  Verifying: dbus-glib-0.110-2.el8.x86_64 1/7
  Verifying: dnf-plugins-core-4.0.18-4.el8.noarch 2/7
  Verifying: python3-dateutil-1:2.6.1-6.el8.noarch 3/7
  Verifying: python3-dbus-1.2.4-15.el8.x86_64 4/7
  Verifying: python3-dnf-plugins-core-4.0.18-4.el8.noarch 5/7
  Verifying: python3-six-1.11.0-8.el8.noarch 6/7
  Verifying: yum-utils-4.0.18-4.el8.noarch 7/7

Installed:
  dbus-glib-0.110-2.el8.x86_64 dnf-plugins-core-4.0.18-4.el8.noarch python3-dateutil-1:2.6.1-6.el8.noarch
  python3-dbus-1.2.4-15.el8.x86_64 python3-dnf-plugins-core-4.0.18-4.el8.noarch python3-six-1.11.0-8.el8.noarch
  yum-utils-4.0.18-4.el8.noarch

Complete!

OK, done~