首先启动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]#
通过图片可以看出容器目录中的内容与宿主机中的目录内容一致。