乐趣区

关于docker:如何在docker容器中安装一些常用工具

一、概述

当应用 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…

首先应用上面的命令备份 /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 发行版增加阿里云软件源

退出移动版