1. 背景
对于初学Docker的小伙伴们,刚学会了拉取镜像,创建容器,进入容器,但却在容器内部被一些常用的Linux命令(如:vi、vim、ifconfig、netstat…)无法使用搞得焦头烂额,本文将对这些问题提出解决方案;
案例如下:
在安装好docker的前提下;
拉取的tomcat镜像如下:
[root@centos7 ~]
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat 9.0.50-jdk8 8ffa9facce42 2 weeks ago 534MB
hello-world latest d1165f221234 4 months ago 13.3kB
以该tomcat镜像创建一个名为tomcat-test的容器,如下:
[root@centos7 ~]
5fb32eaa3c6495d76e0896dfd6b9b589ed79081a2e20696dfab5c981846e8328
[root@centos7 ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5fb32eaa3c64 tomcat:9.0.50-jdk8 "catalina.sh run" 10 seconds ago Up 8 seconds 0.0.0.0:49153->8080/tcp tomcat-test
进入tomcat-test容器,发现vim命令无法使用
[root@centos7 ~]
root@5fb32eaa3c64:/usr/local/tomcat
bash: vim: command not found
通过网上各种博客,其一说需要使用apt-get install vim 命令安装vim,可结果如下:
root@5fb32eaa3c64:/usr/local/tomcat
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package vim
其二说安装之前需要使用apt-get update 命令同步源的索引,再执行apt-get install vim 命令安装vim,结果可能还是如此;
其三说需要更换为国内的apt源,但却没有提出通过不同镜像创建的容器, 因为容器系统的发行版不同,所以源的地址也不同 ,结果导致apt-get update 命令执行失败;
总结
- 因docker容器都是属于比较轻量级的,所以有些镜像对容器的系统做了很多删减的操作,导致一些常规命令无法使用属于正常现象
apt-get update 命令可能因为网络原因失败,因为默认源是国外的地址- 国内有不同发行版系统的对于源地址,如阿里云,清华大学源等等,
但切记一定要把容器系统发行版和版本号与源地址对应上 ,否则就无法成功
2. 解决方案
1. 查看容器系统发行版本
-
命令1: lsb_release -a -
命令2: cat /etc/issue ,可得具体发行版本和版本号 root@5fb32eaa3c64:/usr/local/tomcat
Debian GNU/Linux 10 \n \l
-
命令3: cat /etc/redhat-releasex
2. 搜索国内该发行版本源地址
例如,搜索Debian发行版的阿里云的源地址,可在百度搜索Debian 官方阿里云源 ,注意版本号,可阿里云网站上得到如下结果:
deb http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb http://mirrors.aliyun.com/debian-security buster/updates main
deb-src http://mirrors.aliyun.com/debian-security buster/updates main
deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib
3. 备份/etc/apt/sources.list文件
root@5fb32eaa3c64:/usr/local/tomcat
4. 新建/etc/apt/sources.list文件,并写入国内源地址
root@5fb32eaa3c64:/usr/local/tomcat
deb http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb http://mirrors.aliyun.com/debian-security buster/updates main
deb-src http://mirrors.aliyun.com/debian-security buster/updates main
deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib
EOF
5. 同步
root@5fb32eaa3c64:/usr/local/tomcat
Get:1 http://mirrors.aliyun.com/debian buster InRelease [122 kB]
Get:2 http://mirrors.aliyun.com/debian-security buster/updates InRelease [65.4 kB]
Get:3 http://mirrors.aliyun.com/debian buster-updates InRelease [51.9 kB]
Get:4 http://mirrors.aliyun.com/debian buster-backports InRelease [46.7 kB]
Get:5 http://mirrors.aliyun.com/debian buster/non-free Sources [85.7 kB]
Get:6 http://mirrors.aliyun.com/debian buster/contrib Sources [42.5 kB]
Get:7 http://mirrors.aliyun.com/debian buster/main Sources [7836 kB]
Get:8 http://mirrors.aliyun.com/debian buster/non-free amd64 Packages [87.7 kB]
Get:9 http://mirrors.aliyun.com/debian buster/contrib amd64 Packages [50.1 kB]
Get:10 http://mirrors.aliyun.com/debian buster/main amd64 Packages [7907 kB]
Get:11 http://mirrors.aliyun.com/debian-security buster/updates/main Sources [195 kB]
Get:12 http://mirrors.aliyun.com/debian-security buster/updates/main amd64 Packages [297 kB]
Get:13 http://mirrors.aliyun.com/debian buster-updates/main Sources [16.6 kB]
Get:14 http://mirrors.aliyun.com/debian buster-updates/main amd64 Packages [15.2 kB]
Get:15 http://mirrors.aliyun.com/debian buster-backports/main Sources [479 kB]
Get:16 http://mirrors.aliyun.com/debian buster-backports/contrib Sources [5080 B]
Get:17 http://mirrors.aliyun.com/debian buster-backports/non-free Sources [9148 B]
Get:18 http://mirrors.aliyun.com/debian buster-backports/main amd64 Packages [490 kB]
Get:19 http://mirrors.aliyun.com/debian buster-backports/contrib amd64 Packages [9704 B]
Get:20 http://mirrors.aliyun.com/debian buster-backports/non-free amd64 Packages [34.4 kB]
Fetched 17.8 MB in 3s (5398 kB/s)
Reading package lists... Done
6. 安装
root@5fb32eaa3c64:/usr/local/tomcat
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libgpm2 vim-common vim-runtime xxd
Suggested packages:
gpm ctags vim-doc vim-scripts
The following NEW packages will be installed:
libgpm2 vim vim-common vim-runtime xxd
0 upgraded, 5 newly installed, 0 to remove and 2 not upgraded.
Need to get 7425 kB of archives.
After this operation, 33.8 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://mirrors.aliyun.com/debian buster/main amd64 xxd amd64 2:8.1.0875-5 [140 kB]
Get:2 http://mirrors.aliyun.com/debian buster/main amd64 vim-common all 2:8.1.0875-5 [195 kB]
Get:3 http://mirrors.aliyun.com/debian buster/main amd64 libgpm2 amd64 1.20.7-5 [35.1 kB]
Get:4 http://mirrors.aliyun.com/debian buster/main amd64 vim-runtime all 2:8.1.0875-5 [5775 kB]
Get:5 http://mirrors.aliyun.com/debian buster/main amd64 vim amd64 2:8.1.0875-5 [1280 kB]
Fetched 7425 kB in 0s (16.9 MB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package xxd.
(Reading database ... 12572 files and directories currently installed.)
Preparing to unpack .../xxd_2%3a8.1.0875-5_amd64.deb ...
Unpacking xxd (2:8.1.0875-5) ...
Selecting previously unselected package vim-common.
Preparing to unpack .../vim-common_2%3a8.1.0875-5_all.deb ...
Unpacking vim-common (2:8.1.0875-5) ...
Selecting previously unselected package libgpm2:amd64.
Preparing to unpack .../libgpm2_1.20.7-5_amd64.deb ...
Unpacking libgpm2:amd64 (1.20.7-5) ...
Selecting previously unselected package vim-runtime.
Preparing to unpack .../vim-runtime_2%3a8.1.0875-5_all.deb ...
Adding 'diversion of /usr/share/vim/vim81/doc/help.txt to /usr/share/vim/vim81/doc/help.txt.vim-tiny by vim-runtime'
Adding 'diversion of /usr/share/vim/vim81/doc/tags to /usr/share/vim/vim81/doc/tags.vim-tiny by vim-runtime'
Unpacking vim-runtime (2:8.1.0875-5) ...
Selecting previously unselected package vim.
Preparing to unpack .../vim_2%3a8.1.0875-5_amd64.deb ...
Unpacking vim (2:8.1.0875-5) ...
Setting up libgpm2:amd64 (1.20.7-5) ...
Setting up xxd (2:8.1.0875-5) ...
Setting up vim-common (2:8.1.0875-5) ...
Setting up vim-runtime (2:8.1.0875-5) ...
Setting up vim (2:8.1.0875-5) ...
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vim (vim) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vimdiff (vimdiff) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rvim (rvim) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rview (rview) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vi (vi) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/view (view) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/ex (ex) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/editor (editor) in auto mode
Processing triggers for libc-bin (2.28-10) ...
Processing triggers for mime-support (3.62) ...
7. 成功
root@5fb32eaa3c64:/usr/local/tomcat
VIM - Vi IMproved 8.1 (2018 May 18, compiled Jun 15 2019 16:41:15)
Usage: vim [arguments] [file ..] edit specified file(s)
or: vim [arguments] - read text from stdin
or: vim [arguments] -t tag edit file where tag is defined
or: vim [arguments] -q [errorfile] edit file with first error
......
|