网上下载的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