修改docker镜像&保存镜像文件

网上下载的docker虽然很丰富,但总有一些需求无法满足,比如下载Ubuntu镜像后,没有gcc也没有cmake,于是需要自己安装,安装后最好能保存到本地,以后重新装虚拟机时可以立即使用,因此我准备自己修改镜像并保存到本地。

运行docker

jun@jun-u:/home$ sudo docker run -it -p 9933:22 ubuntu /bin/bash
[sudo] password for jun: 
root@63243393225c:/# 

查看docker现在的gcc版本

root@63243393225c:/# gcc -v
bash: gcc: command not found
root@63243393225c:/# g++
bash: g++: command not found
root@63243393225c:/# cmake -v
bash: cmake: command not found
root@63243393225c:/# python3 -v
bash: python3: command not found
root@63243393225c:/# python -v 
bash: python: command not found
root@63243393225c:/# 

发现Ubuntu官方的镜像没有安装gcc、cmake、python

更新apt-get

root@63243393225c:/# apt-get update
Get:1 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:2 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]
Get:3 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [1335 kB]
Get:4 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:5 http://archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Get:6 http://archive.ubuntu.com/ubuntu focal/main amd64 Packages [1275 kB]
Get:7 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 Packages [30.1 kB]
Get:8 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 Packages [733 kB]
Get:9 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [828 kB]
Get:10 http://archive.ubuntu.com/ubuntu focal/multiverse amd64 Packages [177 kB]
Get:11 http://archive.ubuntu.com/ubuntu focal/restricted amd64 Packages [33.4 kB]
Get:12 http://archive.ubuntu.com/ubuntu focal/universe amd64 Packages [11.3 MB]
Get:13 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [1758 kB]
Get:14 http://archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 Packages [33.6 kB]
Get:15 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [1108 kB]
Get:16 http://archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packages [797 kB]
Get:17 http://archive.ubuntu.com/ubuntu focal-backports/main amd64 Packages [50.8 kB]
Get:18 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 Packages [21.7 kB]
Fetched 20.1 MB in 10s (1954 kB/s)                                             
Reading package lists... Done
root@63243393225c:/# 

安装gcc、g++、cmake、python

root@63243393225c:/# apt-get install gcc
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  binutils binutils-common binutils-x86-64-linux-gnu cpp cpp-9 gcc-9
  gcc-9-base libasan5 libatomic1 libbinutils libc-dev-bin libc6-dev libcc1-0
  libcrypt-dev libctf-nobfd0 libctf0 libgcc-9-dev libgomp1 libisl22 libitm1
  liblsan0 libmpc3 libmpfr6 libquadmath0 libtsan0 libubsan1 linux-libc-dev
  manpages manpages-dev
Suggested packages:
  binutils-doc cpp-doc gcc-9-locales gcc-multilib make autoconf automake
  libtool flex bison gdb gcc-doc gcc-9-multilib gcc-9-doc glibc-doc
  man-browser
The following NEW packages will be installed:
  binutils binutils-common binutils-x86-64-linux-gnu cpp cpp-9 gcc gcc-9
  gcc-9-base libasan5 libatomic1 libbinutils libc-dev-bin libc6-dev libcc1-0
  libcrypt-dev libctf-nobfd0 libctf0 libgcc-9-dev libgomp1 libisl22 libitm1
  liblsan0 libmpc3 libmpfr6 libquadmath0 libtsan0 libubsan1 linux-libc-dev
  manpages manpages-dev
0 upgraded, 30 newly installed, 0 to remove and 1 not upgraded.
Need to get 33.2 MB of archives.
After this operation, 139 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

等待安装即可。
另外几个相同方法:

root@63243393225c:/# apt-get install g++
root@63243393225c:/# apt-get install cmake
root@63243393225c:/# apt-get install python3

查看安装的版本

root@592a3d1e06e8:/usr1# gcc -v     
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/9/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 9.3.0-17ubuntu1~20.04' --with-bugurl=file:///usr/share/doc/gcc-9/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,gm2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-9 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-9-HskZEa/gcc-9-9.3.0/debian/tmp-nvptx/usr,hsa --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04) 
root@592a3d1e06e8:/usr1# 
root@592a3d1e06e8:/usr1# g++ --version
g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

root@592a3d1e06e8:/usr1#
root@592a3d1e06e8:/usr1# cmake --version
cmake version 3.16.3

CMake suite maintained and supported by Kitware (kitware.com/cmake).
root@592a3d1e06e8:/usr1#
root@592a3d1e06e8:/usr1# python3 --version
Python 3.8.10
root@592a3d1e06e8:/usr1# 

拷贝镜像要保存的脚本

root@592a3d1e06e8:/usr1# cd 
root@592a3d1e06e8:~# ll
total 52
drwx------ 1 root root  4096 Jan  2 12:55 ./
drwxr-xr-x 1 root root  4096 Jan  2 12:18 ../
-rw------- 1 root root  4019 Jan  2 12:41 .bash_history
-rw-r--r-- 1 root root  3106 Dec  5  2019 .bashrc
drwx------ 2 root root  4096 Oct 31 17:20 .cache/
drwx------ 3 root root  4096 Oct 31 16:58 .config/
-rw-r--r-- 1 root root   161 Dec  5  2019 .profile
drwxr-xr-x 2 root root  4096 Oct 31 17:12 .vim/
-rw------- 1 root root 10322 Jan  2 12:29 .viminfo
-rw-r--r-- 1 root root  1368 Jan  2 11:40 create.sh
root@592a3d1e06e8:~# 

比如要把create.sh脚本放到镜像里面,需要把文件拷贝到用户home目录,在任意目录敲cd就进到用户hone目录了,把文件拷贝到这里。

提交镜像修改内容

  • 退出docker,查看所有docker
root@592a3d1e06e8:~# exit
exit
jun@jun-u:/home$ sudo docker ps -a
CONTAINER ID   IMAGE       COMMAND       CREATED          STATUS                      PORTS     NAMES
63243393225c   ubuntu      "/bin/bash"   17 minutes ago   Exited (0) 7 minutes ago              upbeat_zhukovsky
592a3d1e06e8   ubuntu:v2   "/bin/bash"   40 minutes ago   Exited (0) 23 seconds ago             sweet_chandrasekhar
jun@jun-u:/home$
  • 提交修改
jun@jun-u:/home$ sudo docker commit -m "comment" -a "jun" 63 ubuntu:v3
sha256:e41264d46bf97bcf9aaacc7cdbebe92a656f678bf4a2c31075cedccbc2ae5db9
jun@jun-u:/home$ 

-m:备注信息
-a:提交作者
63:docker id
ubuntu:v3:镜像名称
以上信息在生成成功后,通过查询镜像信息可以看到

jun@jun-u:/home$ sudo docker images 
REPOSITORY   TAG       IMAGE ID       CREATED             SIZE
ubuntu       v3        e41264d46bf9   4 minutes ago       105MB
ubuntu       v2        ac0818a6cdb6   About an hour ago   480MB
ubuntu       latest    ba6acccedd29   2 months ago        72.8MB

保存镜像文件

jun@jun-u:/home$ sudo docker save -o testimg ac
jun@jun-u:/home$ ll
total 968220
drwxr-xr-x  3 root root      4096 1月   2 13:08 ./
drwxr-xr-x 22 root root      4096 1月   1 22:29 ../
drwxr-xr-x 17 jun  jun       4096 10月 31 17:44 jun/
-rw-------  1 root root 495715840 1月   2 13:08 testimg
-rwxrwxrwx  1 root root 495716864 1月   2 11:57 ubuntu_docker_cmake*
jun@jun-u:/home$ 

testimg:镜像文件名
ac:惊醒ID
生成的testimg文件就是完整的镜像文件,后续可以通过load命令可以加载镜像

jun@jun-u:/home$ sudo docker load -i testimg 
Loaded image ID: sha256:ac0818a6cdb6e0ce4215df037baf8def4c7d5685ed8ec3d149a3a036d4046387
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,612评论 5 471
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,345评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,625评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,022评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,974评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,227评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,688评论 3 392
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,358评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,490评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,402评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,446评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,126评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,721评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,802评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,013评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,504评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,080评论 2 341

推荐阅读更多精彩内容