当使用 docker 建立一个容器后,进入到容器内部,发现不少经常使用的工具命令不存在,好比我想要使用 ping 命令测试一下网络的是否畅通,发现容器中找不到这个命令,以下所示:mysql
root@kuboard-5967d77d89-h2hgn:/# ping www.baidu.com bash: ping: command not found
由于容器能够简单的当作一个很是精简的操做系统,因此咱们须要本身安装咱们须要的工具,这篇文章简单记录一下安装一些经常使用工具的过程以及遇到的问题,但愿能对有须要的小伙伴提供一些帮助。linux
使用下面的命令查看正在运行容器的 id:sql
docker ps
使用下面的命令进入到 docker 容器中:docker
docker exec -it 容器id bash
进入容器以后,首先查看一下容器是基于 linux 的哪个发行版本构建的,可使用下面的命令进行判断。
基于 Debian 构建的镜像输出以下:express
root@kuboard-5967d77d89-h2hgn:/# cat /etc/issue Debian GNU/Linux 10 \n \l
基于 Centos 构建的镜像输出以下:centos
[root@bd3c8187d8c8 /]# cat /etc/issue \S Kernel \r on an \m
进入容器后,还能够直接输入 apt 或者 yum 进行判断
若是容器是使用 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.
可使用下面的命令更新可用安装包列表:bash
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 工具命令:curl
apt install -y wget
若是网络环境比较好,使用上面的命令能够安装成功,一般状况下咱们须要配置国内源,提升安装工具包的速度,下面将介绍 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
使用 Centos 构建的容器,可使用 yum 命令安装软件工具包,直接输入 yum 并回车能够看到 yum 的使用帮助说明。
使用下面的命令安装 curl 工具:
yum install -y curl
使用下面的命令安装 wget 工具:
yum install -y wget
查看官方文档: https://mirrors.tuna.tsinghua...
首先使用下面的命令备份 /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 系统的配置是同样的,只是这个是在容器中配置并安装,容器删除之后,相关的安装配置也会一并删除。在容器中安装这些工具主要是为了方便调试,好比一些网络的问题,也能够自定义一个基础镜像,把相关的工具和配置安装在基础镜像中。