在学习docker使用的时候,遇到一个小的但蛮有意思的问题,做一点记录,也供诸位一乐。
问题描述
遇到了一个现实生活中的第二十二条军规,在docker中使用ubuntu镜像,但是由于服务器架设在国外所以无论是apt update
或者apt install
都频繁导致超时。为了解决这种情况,大家都知道应当对apt的源进行换源,换源需要修改/etc/apt/sources.list
文件,但是ubuntu镜像初始时不带vi或者vim工具,所以需要先进行apt update
再进行安装。所以导致了一个死循环。
解决方案
忽然想到echo
指令可以对文件进行写入,其实平时很少使用到这一指令,但实在是没办法打破这一个逻辑死循环。
用echo
指令将结果定向至文件
echo "It is a test" > myfile
所以写入源的命令如下:(使用阿里源)
mv /etc/apt/sources.list /etc/apt/sources.list.back #备份原始文件以防万一
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse" >>/etc/apt/sources.list
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse" >>/etc/apt/sources.list
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse" >>/etc/apt/sources.list
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse" >>/etc/apt/sources.list
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse" >>/etc/apt/sources.list
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse" >>/etc/apt/sources.list
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse" >>/etc/apt/sources.list
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse" >>/etc/apt/sources.list
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse" >>/etc/apt/sources.list
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse" >>/etc/apt/sources.list