IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发工具 -> Docker容器安装vim、net-tools等失败解决方案 -> 正文阅读

[开发工具]Docker容器安装vim、net-tools等失败解决方案

1. 背景

对于初学Docker的小伙伴们,刚学会了拉取镜像,创建容器,进入容器,但却在容器内部被一些常用的Linux命令(如:vi、vim、ifconfig、netstat…)无法使用搞得焦头烂额,本文将对这些问题提出解决方案;

案例如下:

在安装好docker的前提下;

拉取的tomcat镜像如下:

[root@centos7 ~]# docker images
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 ~]# docker run -id -P --name=tomcat-test tomcat:9.0.50-jdk8
5fb32eaa3c6495d76e0896dfd6b9b589ed79081a2e20696dfab5c981846e8328
[root@centos7 ~]# docker ps -a
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 ~]# docker exec -it tomcat-test /bin/bash
root@5fb32eaa3c64:/usr/local/tomcat# vim test.txt
bash: vim: command not found

通过网上各种博客,其一说需要使用apt-get install vim命令安装vim,可结果如下:

root@5fb32eaa3c64:/usr/local/tomcat# apt-get install vim
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命令执行失败;

总结

  1. 因docker容器都是属于比较轻量级的,所以有些镜像对容器的系统做了很多删减的操作,导致一些常规命令无法使用属于正常现象
  2. apt-get update命令可能因为网络原因失败,因为默认源是国外的地址
  3. 国内有不同发行版系统的对于源地址,如阿里云,清华大学源等等,但切记一定要把容器系统发行版和版本号与源地址对应上,否则就无法成功

2. 解决方案

1. 查看容器系统发行版本

  • 命令1: lsb_release -a

  • 命令2: cat /etc/issue,可得具体发行版本和版本号

    root@5fb32eaa3c64:/usr/local/tomcat# cat /etc/issue
    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# mv /etc/apt/sources.list /etc/apt/sources.list.bak

4. 新建/etc/apt/sources.list文件,并写入国内源地址

root@5fb32eaa3c64:/usr/local/tomcat# cat >> /etc/apt/sources.list << EOF
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# apt-get update
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# apt-get install vim
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 --help
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
......
  开发工具 最新文章
Postman接口测试之Mock快速入门
ASCII码空格替换查表_最全ASCII码对照表0-2
如何使用 ssh 建立 socks 代理
Typora配合PicGo阿里云图床配置
SoapUI、Jmeter、Postman三种接口测试工具的
github用相对路径显示图片_GitHub 中 readm
Windows编译g2o及其g2o viewer
解决jupyter notebook无法连接/ jupyter连接
Git恢复到之前版本
VScode常用快捷键
上一篇文章      下一篇文章      查看所有文章
加:2021-07-23 11:00:29  更:2021-07-23 11:02:19 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/20 21:22:44-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码