docker开启和关闭容器
```
docker start nginx-cntf1
docker stop nginx-cntf2
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0df345d808d5 nginx "nginx -g 'daemon of…" 3 minutes ago Up 13 seconds 80/tcp nginx-cntf2
9b7a95f7458e ubuntu "/bin/bash" 13 days ago Up 4 minutes eager_rosalind
dbafe142287d nginx "nginx -g 'daemon of…" 13 days ago Up 4 seconds 80/tcp nginx-cntf1
[root@localhost ~]# docker kill nginx-cntf2
nginx-cntf2
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9b7a95f7458e ubuntu "/bin/bash" 13 days ago Up 6 minutes eager_rosalind
dbafe142287d nginx "nginx -g 'daemon of…" 13 days ago Up About a minute 80/tcp nginx-cntf1
```
docker删除容器
```
正常删除
docker rm nginx-cntf2
docker rm 31a3e8eb4b94
强制删除
[root@localhost ~]# docker rm 9b7a95f7458e
Error response from daemon: You cannot remove a running container 9b7a95f7458e018188d7e1739acea584607d801423c3a26cb8742e34ef987183. Stop the container before attempting removal or force remove
[root@localhost ~]# docker rm -f 9b7a95f7458e
9b7a95f7458e
[root@localhost ~]#
批量删除
[root@localhost ~]# docker rm -f $(docker ps -a -q)
25c1076c138b
d9302fd83449
2b4546fb72e0
d05d62f99309
ea857695a934
1215fbb51daf
a86a6c0530e1
42d630812029
4cbe68180053
79a45ef1cd1d
dbafe142287d
```
docker容器进入和退出
```
创建容器并进入
docker run -it --name nginx1010 nginx bash
docker run -it --name centos1011 centos /bin/bash
手动方式进入
以一个守护进程启动容器
[root@localhost ~]# docker run -itd --name cntf-nginx1 nginx bash
a6151efb502f01ed037c9afb2c48fbc5eb0db53c8e27110967830140df53ba02
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a6151efb502f nginx "bash" 11 seconds ago Up 9 seconds 80/tcp cntf-nginx1
656aac6c3b21 nginx "bash" 5 minutes ago Exited (127) 5 minutes ago nginx1010
58fcbcb55211 centos "/bin/bash" 6 minutes ago Exited (127) 6 minutes ago centos1011
9e91890fbdba centos "bash" 7 minutes ago Exited (0) 7 minutes ago centos1010
[root@localhost ~]# docker exec -it cntf-nginx1 bash
root@a6151efb502f:/# ls
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr
```
基于容器创建镜像
```
方法一
首先进入某个容器进行一序列操作,然后退出,执行如下命令
docker commit -m "vim-net-tools" -a "cntf" 9e91890fbdba cntf1
-m 指容器里面装了哪些东西,这里是描述
-a 作者的信息
cntf1 这是创建新的镜像自定义的一个名称
根据镜像生成容器,然后进入
docker run -it --name centos-nettools cntf1 /bin/bash
方法二
导出,名称centos-nettools.tar
docker export 6b948eac3524 > centos-nettools.tar
导入,名称centosnettools1015
cat centos-nettools.tar | docker import centos-nettools.tar centosnettools1015
查看是否成功
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centosnettools1015 latest 7169ca747102 3 minutes ago 308MB
cntf1 v1 77b860caf81f 19 minutes ago 331MB
cntf1 latest 5b06cb7fa3db 26 minutes ago 331MB
ubuntu-mini-12.04 latest 37bd25f26b7e 2 weeks ago 322MB
nginx latest f949e7d76d63 2 weeks ago 126MB
ubuntu latest 2ca708c1c9cc 3 weeks ago 64.2MB
centos latest 67fa590cfc1c 7 weeks ago 202MB
hello-world latest fce289e99eb9 9 months ago 1.84kB
opensuse/amd64 latest c94c4a5730aa 20 months ago 108MB
[root@localhost ~]#
进入docker
docker run -it --name centosnettls centosnettools1015 bash
```