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

網(wǎng)上下載的docker雖然很豐富,但總有一些需求無法滿足,比如下載Ubuntu鏡像后,沒有g(shù)cc也沒有cmake,于是需要自己安裝,安裝后最好能保存到本地,以后重新裝虛擬機時可以立即使用,因此我準(zhǔn)備自己修改鏡像并保存到本地。

運行docker

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

查看docker現(xiàn)在的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:/# 

發(fā)現(xiàn)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目錄了,把文件拷貝到這里。

提交鏡像修改內(nèi)容

  • 退出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文件就是完整的鏡像文件,后續(xù)可以通過load命令可以加載鏡像

jun@jun-u:/home$ sudo docker load -i testimg 
Loaded image ID: sha256:ac0818a6cdb6e0ce4215df037baf8def4c7d5685ed8ec3d149a3a036d4046387
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容