首先啟動docker后,查看有哪些鏡像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest dd34e67e3371 39 hours ago 133 MB
centos 6.9 2199b8eb8390 2 years ago 195 MB
ansible/ansible centos7 0731001e75a9 4 years ago 669 MB
導出容器使用 docker export命令
在導出之前先讓容器運行起來
[root@localhost file]# docker run -it --name "testcentos" centos:6.9 /bin/bash #運行容器
[root@cbe19adb8143 /]# 發生變化,說明進入容器
[root@localhost file]# docker run -it --name "testnginx" nginx /bin/bash
root@954d7c1b68b2:/#
[root@localhost file]# docker ps #查看正在運行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
954d7c1b68b2 nginx "/docker-entrypoin..." About a minute ago Up About a minute 80/tcp testnginx (容器名指定)
cbe19adb8143 centos:6.9 "/bin/bash" 4 minutes ago Up 4 minutes testcentos
[root@localhost file]# docker export cbe19adb8143 >centos6.9.tar #容器導出
[root@localhost file]# ls
centos6.9.tar our.sql wangju.sql
導入容器使用docker import命令
[root@localhost file]# cat centos6.9.tar | docker import - my/centos:v888 #導入容器
sha256:324dd4d2d58361e834c322367aa7d3d8ecfef5c6e1693dadea483bfa3128f533
[root@localhost file]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my/centos v888 324dd4d2d583 About a minute ago 195 MB
nginx latest dd34e67e3371 39 hours ago 133 MB
centos 6.9 2199b8eb8390 2 years ago 195 MB
ansible/ansible centos7 0731001e75a9 4 years ago 669 MB
docker容器映射端口
[root@localhost file]# docker run -d -P nginx #使用-P映射隨機端口
6580bd906620a26e294ce435e834fa7e40e2877f91268b0156c5e7abaa3f7c76
[root@localhost file]# docker run -d -p 8080:80 nginx #使用-p映射指定端口
b085c3481ade402c56d4c6a2f1ad0c54778a7a1865ef0606b51cd34e6a735104
查看端口映射
[root@localhost file]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b085c3481ade nginx "/docker-entrypoin..." 2 minutes ago Up 2 minutes 0.0.0.0:8080->80/tcp tender_curie #名字是隨機生成的
6580bd906620 nginx "/docker-entrypoin..." 3 minutes ago Up 3 minutes 0.0.0.0:32768->80/tcp (端口是隨機分配的)boring_gates
954d7c1b68b2 ngin "/docker-entrypoin..." 21 minutes ago Up 21 minutes 80/tcp testnginx
cbe19adb8143 centos:6.9 "/bin/bash" 24 minutes ago Up 24 minutes testcentos
目錄映射
[root@localhost file]# docker run --name test -it --privileged -v /tmp/file:/tmp/soft centos:6.9 /bin/bash #黃色的是宿主機的目錄,綠色的是容器的目錄
[root@70e54b3a3651 /]# cd /tmp/soft #看綠色處說明進入了容器
[root@70e54b3a3651 soft]# ls #看紅色處說明進入了容器中的soft目錄
centos6.9.tar our.sql wangju.sql
[root@70e54b3a3651 soft]#
通過圖片可以看出容器目錄中的內容與宿主機中的目錄內容一致。