一、Docker容器管理
- 查看启动的容器
bash-3.2# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
96d50d4ae5e4 centos "/bin/bash" 22 hours ago Up 19 hours desperate_hopper
- 查看所有的容器(包括启动、停止)
bash-3.2# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4711aa1104b4 centos "/bin/bash" 18 hours ago Exited (0) 18 hours ago loving_fermi
96d50d4ae5e4 centos "/bin/bash" 22 hours ago Up 19 hours desperate_hopper
- 创建容器(create、run)、进入容器
### 用create创建容器.
bash-3.2# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
new_centos 01 c492c1644918 2 hours ago 279.3 MB
centos-6-x86_minimal latest 4cc6c9327f19 2 hours ago 326.3 MB
90root 90root 2a332da70fd1 2 weeks ago 196.7 MB
centos latest 2a332da70fd1 2 weeks ago 196.7 MB
bash-3.2# docker create -it centos-6-x86_minimal /bin/bash
9e3281c1e756ea7c67aa7ec404b16e3483b4e2be26b7f15548f116b5c86c0aaa
bash-3.2# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9e3281c1e756 centos "/bin/bash" 25 seconds ago goofy_bohr
bash-3.2# docker exec -it 9e3281c1e756 /bin/bash #进入容器
9e3281c1e756# exit
bash-3.2# docker attach 9e3281c1e756
9e3281c1e756# exit
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b527af0824ae centos-6-x86_minimal "/bin/bash" 8 minutes ago Up 8 minutes cocky_darwin
96d50d4ae5e4 centos "/bin/bash" 22 hours ago Up 19 hours desperate_hopper
# exec和attach总结: attach登陆容器后,退出时容器会关闭. 推荐使用exec进入容器
### 用run创建容器
bash-3.2# docker run -it centos-6-x86_minimal /bin/bash
80b9fd653f14# cat /etc/redhat-release
CentOS release 6.7 (Final)
80b9fd653f14# uname -r
2.6.32-573.26.1.el6.x86_64
- 启动停止容器
bash-3.2# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
80b9fd653f14 centos-6-x86_minimal "/bin/bash" 5 minutes ago Exited (0) 2 minutes ago serene_darwin
bash-3.2# docker start 80b9fd653f14
bash-3.2# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
80b9fd653f14 centos-6-x86_minimal "/bin/bash" 5 minutes ago Up 1 seconds serene_darwin
bash-3.2# docker stop 80b9fd653f14
bash-3.2# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
80b9fd653f14 centos-6-x86_minimal "/bin/bash" 6 minutes ago Exited (137) 6 seconds ago serene_darwin
- 创建容器,指定容器名
bash-3.2# docker run -itd -h 90root --name 90root centos:latest bash
9c6686180ab82b21f3153251e81b4c5848c5e8162d03d57e8e08b630ff30f12c
bash-3.2# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9c6686180ab8 centos:latest "bash" 3 seconds ago Up 2 seconds 90root
### -d: 容器退出后不关闭容器. -h:指定容器主机名.
- 删除容器/镜像
### 删除容器
bash-3.2# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d2ef72ac1353 centos-6-x86_minimal "/bin/bash" 46 minutes ago desperate_yonath
bash-3.2# docker rm d2ef72ac1353 #删除一个关闭的容器
bash-3.2# docker rm -f 96d50d4ae5e4 #删除一个启动的容器
### 删除镜像
bash-3.2# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
registry latest 07d93e41c370 4 months ago 234.9 MB
bash-3.2# docker rmi registry:latest #删除一个镜像
- 导出容器(可迁移到其它机器)/导入容器
###导出容器
bash-3.2# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9c6686180ab8 centos:latest "bash" 9 minutes ago Up 9 minutes 90root
bash-3.2# docker export 9c6686180ab8 > 90root_centos.tar
###导入容器(恢复的只是一个镜像,需要通过新镜像创建新的容器)
bash-3.2# docker rm -f 9c6686180ab8
bash-3.2# cat 90root_centos.tar | docker import - 90root_centos
345cf4e7d1a6a9747719d3bb873e6354417cc6322c06368b6c556b83b506c717
bash-3.2# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
90root_centos latest 345cf4e7d1a6 15 seconds ago 196.7 MB
bash-3.2# docker run -itd 90root_centos bash
4db99c957051af65a9fe19810752a10fe7da3e2462f67734149f4c416b58f725