一、概述
最近遇到这样一个问题,当使用 docker 创建一个容器后,进入到容器内部,发现很多常用的工具命令不存在,比如我想要使用 ping 命令测试一下网络的是否畅通,发现容器中找不到这个命令,如下所示:
root@kuboard-5967d77d89-h2hgn:/# ping www.baidu.com
bash: ping: command not found
因为容器可以简单的当做一个非常精简的操作系统,所以我们需要自己安装我们需要的工具,这篇文章简单记录一下安装一些常用工具的过程以及遇到的问题,希望能对有需要的小伙伴提供一些帮助。
使用下面的命令查看正在运行容器的 id:
docker ps
使用下面的命令进入到 docker 容器中:
docker exec -it 容器id bash
二、在 docker 容器中安装常用工具
进入容器之后,首先查看一下容器是基于 linux 的哪一个发行版本构建的,可以使用下面的命令进行判断。
基于 Debian 构建的镜像输出如下:
root@kuboard-5967d77d89-h2hgn:/# cat /etc/issue
Debian GNU/Linux 10 \n \l
基于 Centos 构建的镜像输出如下:
[root@bd3c8187d8c8 /]# cat /etc/issue
\S
Kernel \r on an \m
进入容器后,还可以直接输入 apt 或者 yum 进行判断
1、基于 Debian 的容器
如果容器是使用 Debian 构建的,容器中可以使用 apt
命令来安装软件,直接输入 apt 并回车,可以查看 apt 的帮助说明,如下所示:
root@kuboard-5967d77d89-h2hgn:/# apt
apt 1.4.9 (amd64)
Usage: apt [options] command
apt is a commandline package manager and provides commands for
searching and managing as well as querying information about packages.
It provides the same functionality as the specialized APT tools,
like apt-get and apt-cache, but enables options more suitable for
interactive use by default.
Most used commands:
list - list packages based on package names
search - search in package descriptions
show - show package details
install - install packages
remove - remove packages
autoremove - Remove automatically all unused packages
update - update list of available packages
upgrade - upgrade the system by installing/upgrading packages
full-upgrade - upgrade the system by removing/installing/upgrading packages
edit-sources - edit the source information file
See apt(8) for more information about the available commands.
Configuration options and syntax is detailed in apt.conf(5).
Information about how to configure sources can be found in sources.list(5).
Package and version choices can be expressed via apt_preferences(5).
Security details are available in apt-secure(8).
This APT has Super Cow Powers.
可以使用下面的命令更新可用安装包列表:
root@kuboard-5967d77d89-h2hgn:/# apt update
Hit:1 http://openresty.org/package/debian stretch InRelease
Get:2 http://security.debian.org/debian-security stretch/updates InRelease [53.0 kB]
Ign:3 http://deb.debian.org/debian stretch InRelease
Get:4 http://deb.debian.org/debian stretch-updates InRelease [93.6 kB]
Hit:5 http://deb.debian.org/debian stretch Release
Get:6 http://security.debian.org/debian-security stretch/updates/main amd64 Packages [678 kB]
Fetched 824 kB in 1s (428 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
33 packages can be upgraded. Run 'apt list --upgradable' to see them.
使用下面的命令安装 ping
工具:
apt install -y inetutils-ping
安装 wget 工具命令:
apt install -y wget
Debian apt 配置清华镜像源
如果网络环境比较好,使用上面的命令可以安装成功,通常情况下我们需要配置国内源,提高安装工具包的速度,下面将介绍 Debian apt 配置清华镜像源。
首先需要确定此容器使用的 Debian 版本是什么,Debian 的软件源配置文件是 /etc/apt/sources.list
,所以我们可以查看此文件中的原始配置,就可以知道它使用的是什么版本,查看 /etc/apt/sources.list
文件:
cat /etc/apt/sources.list
返回内容如下:
# deb http://snapshot.debian.org/archive/debian/20210408T000000Z buster main
deb http://deb.debian.org/debian buster main
# deb http://snapshot.debian.org/archive/debian-security/20210408T000000Z buster/updates main
deb http://security.debian.org/debian-security buster/updates main
# deb http://snapshot.debian.org/archive/debian/20210408T000000Z buster-updates main
deb http://deb.debian.org/debian buster-updates main
可以发现 Debian 版本为 buster
,然后去到清华大学开源软件镜像站找到对应版本,如下图示所示:
使用下面的命令将系统自带的该文件做个备份:
cp /etc/apt/sources.list /etc/apt/sources.list.backup
使用下面的命令将 /etc/apt/sources.list
文件替换为下面内容,即可使用 TUNA 的软件源镜像:
cat >> /etc/apt/sources.list << EOF
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security buster/updates main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security buster/updates main contrib non-free
EOF
使用下面的命令更新安装包列表:
apt update
如果看到报出如下错误:
Ign:1 https://mirrors.tuna.tsinghua.edu.cn/debian buster InRelease
Ign:2 https://mirrors.tuna.tsinghua.edu.cn/debian buster-updates InRelease
Ign:3 https://mirrors.tuna.tsinghua.edu.cn/debian buster-backports InRelease
Ign:4 https://mirrors.tuna.tsinghua.edu.cn/debian-security buster/updates InRelease
Hit:5 http://repo.mysql.com/apt/debian buster InRelease
Err:6 https://mirrors.tuna.tsinghua.edu.cn/debian buster Release
Certificate verification failed: The certificate is NOT trusted. The certificate issuer is unknown. Could not handshake: Error in the certificate verification. [IP: 101.6.8.193 443]
Err:7 https://mirrors.tuna.tsinghua.edu.cn/debian buster-updates Release
Certificate verification failed: The certificate is NOT trusted. The certificate issuer is unknown. Could not handshake: Error in the certificate verification. [IP: 101.6.8.193 443]
Err:8 https://mirrors.tuna.tsinghua.edu.cn/debian buster-backports Release
Certificate verification failed: The certificate is NOT trusted. The certificate issuer is unknown. Could not handshake: Error in the certificate verification. [IP: 101.6.8.193 443]
Err:9 https://mirrors.tuna.tsinghua.edu.cn/debian-security buster/updates Release
Certificate verification failed: The certificate is NOT trusted. The certificate issuer is unknown. Could not handshake: Error in the certificate verification. [IP: 101.6.8.193 443]
Reading package lists... Done
W: https://mirrors.tuna.tsinghua.edu.cn/debian/dists/buster/InRelease: No system certificates available. Try installing ca-certificates.
W: https://mirrors.tuna.tsinghua.edu.cn/debian/dists/buster-updates/InRelease: No system certificates available. Try installing ca-certificates.
W: https://mirrors.tuna.tsinghua.edu.cn/debian/dists/buster-backports/InRelease: No system certificates available. Try installing ca-certificates.
W: https://mirrors.tuna.tsinghua.edu.cn/debian-security/dists/buster/updates/InRelease: No system certificates available. Try installing ca-certificates.
W: https://mirrors.tuna.tsinghua.edu.cn/debian/dists/buster/Release: No system certificates available. Try installing ca-certificates.
E: The repository 'https://mirrors.tuna.tsinghua.edu.cn/debian buster Release' does not have a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
是因为默认的源是 http 的,但是准备使用的镜像源是 https 的,所以需要额外的安装有关 http 的包
apt install apt-transport-https ca-certificates
使用下面的命令再次更新安装包列表:
apt update
2、基于 Centos 的容器
使用 Centos 构建的容器,可以使用 yum 命令安装软件工具包,直接输入 yum 并回车可以看到 yum 的使用帮助说明。
使用下面的命令安装 curl 工具:
yum install -y curl
使用下面的命令安装 wget 工具:
yum install -y wget
Centos 配置清华镜像源
查看官方文档:https://mirrors.tuna.tsinghua.edu.cn/help/centos/
首先使用下面的命令备份 /etc/yum.repos.d/
内的文件(CentOS 7 及之前为 CentOS-Base.repo,CentOS 8 为CentOS-Linux-*.repo)
cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
然后编辑 /etc/yum.repos.d/
中的相应文件,在 mirrorlist=
开头行前面加 # 注释掉;并将 baseurl=
开头行取消注释(如果被注释的话),把该行内的域名(例如mirror.centos.org)替换为 mirrors.tuna.tsinghua.edu.cn
。
以上步骤可以被下方的命令一步完成:
sudo sed -e 's|^mirrorlist=|#mirrorlist=|g' \
-e 's|^#baseurl=http://mirror.centos.org|baseurl=https://mirrors.tuna.tsinghua.edu.cn|g' \
-i.bak \
/etc/yum.repos.d/CentOS-*.repo
注意其中的*通配符,如果只需要替换一些文件中的源,请自行增删。
注意,如果需要启用其中一些 repo,需要将其中的 enabled=0 改为 enabled=1。
最后,更新软件包缓存:
yum makecache
更新软件包:
yum update
三、总结
上面简单介绍了如何在 docker 容器中安装常用的工具,主要介绍如何配置国内镜像源,本质上跟普通的 Linux 系统的配置是一样的,只是这个是在容器中配置并安装,容器删除以后,相关的安装配置也会一并删除。在容器中安装这些工具主要是为了方便调试,比如一些网络的问题,也可以自定义一个基础镜像,把相关的工具和配置安装在基础镜像中。
参考文档
Linux 发行版添加清华软件源
Linux 发行版添加阿里云软件源