一、前言
本文记录Docker使用过程中遇见的问题,供后续回顾参考。
关联资源:网络Docker博客、官方FAQ、文档、Docker 从入门到实践、中文社区、riptutorial
二、问题及处理记录
2.1、docker容器内没有vi,nano等编辑器
1)如果宿主机本地有,可映射到docker容器内,但vim会有共享库限制;
docker run -v /usr/bin/vi:/usr/bin/vi --name mysql_soft image_name
2)docker容器内执行apt update报错:
Get:1 http://deb.debian.org/debian bookworm InRelease [151 kB]
Get:2 http://deb.debian.org/debian bookworm-updates InRelease [55.4 kB]
Get:3 http://deb.debian.org/debian-security bookworm-security InRelease [48.0 kB]
Get:4 http://deb.debian.org/debian bookworm/main amd64 Packages [8786 kB]
Get:5 http://deb.debian.org/debian bookworm-updates/main amd64 Packages [13.8 kB]
Get:6 http://deb.debian.org/debian-security bookworm-security/main amd64 Packages [155 kB]
Fetched 9209 kB in 2s (5838 kB/s)
Reading package lists…
E: Problem executing scripts APT::Update::Post-Invoke ‘rm -f /var/cache/apt/archives/.deb /var/cache/apt/archives/partial/.deb /var/cache/apt/*.bin || true’
E: Sub-process returned an error code
分析处理过程:
//docker网络检查
docker run redis apt update #宿主机执行更新检查,报错同上,ping deb.debian.org通,网络正常
//相关经验表明:Docker version 20.10.9以及以下版本使用apt有问题,要么升级,要么执行如下临时修改,
sed -i -e 's/^APT/# APT/' -e 's/^DPkg/# DPkg/' /etc/apt/apt.conf.d/docker-clean
#现场docker版本
docker --version
Docker version 20.10.7, build f0df350
#查看可从软件源安装的软件包的版本列表
apt-cache madison <package_name>
#容器OS版本
cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
#修改docker-clean文件后再次执行:
apt update #输出如下
Hit:1 http://deb.debian.org/debian bookworm InRelease
Hit:2 http://deb.debian.org/debian bookworm-updates InRelease
Hit:3 http://deb.debian.org/debian-security bookworm-security InRelease
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
2 packages can be upgraded. Run 'apt list --upgradable' to see them.
#安装再次报错:apt-get install vim -y
dpkg-deb --control subprocess returned error exit status 2
dpkg-deb (subprocess): decompressing archive '/tmp/apt-dpkg-install-UoSzsi/3-vim-runtime_2%3a9.0.1378-2_all.deb' (size=7025180) member 'control.tar': lzma error: Cannot allocate memory
tar: This does not look like a tar archive
tar: Exiting with failure status due to previous errors
dpkg-deb: error: tar subprocess returned error exit status 2
dpkg: error processing archive /tmp/apt-dpkg-install-UoSzsi/3-vim-runtime_2%3a9.0.1378-2_all.deb (--unpack):
dpkg-deb --control subprocess returned error exit status 2
dpkg-deb (subprocess): decompressing archive '/tmp/apt-dpkg-install-UoSzsi/4-vim_2%3a9.0.1378-2_amd64.deb' (size=1567304) member 'control.tar': lzma error: Cannot allocate memory
tar: This does not look like a tar archive
tar: Exiting with failure status due to previous errors
dpkg-deb: error: tar subprocess returned error exit status 2
dpkg: error processing archive /tmp/apt-dpkg-install-UoSzsi/4-vim_2%3a9.0.1378-2_amd64.deb (--unpack):
dpkg-deb --control subprocess returned error exit status 2
dpkg-deb (subprocess): decompressing archive '/tmp/apt-dpkg-install-UoSzsi/5-xxd_2%3a9.0.1378-2_amd64.deb' (size=83680) member 'control.tar': lzma error: Cannot allocate memory
#容器因与宿主机共用内核,所以需修改内核参数
vim /etc/sysctl.conf #新增
fs.file-max = 1500000
fs.nr_open = 1500000
net.core.somaxconn = 2048
vm.overcommit_memory = 1
或:echo 1 > /proc/sys/vm/overcommit_memory
#为容器分配内存和交换空间,--memory-swap 参数用于设置容器可使用的总交换空间大小(包括内存和交换分区)。这个值必须大于或等于指定的 --memory 参数值
docker inspect --format='{{.HostConfig.MemoryReservation}}, {{.HostConfig.MemorySwap}}'
docker update redis -m 512m --memory-swap -1 #其中,容器能够使用无限的交换空间,可以设置 --memory-swap 为 -1
#要想让所有容器这2个值一样,直接修改宿主机的
vim /etc/docker/daemon.json
{
"default-memory-swap": "2g",
"default-memory": "1g"
}
## 结果还是不行,没有权限修改,/etc/security/limit.conf为只读,出于安全和一致性的考虑,Docker 容器通常以只读的方式挂载许多系统文件和目录
docker run --user $(id -u):$(id -g) your-image
docker run --itd --privileged=true --name redis redis /bin/bash #其中,privileged=true:获得真正的root权限,使其能够访问宿主机的所有设备,并且可以执行一些通常需要特权的操作,但不一定能够直接修改 /etc/security/limit.conf 文件
docker run -it --ulimit nofile=1024:4096 redis #参数修改,nofile 表示文件描述符的数量,1024:4096 分别表示软限制和硬限制。
#修改为镜像,提交
docker commit -m “备注” -a “作者” 容器id 镜像repository
## 反构建通过镜像生成对应的dockfile文件
git clone https://github.com/CenturyLinkLabs/dockerfile-from-image.git
cd dockerfile-from-image
./bin/dockerfile-from-image <image_name> > Dockerfile
#配合
docker history <image_name> #修改Dockerfile
2.2、Docker容器工具补充
#补充ps命令
apt update
apt install procps #输出
Get:1 http://deb.debian.org/debian bookworm InRelease [151 kB]
Get:2 http://deb.debian.org/debian bookworm-updates InRelease [55.4 kB]
Get:3 http://deb.debian.org/debian-security bookworm-security InRelease [48.0 kB]
Get:4 http://deb.debian.org/debian bookworm/main amd64 Packages [8786 kB]
Get:5 http://deb.debian.org/debian bookworm-updates/main amd64 Packages [13.8 kB]
Get:6 http://deb.debian.org/debian-security bookworm-security/main amd64 Packages [155 kB]
Fetched 9209 kB in 4s (2066 kB/s)
Reading package lists... Done
root@3d19f505db4d:/var/log# apt-get install procps
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
libgpm2 libncursesw6 libproc2-0 psmisc
Suggested packages:
gpm
The following NEW packages will be installed:
libgpm2 libncursesw6 libproc2-0 procps psmisc
0 upgraded, 5 newly installed, 0 to remove and 2 not upgraded.
Need to get 1178 kB of archives.
After this operation, 3778 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://deb.debian.org/debian bookworm/main amd64 libncursesw6 amd64 6.4-4 [134 kB]
Get:2 http://deb.debian.org/debian bookworm/main amd64 libproc2-0 amd64 2:4.0.2-3 [62.8 kB]
Get:3 http://deb.debian.org/debian bookworm/main amd64 procps amd64 2:4.0.2-3 [709 kB]
Get:4 http://deb.debian.org/debian bookworm/main amd64 libgpm2 amd64 1.20.7-10+b1 [14.2 kB]
Get:5 http://deb.debian.org/debian bookworm/main amd64 psmisc amd64 23.6-1 [259 kB]
Fetched 1178 kB in 1s (2023 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package libncursesw6:amd64.
(Reading database ... 6101 files and directories currently installed.)
Preparing to unpack .../libncursesw6_6.4-4_amd64.deb ...
Unpacking libncursesw6:amd64 (6.4-4) ...
Selecting previously unselected package libproc2-0:amd64.
Preparing to unpack .../libproc2-0_2%3a4.0.2-3_amd64.deb ...
Unpacking libproc2-0:amd64 (2:4.0.2-3) ...
Selecting previously unselected package procps.
Preparing to unpack .../procps_2%3a4.0.2-3_amd64.deb ...
Unpacking procps (2:4.0.2-3) ...
Selecting previously unselected package libgpm2:amd64.
Preparing to unpack .../libgpm2_1.20.7-10+b1_amd64.deb ...
Unpacking libgpm2:amd64 (1.20.7-10+b1) ...
Selecting previously unselected package psmisc.
Preparing to unpack .../psmisc_23.6-1_amd64.deb ...
Unpacking psmisc (23.6-1) ...
Setting up libgpm2:amd64 (1.20.7-10+b1) ...
Setting up psmisc (23.6-1) ...
Setting up libproc2-0:amd64 (2:4.0.2-3) ...
Setting up libncursesw6:amd64 (6.4-4) ...
Setting up procps (2:4.0.2-3) ...