如果要导出本地某个容器,可以使用docker export
命令:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8e847a221af5 ubuntu "/bin/bash" 14 hours ago Exited (0) 5 seconds ago distracted_lalande
888b1857981e ubuntu "/bin/bash" 14 hours ago Exited (0) 14 hours ago boring_dijkstra
c74c8288ae0b ubuntu:14.04 "/bin/bash" 15 hours ago Exited (0) 15 hours ago awesome_goldstine
$ docker export 888 > ubuntu.tar
$ ls
Applications IdeaProjects Pictures eclipse-workspace ubuntu.tar
注意:导出容器的容器ID可以简写,去前面三个字符
可以看见我们把容器已经导出了。
可以使用docker import
从容器快照文件中再导人为镜像,在操作这个之前,我们可以找个容器,进去修改一些东西。
再将修改的容器导入为镜像文件,然后查看我们修改的文件是否还存在:
B000000094106A:~ zhanghao53$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8e847a221af5 ubuntu "/bin/bash" 14 hours ago Exited (0) 12 minutes ago distracted_lalande
888b1857981e ubuntu "/bin/bash" 14 hours ago Exited (0) 14 hours ago boring_dijkstra
c74c8288ae0b ubuntu:14.04 "/bin/bash" 15 hours ago Exited (0) 15 hours ago awesome_goldstine
B000000094106A:~ zhanghao53$ docker start 888
888
$ docker exec -it 888 bash
root@888b1857981e:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@888b1857981e:/# mkdir zhanghao
root@888b1857981e:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var zhanghao
root@888b1857981e:/# cd zhanghao/
root@888b1857981e:/zhanghao# ls
root@888b1857981e:/zhanghao# vim
bash: vim: command not found
root@888b1857981e:/zhanghao# apt-get update
Get:1 http://archive.ubuntu.com/ubuntu bionic InRelease [242 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [65.4 kB]
... ...
Fetched 24.9 MB in 35s (721 kB/s)
Reading package lists... Done
root@888b1857981e:/zhanghao# apt-get install vim
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
file libexpat1 libgpm2 libmagic-mgc libmagic1 libmpdec2 libpython3.6 libpython3.6-minimal libpython3.6-stdlib libreadline7 libsqlite3-0
libssl1.1 mime-support readline-common vim-common vim-runtime xxd xz-utils
Suggested packages:
gpm readline-doc ctags vim-doc vim-scripts
The following NEW packages will be installed:
file libexpat1 libgpm2 libmagic-mgc libmagic1 libmpdec2 libpython3.6 libpython3.6-minimal libpython3.6-stdlib libreadline7 libsqlite3-0
libssl1.1 mime-support readline-common vim vim-common vim-runtime xxd xz-utils
0 upgraded, 19 newly installed, 0 to remove and 0 not upgraded.
Need to get 12.7 MB of archives.
After this operation, 61.0 MB of additional disk space will be used.
......
root@888b1857981e:/zhanghao# vim fasdfa.txt
root@888b1857981e:/zhanghao# ls -l
total 4
-rw-r--r-- 1 root root 138 May 10 02:45 fasdfa.txt
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
888b1857981e ubuntu "/bin/bash" 14 hours ago Up 4 minutes boring_dijkstra
$ docker export 888 > ubuntu.tar
B000000094106A:~ zhanghao53$ ls
Applications IdeaProjects Pictures eclipse-workspace ubuntu.tar
$ cat ubuntu.tar | docker import - test/ubuntu:v1.0
sha256:cde190ed9432a2b79d0d2a389d4b41e26b9c50d0f314c6b2130a430956f6bbf9
$ docker images;
REPOSITORY TAG IMAGE ID CREATED SIZE
test/ubuntu v1.0 cde190ed9432 16 seconds ago 168.5 MB
ubuntu 14.04 8cef1fa16c77 12 days ago 222.9 MB
ubuntu latest 452a96d81c30 12 days ago 79.62 MB
ubuntu 17.10 e4422b8da209 12 days ago 99.22 MB
$ docker run -dit cde /bin/bash
373635d352ca2c4582aa9aa8373d8108c1f92f647a364e20814d7970f84dc367
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
373635d352ca cde "/bin/bash" 8 seconds ago Up 7 seconds amazing_leavitt
888b1857981e ubuntu "/bin/bash" 14 hours ago Up 7 minutes boring_dijkstra
$ docker exec -it 373 bash
root@373635d352ca:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var zhanghao
root@373635d352ca:/# cd zhanghao/
root@373635d352ca:/zhanghao# ls -l
total 4
-rw-r--r-- 1 root root 138 May 10 02:45 fasdfa.txt
除此之外还可以通过指定URL或者某个目录来导入:
docker import http://example.com/exampleimage.tgz example/imagerepo
使用docker rm 'CONTAINER ID'
命令可以删除容器
在Docker 1.13版本以后,可以使用docker container prune
我的版本你不符合,大家可以自己实验一下
首先明确一点save、load是操作镜像的,而export、import是操作容器的,这个是根本的区别。
Save命令用于持久化镜像,可以通过下面的命令来保存镜像:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f96b8d635fab ubuntu "/bin/bash" 13 minutes ago Up 13 minutes sick_fermi
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 452a96d81c30 12 days ago 79.62 MB
$ docker save 452 > ubuntu:latest.tar
$ ls -lh ubuntu\:latest.tar
-rw-r--r-- 1 soygrow INTERNAL\Domain Users 78M 5 10 11:41 ubuntu:latest.tar
// 首先把容器和镜像都删了
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
// 从保存的镜像文件中加载
$ docker load < ubuntu\:latest.tar
65bdd50ee76a: Loading layer [==================================================>] 82.09 MB/82.09 MB
ec75999a0cb1: Loading layer [==================================================>] 15.87 kB/15.87 kB
67885e448177: Loading layer [==================================================>] 8.192 kB/8.192 kB
8db5f072feec: Loading layer [==================================================>] 5.632 kB/5.632 kB
059ad60bcacf: Loading layer [==================================================>] 3.072 kB/3.072 kB
Loaded image ID: sha256:452a96d81c30a1e426bc250428263ac9ca3f47c9bf086f876d11cb39cf57aeec
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
452a96d81c30 12 days ago 79.62 MB
$ docker tag 452 ubuntu:latest
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 452a96d81c30 12 days ago 79.62 MB
注意:docker load 加载镜像后repository和tag名称都为none,这里可以使用 docker tag 命令添加相应的REPOSITORY和TAG
首先这两个命令是操作容器的,和save、load不同,这两个命令的使用方法在上文中已经介绍了。
docker import
可以重新指定镜像的名字,docker load
不可以docker export
导出的版本会比原来的版本小点,因为导出的版本会丢失历史和元数据导出后再导入(exported-imported)的镜像会丢失所有的历史,而保存后再加载(saveed-loaded)的镜像没有丢失历史和层(layer)。这意味着使用导出后再导入的方式,你将无法回滚到之前的层(layer),同时,使用保存后再加载的方式持久化整个镜像,就可以做到层回滚(可以执行docker tag 来回滚之前的层)