如何在docker容器中安裝一些經常使用工具

1、概述

當使用 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

2、在 docker 容器中安裝經常使用工具

進入容器以後,首先查看一下容器是基於 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 的容器

若是容器是使用 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 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 ,而後去到清華大學開源軟件鏡像站找到對應版本,以下圖示所示:
image.png

使用下面的命令將系統自帶的該文件作個備份:

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 的容器

使用 Centos 構建的容器,可使用 yum 命令安裝軟件工具包,直接輸入 yum 並回車能夠看到 yum 的使用幫助說明。
使用下面的命令安裝 curl 工具:

yum install -y curl

使用下面的命令安裝 wget 工具:

yum install -y wget

Centos 配置清華鏡像源

查看官方文檔: 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

3、總結

上面簡單介紹瞭如何在 docker 容器中安裝經常使用的工具,主要介紹如何配置國內鏡像源,本質上跟普通的 Linux 系統的配置是同樣的,只是這個是在容器中配置並安裝,容器刪除之後,相關的安裝配置也會一併刪除。在容器中安裝這些工具主要是爲了方便調試,好比一些網絡的問題,也能夠自定義一個基礎鏡像,把相關的工具和配置安裝在基礎鏡像中。

參考文檔
Linux 發行版添加清華軟件源
Linux 發行版添加阿里雲軟件源

相關文章
相關標籤/搜索