An overview,

After using Docker to create a container, I found that many commonly used tool commands did not exist when I entered the container. For example, I wanted to use ping command to test whether the network was smooth, but found that this command could not be found in the container, as shown below:

root@kuboard-5967d77d89-h2hgn:/# ping www.baidu.com
bash: ping: command not found
Copy the code

Because the container can be simply treated as a very streamlined operating system, so we need to install the tools we need, this article simply record the process of installing some commonly used tools and encountered problems, hope to provide some help to the friends in need.

Use the following command to view the ID of the running container:

docker ps
Copy the code

Enter the Docker container with the following command:

Docker exec-it container ID bashCopy the code

Install common tools in the Docker container

Once inside the container, first take a look at which distribution of Linux the container is built on, using the following command to determine. The image output based on Debian is as follows:

root@kuboard-5967d77d89-h2hgn:/# cat /etc/issue
Debian GNU/Linux 10 \n \l
Copy the code

The image output based on Centos is as follows:

[root@bd3c8187d8c8 /]# cat /etc/issue
\S
Kernel \r on an \m
Copy the code

After entering the container, you can also directly enter apt or yum to determine

1. Debian based containers

If the container is built using Debian, you can use the apt command to install software in the container. Type apt and press Enter to view the apt help instructions, as follows:

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.Copy the code

You can update the list of available packages with the following command:

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.Copy the code

Use the following command to install the ping tool:

apt install -y inetutils-ping
Copy the code

To install the wget tool:

apt install -y wget
Copy the code

Debian APT configure tsinghua image source

If the network environment is good, the above command can be successfully installed. In general, we need to configure the domestic source to improve the speed of installing the tool package. The following describes how to configure the Debian APT tsinghua image source.

Debian’s software source configuration file is /etc/apt/sources.list, so we can see what version the container is using by looking at the original configuration file. /etc/apt/sources.list

 cat /etc/apt/sources.list
Copy the code

The following is returned:

# 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
Copy the code

The Debian version can be found asbusterAnd then go toTsinghua University open source software mirror stationFind the corresponding version, as shown in the following figure:

Use the following command to make a backup of the system’s own file:

cp /etc/apt/sources.list /etc/apt/sources.list.backup
Copy the code

To use TUNA’s software source image, replace the /etc/apt/sources.list file with the following:

Cat >> /etc/apt/sources.list << EOF # If necessary can uncomment deb https://mirrors.tuna.tsinghua.edu.cn/debian/ arcade 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 EOFCopy the code

Update the package list with the following command:

 apt update
Copy the code

If you see the following error:

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 arcade - updates the 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 arcade - 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 arcade/updates the 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.Copy the code

Because the default source is HTTP, but the intended image source is HTTPS, additional installation of the HTTP package is required

apt install apt-transport-https ca-certificates
Copy the code

Update the package list again with the following command:

apt update
Copy the code

2. Centos based container

Using Centos built containers, you can use the yum command to install software toolkits. Enter yum and press Enter to see the help instructions for using YUM. Use the following command to install the curl tool:

yum install -y curl
Copy the code

Install the Wget tool using the following command:

yum install -y wget
Copy the code

Centos Configures the Qing China image source

Look at the official document: mirrors.tuna.tsinghua.edu.cn/help/centos…

Repo for CentOS 7 and CentOS 8; repo for CentOS-Linux-*.repo for CentOS 8; repo for CentOS-Linux-*.repo for CentOS 8

cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
Copy the code

Then edit the corresponding file in /etc/yum. Repos.d/and comment out the mirrorlist= starting line with #; And the baseurl = start line uncomment (if be annotations), have replaced the bank within the domain name (e.g. mirror.centos.org) to mirrors.tuna.tsinghua.edu.cn.

The above steps can be completed in one step with the following command:

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
Copy the code

Note the * wildcard character. If you only need to replace the source in some files, please add or delete it yourself.

Note that if you want to enable some of these Repos, you need to change the enabled=0 to Enabled =1.

Finally, update the package cache:

yum makecache
Copy the code

Update software package:

yum update
Copy the code

Third, summary

The above briefly introduces how to install commonly used tools in the Docker container, mainly introduces how to configure the domestic image source, which is essentially the same as the configuration of ordinary Linux system, except that this is configured and installed in the container. After the container is deleted, the relevant installation and configuration will also be deleted. These tools are installed in the container for debugging purposes such as network problems. You can also customize a base image and install the related tools and configurations in the base image.

Reference Document Linux distribution Add Tsinghua Software source Linux distribution add Aliyun software source