docker常用命令整理汇总(新手必备!)
作者:yufei_java
这篇文章主要给大家介绍了关于docker常用命令的相关资料,包括docker的启动、停止、重启、创建、查看等等,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
具体docker的优势我就不说了,下面就使用docker的一些常用命令具体详细说下:
1、docker的启动、停止、重启
[root@localhost ~]# service docker restart Redirecting to /bin/systemctl restart docker.service [root@localhost ~]# service docker stop Redirecting to /bin/systemctl stop docker.service [root@localhost ~]# service docker start Redirecting to /bin/systemctl start docker.service
2、docker创建一个容器
[root@localhost ~]# docker run -it -v /docker_test:/yufei --name yufei_6 centos [root@724e7701f0d4 /]#
- -i:允许我们对容器内的 (STDIN) 进行交互
- -t: 在新容器内指定一个伪终端或终端
- -v:是挂在宿机目录, /docker_test是宿机目录,/yufei是当前docker容器的目录,宿机目录必须是绝对的。
- --name:是给容器起一个名字,可省略,省略的话docker会随机产生一个名字
3、docker启动的容器列表
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 724e7701f0d4 centos "/bin/bash" 4 minutes ago Up 4 minutes yufei_6 f9097691663e centos "/bin/bash" 5 minutes ago Up 5 minutes yufei_5 [root@localhost ~]#
3、查看docker创建的所有容器
[root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 724e7701f0d4 centos "/bin/bash" 5 minutes ago Up 5 minutes yufei_6 f9097691663e centos "/bin/bash" 6 minutes ago Up 6 minutes yufei_5 e59a540fb979 centos "/bin/base" 6 minutes ago Created yufei_4 ff49dfedea4f centos "/bin/bash" 2 hours ago Exited (137) 10 minutes ago yufei_03 d2cc70abb5a5 centos "/bin/bash" 2 hours ago Exited (127) 2 hours ago yufei_02 2d48fc5b7c17 centos "/bin/bash" 2 hours ago Exited (127) 2 hours ago yufei_01 [root@localhost ~]#
docker ps 默认列表是正在启动的容器 -a是显示所有创建的容器
4、启动、停止、重启某个docker 容器
[root@localhost ~]# docker start yufei_01 yufei_01 [root@localhost ~]# docker stop yufei_01 yufei_01 [root@localhost ~]# docker restart yufei_01 yufei_01 [root@localhost ~]#
5、查看指定容器的日志记录
<span style="color:#333333;">[root@localhost ~]# docker logs -f yufei_01 </span><span style="background-color: rgb(51, 204, 255);"><span style="color:#ff0000;">[root@2d48fc5b7c17 /]# ls anaconda-post.log bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@2d48fc5b7c17 /]# exit exit [root@2d48fc5b7c17 /]# ls anaconda-post.log bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@2d48fc5b7c17 /]# [root@2d48fc5b7c17 /]# [root@2d48fc5b7c17 /]# [root@2d48fc5b7c17 /]# [root@2d48fc5b7c17 /]# [root@2d48fc5b7c17 /]# [root@2d48fc5b7c17 /]# [root@2d48fc5b7c17 /]# cd / [root@2d48fc5b7c17 /]# ls anaconda-post.log bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@2d48fc5b7c17 /]# mkdir yufei [root@2d48fc5b7c17 /]# ls anaconda-post.log bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var yufei [root@2d48fc5b7c17 /]# cd yufei [root@2d48fc5b7c17 yufei]# ls yufei [root@2d48fc5b7c17 yufei]# cd yufei [root@2d48fc5b7c17 yufei]# ls application [root@2d48fc5b7c17 yufei]# cd ../../ [root@2d48fc5b7c17 /]# rm -rf yufei [root@2d48fc5b7c17 /]# eixt bash: eixt: command not found [root@2d48fc5b7c17 /]# exit exit</span></span><span style="color:#333333;"> [root@2d48fc5b7c17 /]# </span>
上面红色部分是日志命令部分。
6、删除某个容器,若正在运行,需要先停止
[root@localhost ~]# docker rm yufei_01 Error response from daemon: You cannot remove a running container 2d48fc5b7c17b01e6247cbc012013306faf1e54f24651d5e16d6db4e15f92d33. Stop the container before attempting removal or use -f [root@localhost ~]# docker stop yufei_01 yufei_01 [root@localhost ~]# docker rm yufei_01 yufei_01 [root@localhost ~]#
7、删除所有容器
[root@localhost ~]# docker rm $(docker ps -a -q) Error response from daemon: You cannot remove a running container 724e7701f0d4a830167e21f75b470235a0e408fd6cc2913403426e96f69cba11. Stop the container before attempting removal or use -f Error response from daemon: You cannot remove a running container f9097691663ee36f9d2ee56afbdcca0eeb8b63e5590ddf18c0c42954c93b9f06. Stop the container before attempting removal or use -f [root@localhost ~]# [root@localhost ~]# [root@localhost ~]# docker stop yufei_6 yufei_6 [root@localhost ~]# docker stop yufei_5 yufei_5 [root@localhost ~]# docker rm $(docker ps -a -q) 724e7701f0d4 f9097691663e [root@localhost ~]#
附:Docker 如何保存对容器的修改
1、docker ps 查看正在运行的容器.
2、docker exec –it 3bd0eef03413 bash
进入正在运行的容器内
3、进入容器后,就可以修改镜像了,比如修改镜像中已经部署的代码或者安装新的软件或包等,修改完成之后,exit 退出容器
4、docker commit 3bd0eef03413 demo:v1.3
提交你刚才修改的镜像,新的镜像名称为demo,版本为v1.3
总结
到此这篇关于docker常用命令整理汇总的文章就介绍到这了,更多相关docker常用命令内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!