How to install some common tools in docker container

Posted by djp120 on Fri, 18 Feb 2022 11:07:51 +0100

1, Overview

After using docker to create a container, I enter the container and find that many common tool commands do not exist. For example, I want to use ping command to test whether the network is unblocked. I find that this command cannot be found in the container, as shown below:

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

Because the container can be simply used as a very compact operating system, we need to install the tools we need. This article briefly records the process of installing some common tools and the problems encountered, hoping to provide some help to the little partners in need.

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

docker ps

Enter the docker container with the following command:

docker exec -it container id bash

2, Install common tools in docker container

After entering the container, first check which distribution version of linux the container is based on. You can use the following command to judge.
The image output built based on Debian is as follows:

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

The image output built based on Centos is as follows:

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

After entering the container, you can also directly enter apt or yum for judgment

1. Debian based container

If the container is built using Debian, you can use apt command to install software in the container. Directly enter apt and press enter to view the help instructions of apt, as shown below:

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.

You can update the list of available installation packages using 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.

Use the following command to install the ping tool:

apt install -y inetutils-ping

Install wget tool command:

apt install -y wget

Debian apt configuring Tsinghua image source

If the network environment is good, the installation can be successful by using the above command. Generally, we need to configure the domestic source to improve the speed of the installation kit. The Debian apt configuration of Tsinghua image source will be introduced below.

First, you need to determine the Debian version used by this container. The Debian software source configuration file is / etc / apt / sources List, so we can check the original configuration in this file to know what version it uses. Check / etc / apt / sources List file:

 cat /etc/apt/sources.list

The returned contents are as follows:

# 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

You can find that the Debian version is buster, and then go to Tsinghua University open source software mirror station Find the corresponding version, as shown in the figure below:

Use the following command to make a backup of the file that comes with the system:

cp /etc/apt/sources.list /etc/apt/sources.list.backup

Use the following command to set / etc / apt / sources The software source image of TUNA can be used by replacing the list file with the following:

cat >> /etc/apt/sources.list << EOF
# The source image is annotated by default to improve apt update speed. You can cancel the annotation if necessary
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

Update the installation package list with the following command:

 apt update

If the following errors are reported:

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.

Because the default source is HTTP, but the image source to be used is https, you need to install additional packages related to http

apt install apt-transport-https ca-certificates

Update the installation package list again with the following command:

apt update

2. Centos based container

Using the container built by Centos, you can use the yum command to install the software kit. Directly enter Yum and press enter to see the help instructions for yum.
Install the curl tool using the following command:

yum install -y curl

Use the following command to install the wget tool:

yum install -y wget

Centos configuring image source

View official documents: https://mirrors.tuna.tsinghua.edu.cn/help/centos/

First, use the following command to back up / etc / yum repos. D / (CentOS-Base.repo for CentOS 7 and before, CentOS Linux - *. Repo for CentOS 8)

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

Then edit / etc / yum.com repos. D /, add # comments before the beginning line of mirrorlist =; Uncomment the line beginning with baseurl = (if annotated), and replace the domain name in this line (such as mirror.centos.org) with mirrors tuna. tsinghua. edu. cn.

The above steps can be completed in one step by 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

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

Note that if you need to enable some of these repo s, you need to change enabled=0 to enabled=1.

Finally, update the package cache:

yum makecache

Update package:

yum update

3, Summary

The above briefly introduces how to install common tools in the docker container, and mainly introduces how to configure the domestic image source. In essence, it is the same as the configuration of ordinary Linux systems, but this is configured and installed in the container. After the container is deleted, the relevant installation configuration will also be deleted. Installing these tools in the container is mainly to facilitate debugging. For example, for some network problems, you can also customize a basic image and install relevant tools and configurations in the basic image.

Reference documents
Add Tsinghua software source to Linux distribution
Add alicloud software source for Linux distribution

Topics: Docker