2021Docker容器技术全解-容器管理的10个命令Docker(3)
1启动镜像:
格式:docker run -参数 镜像名称:镜像标签 启动命令
2进入容器:
格式:docker exec -it 容器id 启动命令
3查看容器:
格式:docker ps
4以上帝的方式进入容器:
格式:docker attach 容器id
5以正常的方式进入容器:
docker exec -it 容器id /bin/bash
6删除容器
docker rm 容器id
7停止容器
docker stop 容器id
8启动容器:
docker start 容器id
9重启容器
docker restart 容器id
10查看容器内的进程
docker top 容器id
11查看容器详细信息
docker inspect 容器id
1示例:
#查看镜像 [root@web01 ~]#docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f6987c8d6ed5 8 days ago 141MB centos latest 5d0da3dc9764 3 months ago 231MB #启动容器 [root@web01 ~]#docker run -itd centos:latest /bin/bash f68d32bf3b8869fefb45215dc3294efc020d2d715a09cec5619e2da8dd434a38 #查看容器 [root@web01 ~]#docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f68d32bf3b88 centos:latest "/bin/bash" 3 minutes ago Up 3 minutes naughty_lehmann #查看容器里都运行了哪些程序: [root@web01 ~]#docker top f68 #查看容器的详细信息: [root@web01 ~]# docker inspect f68 #进入容器 [root@web01 ~]#docker exec -it f68 /bin/bash [root@f68d32bf3b88 /]# #以上帝的方式进入容器: 格式:docker attach 容器id 作用:通过attach这个终端查看容器有没有报错信息,99%的情况进入容器都会用exec替代attach,因为这个attach是以上帝的方式在查看容器,操作不当就会导致容器死掉 案例说明: #以上帝的方式进入容器 [root@web01 ~]#docker attach f68 #查看运行的程序 [root@f68d32bf3b88 /]# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 09:28 pts/0 00:00:00 /bin/bash root 30 1 0 10:01 pts/0 00:00:00 ps -ef #输出进程号 [root@f68d32bf3b88 /]# echo $$ 1 #退出(注意此时容器会死掉) [root@f68d32bf3b88 /]# exit exit #查看容器是否还活着 [root@web01 ~]#docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES #启动死去的容器 [root@web01 ~]#docker start f68 f68 #再次进入容器并用快捷键终止终端 [root@web01 ~]#docker attach f68 [root@ [root@f68d32bf3b88 /]# [root@f68d32bf3b88 /]# ctrl+p+q终止终端就不会直接把容器给弄死 #再次查看容器是否还活着 [root@web01 ~]#docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f68d32bf3b88 centos:latest "/bin/bash" 41 minutes ago Up 6 minutes naughty_lehmann [root@web01 ~]# #批量删除容器:
[root@web01 ~]#docker rm $(docker ps -aq)
d444b06bc723
4e46244c234b
b776524015b8
8678f866af9a
9e366dfd0ded
b2aa44ce8578
127b5c8b8870
Error response from daemon: You cannot remove a running container ffd48b47fd213c40b4ea457ef3a17fbf71e3d1b11fd5ecbe4839b6ac8766f2bb. Stop the container before attempting removal or force remove
Error response from daemon: You cannot remove a running container bf907b0a073aa0aa5f5ea1b571c1c9b8386eb231f8f37a2ed45d08c8eca827cf. Stop the container before attempting removal or force remove
[root@web01 ~]#
#停止容器
[root@web01 ~]#docker stop 63b
63b
#删除容器
[root@web01 ~]#docker rm 63b
63b
[root@web01 ~]#
2对于类似于nginx这种无法进行交互的程序跑docker时,请不要用交互程序/bin/bash启动,否则外网无法访问,用gdocker部署nginx示例如下:
[root@web01 ~]#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest 7614ae9453d1 7 days ago 113MB
nginx latest f6987c8d6ed5 8 days ago 141MB
busybox latest ffe9d497c324 3 weeks ago 1.24MB
ubuntu latest ba6acccedd29 2 months ago 72.8MB
centos latest 5d0da3dc9764 3 months ago 231MB
qq 66907360 5d0da3dc9764 3 months ago 231MB
wechat tyjs09 5d0da3dc9764 3 months ago 231MB
[root@web01 ~]#docker run --name nginx-test -p 8081:80 -d nginx
a77e256b8cd0d92ceee02903c358652b3adfb9a0f1e4d7175910af25c401ccc3
[root@web01 ~]#docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a77e256b8cd0 nginx "/docker-entrypoint.…" 8 seconds ago Up 7 seconds 0.0.0.0:8081->80/tcp, :::8081->80/tcp nginx-test
[root@web01 ~]#curl localhost:8081
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@web01 ~]#

浙公网安备 33010602011771号