太长不看
先说结论
# assume you are root in container
rm /etc/apt/apt.conf.d/docker-clean
sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list
apt update
apt install -y bash-completion
vi /etc/bash.bashrc
# enable bash completion in interactive shells
source /etc/bash.bashrc
问题描述
最近需要在一个高性能Linux服务器上的Docker容器内进行远程开发
可视化已使用X11转发解决,但Docker容器使用apt install无法自动补全package名称
正常服务器输入
$ sudo apt install python3-matp<TAB>
会自动补全
$ sudo apt install python3-matplotlib
而Docker容器内使用ubuntu:18.04镜像换源后按tab没反应
分析解决
网上大多数说法是缺少bash-completion
通过apt install bash-completion
安装后开启
# vi /etc/bash.bashrc
# enable bash completion in interactive shells
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
此时已经可以补全基本语法
apt inst<TAB>
-> apt install
仍然无法补全包名,随后尝试安装重新安装
sudo apt-get install --reinstall bash-completion
无果
尝试安装auto-complete-el
sudo apt-get install auto-complete-el
无果
最终怀疑是apt配置问题导致bash-completion无法搜索包名
对比正常ubuntu桌面系统后,发现docker镜像的apt有几处不同
正常系统
ubuntu@ubuntu:/etc/apt/apt.conf.d$ ll
total 64
drwxr-xr-x 2 root root 4096 Jun 4 2020 ./
drwxr-xr-x 6 root root 4096 Jun 4 2020 ../
-rw-rw-r-- 1 root root 49 Jun 4 2020 00aptitude
-rw-rw-r-- 1 root root 40 Jun 4 2020 00trustcdrom
-rw-r--r-- 1 root root 861 Jan 19 2019 01autoremove
-r--r--r-- 1 root root 1494 Jun 4 2020 01autoremove-kernels
-rw-r--r-- 1 root root 42 Apr 14 2016 01-vendor-ubuntu
-rw-r--r-- 1 root root 168 Jun 4 2020 10periodic
-rw-r--r-- 1 root root 108 Dec 7 2018 15update-stamp
-rw-r--r-- 1 root root 85 Dec 7 2018 20archive
-rw-r--r-- 1 root root 168 Jun 4 2020 20auto-upgrades
-rw-r--r-- 1 root root 243 Dec 16 2009 20dbus
-rw-r--r-- 1 root root 1432 Apr 18 2016 50appstream
-rw-r--r-- 1 root root 2656 Feb 27 2019 50unattended-upgrades
-rw-r--r-- 1 root root 182 Nov 11 2015 70debconf
-rw-r--r-- 1 root root 305 Dec 7 2018 99update-notifier
Docker容器
root@d693b6733c01:/etc/apt/apt.conf.d# ll
total 40
drwxr-xr-x 1 root root 4096 Nov 26 06:25 ./
drwxr-xr-x 1 root root 4096 Dec 12 14:56 ../
-rw-r--r-- 1 root root 92 Apr 9 2020 01-vendor-ubuntu
-rw-r--r-- 1 root root 630 Apr 9 2020 01autoremove
-r--r--r-- 1 root root 623 Nov 6 09:21 01autoremove-kernels
-rw-r--r-- 1 root root 182 Aug 3 2019 70debconf
-rw-r--r-- 1 root root 44 Nov 26 06:25 docker-autoremove-suggests
-rw-r--r-- 1 root root 318 Nov 26 06:25 docker-clean
-rw-r--r-- 1 root root 70 Nov 26 06:25 docker-gzip-indexes
-rw-r--r-- 1 root root 27 Nov 26 06:25 docker-no-languages
可以看出容器内多了几个docker开头的文件,其中docker-clean
负责清除apt缓存减小容器体积
DPkg::Post-Invoke { "rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true"; };
APT::Update::Post-Invoke { "rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true"; };
Dir::Cache::pkgcache ""; Dir::Cache::srcpkgcache "";
原因找到了!删除docker-clean
后在进行apt update
操作重建本地缓存,命令补全恢复正常!
参考
enabling auto completion for apt-get install in docker (ubuntu 14.04) - Ask Ubuntu