目录
查看已有的镜像文件
[root@localhost ~]# docker images tomcat
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest 451d25ef4583 4 weeks ago 483MB
[root@localhost ~]#
运行镜像并修改
[root@localhost ~]# docker run -d tomcat
b513773a8ec5c1260ec75b19d905920d61bb41e5243deb591a89cd304ba0e848
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b513773a8ec5 tomcat "catalina.sh run" 4 seconds ago Up 3 seconds 8080/tcp romantic_grothendieck
a685a2897cd9 redis:6.0.8 "docker-entrypoint.s…" 19 minutes ago Up 13 minutes 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp myredis
[root@localhost ~]#
[root@localhost ~]# docker exec -it b513773a8ec5 /bin/bash
root@b513773a8ec5:/usr/local/tomcat#
root@b513773a8ec5:/usr/local/tomcat#
root@b513773a8ec5:/usr/local/tomcat# cd ~
root@b513773a8ec5:~#
root@b513773a8ec5:~#
root@b513773a8ec5:~# ls
root@b513773a8ec5:~# echo "hello world!!!" > 123.txt
echo "hello worldls!" > 123.txt
root@b513773a8ec5:~#
root@b513773a8ec5:~#
root@b513773a8ec5:~# cat 123.txt
hello worldls!
root@b513773a8ec5:~#
将镜像打包成本地文件
特别注意,通过save 只是把镜像保存到本地,并没有把修改的内容也保存到本地,如果需要保存修改,需要使用docker commit 命令,详见:docker commit 操作实例
[root@localhost ~]# docker images tomcat
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest 451d25ef4583 4 weeks ago 483MB
[root@localhost ~]#
[root@localhost ~]# docker save 451d25ef4583 > tomcatV2.0.tar
[root@localhost ~]# ls -lh
total 566M
-rw-r--r--. 1 root root 6 Jul 10 10:10 123.txt
-rw-------. 1 root root 2.0K Jul 9 23:00 anaconda-ks.cfg
-rw-------. 1 root root 2.2K Jul 9 23:00 original-ks.cfg
-rw-r--r--. 1 root root 100M Jul 10 10:18 redis_bak.tar
-rw-r--r--. 1 root root 466M Jul 31 10:56 tomcatV2.0.tar
在另一台主机加载本地文件到镜像
[root@localhost ~]# docker load <tomcatV2.0.tar
Loaded image ID: sha256:451d25ef4583d58da6cb84bbbfd36aa6e752a8efa3d9a137da9b77334e7b902b
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 3147495b3a5c 4 days ago 431MB
<none> <none> 451d25ef4583 4 weeks ago 483MB
redis latest 2e50d70ba706 5 weeks ago 117MB
ubuntu latest 27941809078c 7 weeks ago 77.8MB
registry latest 773dbf02e42e 2 months ago 24.1MB
hello-world latest feb5d9fea6a5 10 months ago 13.3kB
centos latest 5d0da3dc9764 10 months ago 231MB
192.168.2.250:5000/redis 6.0.8 16ecd2772934 21 months ago 104MB
redis 6.0.8 16ecd2772934 21 months ago 104MB
billygoo/tomcat8-jdk8 latest 30ef4019761d 3 years ago 523MB
[root@localhost ~]#
镜像重命名
执行完上述语句后,查看本地镜像,会看到新加载的镜像名字和标签都是none,利用该镜像的id对名字和标签重新命名即可:
指令:docker tag 镜像id 镜像名:标签<none> <none> 451d25ef4583 4 weeks ago 483MB
[root@localhost ~]# docker tag 451d25ef4583 tomcatv2_0:v2.0
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 3147495b3a5c 4 days ago 431MB
tomcatv2_0 v2.0 451d25ef4583 4 weeks ago 483MB
redis latest 2e50d70ba706 5 weeks ago 117MB
ubuntu latest 27941809078c 7 weeks ago 77.8MB
registry latest 773dbf02e42e 2 months ago 24.1MB
hello-world latest feb5d9fea6a5 10 months ago 13.3kB
centos latest 5d0da3dc9764 10 months ago 231MB
192.168.2.250:5000/redis 6.0.8 16ecd2772934 21 months ago 104MB
redis 6.0.8 16ecd2772934 21 months ago 104MB
billygoo/tomcat8-jdk8 latest 30ef4019761d 3 years ago 523MB
[root@localhost ~]#
重新运行
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 3147495b3a5c 4 days ago 431MB
tomcatv2_0 v2.0 451d25ef4583 4 weeks ago 483MB
redis latest 2e50d70ba706 5 weeks ago 117MB
ubuntu latest 27941809078c 7 weeks ago 77.8MB
registry latest 773dbf02e42e 2 months ago 24.1MB
hello-world latest feb5d9fea6a5 10 months ago 13.3kB
centos latest 5d0da3dc9764 10 months ago 231MB
192.168.2.250:5000/redis 6.0.8 16ecd2772934 21 months ago 104MB
redis 6.0.8 16ecd2772934 21 months ago 104MB
billygoo/tomcat8-jdk8 latest 30ef4019761d 3 years ago 523MB
[root@localhost ~]# docker run -d tomcatv2_0:v2.0
4bf5bb3a247f15146f13b6b8ca5dc0546a5e31064344cdcf70963f4bc34df66b
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4bf5bb3a247f tomcatv2_0:v2.0 "catalina.sh run" 5 seconds ago Up 4 seconds 8080/tcp zealous_johnson
a685a2897cd9 redis:6.0.8 "docker-entrypoint.s…" 40 minutes ago Up 34 minutes 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp myredis
[root@localhost ~]# docker exec -it 4bf5bb3a247f /bin/bash
root@4bf5bb3a247f:/usr/local/tomcat#
root@4bf5bb3a247f:/usr/local/tomcat# cd ~
root@4bf5bb3a247f:~# ls
root@4bf5bb3a247f:~#