1.创建容器
1.前台运行创建
注意:下面是创建容器并进入容器,当exit退出容器后,容器就不在运行,需要使用-d选项,后台运行
1. [root@localhost ~]# docker run -i -t centos:7 /bin/bash 2. [root@4af91bbbc860 /]# ps -ef 3. UID PID PPID C STIME TTY TIME CMD 4. root 1 0 0 03:19 pts/0 00:00:00 /bin/bash 5. root 15 1 0 03:19 pts/0 00:00:00 ps -ef 6. [root@4af91bbbc860 /]# exit 7. exit 8. [root@localhost ~]# docker ps -a 9. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 10. 4af91bbbc860 centos:7 "/bin/bash" 7 minutes ago Exited (0) 8 seconds ago naughty_meitner 11. 12. 选项: 13. -name:指定容器名称,默认是随机名字 14. -i:创建一个交互式容器 15. -t:为容器分配一个伪终端,绑定到容器的标准输入上 16. -d:后台运行容器
2.后台运行创建
1. [root@localhost ~]# docker run -itd --name centos7-1 centos:7 /bin/bash 2. 08b6e56e54f2d645601cd71020a922c7b5f916725401b7408e358a782a965284 3. [root@localhost ~]# docker ps -a 4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5. 08b6e56e54f2 centos:7 "/bin/bash" 2 seconds ago Up 2 seconds centos7-1 6. 4af91bbbc860 centos:7 "/bin/bash" 8 minutes ago Exited (0) About a minute ago naughty_meitner 7. 8. 选项: 9. -name:指定容器名称,默认是随机名字 10. -i:创建一个交互式容器 11. -t:为容器分配一个伪终端,绑定到容器的标准输入上 12. -d:后台运行容器
2.查看容器
1. [root@localhost ~]# docker ps --help 2. 3. Usage: docker ps [OPTIONS] 4. 5. List containers 6. 7. Aliases: 8. docker container ls, docker container list, docker container ps, docker ps 9. 10. Options: 11. -a, --all Show all containers (default shows just running) #查看所有容器 默认之查看在运行的容器 12. -f, --filter filter Filter output based on conditions provided 13. --format string Format output using a custom template: 14. 'table': Print output in table format 15. with column headers (default) 16. 'table TEMPLATE': Print output in table format 17. using the given Go template 18. 'json': Print in JSON format 19. 'TEMPLATE': Print output using the given Go 20. template. 21. Refer to https://docs.docker.com/go/formatting/ for 22. more information about formatting output with templates 23. -n, --last int Show n last created containers (includes all #查看多少行容器 24. states) (default -1) 25. -l, --latest Show the latest created container (includes all states) #查看最近创建的容器 26. --no-trunc Don't truncate output #显示完整ID号 27. -q, --quiet Only display container IDs #只显示ID号 28. -s, --size Display total file sizes #显示容器大小
1.查看所有的容器
1. [root@localhost ~]# docker ps -a 2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3. 08b6e56e54f2 centos:7 "/bin/bash" 27 minutes ago Up 27 minutes centos7-1 4. 4af91bbbc860 centos:7 "/bin/bash" 35 minutes ago Exited (0) 28 minutes ago naughty_meitner
2.查看容器的ID
注:可以用于删除容器时,过滤
1. [root@localhost ~]# docker ps -q 2. 08b6e56e54f2
3.管理容器
1.开启容器
1. [root@localhost ~]# docker start 4af91bbbc860 2. 4af91bbbc860 3. [root@localhost ~]# docker ps -a 4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5. 08b6e56e54f2 centos:7 "/bin/bash" 30 minutes ago Up 30 minutes centos7-1 6. 4af91bbbc860 centos:7 "/bin/bash" 38 minutes ago Up 1 second naughty_meitner
2.关闭容器
1. [root@localhost ~]# docker stop 08b6e56e54f2 2. 08b6e56e54f2 3. [root@localhost ~]# docker ps -a 4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5. 08b6e56e54f2 centos:7 "/bin/bash" 30 minutes ago Exited (137) 10 seconds ago centos7-1 6. 4af91bbbc860 centos:7 "/bin/bash" 39 minutes ago Up 53 seconds naughty_meitner
3.强制关闭容器
1. [root@localhost ~]# docker kill 4af91bbbc860 2. 4af91bbbc860 3. [root@localhost ~]# docker ps -a 4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5. 08b6e56e54f2 centos:7 "/bin/bash" 31 minutes ago Exited (137) 51 seconds ago centos7-1 6. 4af91bbbc860 centos:7 "/bin/bash" 39 minutes ago Exited (137) 1 second ago naughty_meitner
4.重启容器
1. [root@localhost ~]# docker restart 4af91bbbc860 2. 4af91bbbc860 3. [root@localhost ~]# docker ps -a 4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5. 08b6e56e54f2 centos:7 "/bin/bash" 32 minutes ago Exited (137) About a minute ago centos7-1 6. 4af91bbbc860 centos:7 "/bin/bash" 40 minutes ago Up 7 seconds naughty_meitner
4.查看容器信息
1.查看所有信息
[root@localhost ~]# docker inspect 4af91bbbc860
2.查看容器IP
1. [root@localhost ~]# docker inspect -f '{{.NetworkSettings.IPAddress}}' 4af91bbbc860 2. 172.17.0.2
3.查看PID
1. [root@localhost ~]# docker inspect -f '{{.State.Pid}}' 4af91bbbc860 2. 72633
5.进入容器
注意:进入当前的伪终端,如果exit退出之后,当前的容器也会随之关闭,因为当前只存在一个终端。
1.进入当前的伪终端中
1. [root@localhost ~]# docker attach 4af91bbbc860 2. [root@4af91bbbc860 /]# exit 3. exit 4. [root@localhost ~]# docker ps -a 5. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6. 08b6e56e54f2 centos:7 "/bin/bash" 42 minutes ago Exited (137) 11 minutes ag o centos7-1 7. 4af91bbbc860 centos:7 "/bin/bash" 50 minutes ago Exited (0) 5 seconds ago naughty_meitner
2.创建一个新的伪终端并进入
1. [root@localhost ~]# docker exec -it 4af91bbbc860 /bin/bash 2. [root@4af91bbbc860 /]# exit 3. exit 4. [root@localhost ~]# docker ps -a 5. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6. 08b6e56e54f2 centos:7 "/bin/bash" 43 minutes ago Exited (137) 13 minutes ago centos7-1 7. 4af91bbbc860 centos:7 "/bin/bash" 51 minutes ago Up 28 seconds naughty_meitner
6.导出\导入容器
1.导出容器
将当前容器导出到本地
1. [root@localhost ~]# docker export 4af91bbbc860 > centos7.tar 2. [root@localhost ~]# ls 3. anaconda-ks.cfg centos7.tar original-ks.cfg
2.导入容器
将当前容器导入为竟镜像,然后再基于这个镜像创建容器
1. [root@localhost ~]# cat centos7.tar | docker import - centos7:test 2. sha256:d0373e6a70dc1413fa992d11c40b9260fe742db22196429835d33b9cc3eb2135 3. [root@localhost ~]# docker images 4. REPOSITORY TAG IMAGE ID CREATED SIZE 5. centos7 test d0373e6a70dc 11 seconds ago 204MB
7.删除容器
1.删除单个容器
1. [root@localhost ~]# docker rm -f 4af91bbbc860 2. 3. 选项: 4. -f:强制删除
2.删除多个容器
[root@localhost ~]# docker rm -f 4af91bbbc860 08b6e56e54f2
3.删除全部容器
[root@localhost ~]# docker rm -f $(docker ps -aq)