Docker 常用命令——容器
Posted plluoye
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker 常用命令——容器相关的知识,希望对你有一定的参考价值。
1.新建并启动容器
docker run [option] images [command][arg] #根据镜像新建容器并运行。如果本地没有镜像则从docker hub上拉取。
--name 为容器指定名称。
-d 后台运行容器,并返回容器id
-i 交互模式运行容器,通常与 -t同时用
-t 为容器分配伪输入终端。
-P 随机端口映射
-p 指定端口映射,有以下四种格式
ip:hostport:ContainerPort
ip::containerPort
HostPort:ContainerPort
ContainerPort
docker run centos #创建/运行/退出容器
docker run -t centos #创建容器,并进入伪终端,但不能操作。ctrl+c 退出,容器不退出。
docker run -it centos
docker run -it --name mycentos centos
docker run -d centos #以后台进程方式创建容器并运行,但因为没有可交互的前台进程,所以又被立刻终止。故docker ps 看不到。
[[email protected] ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE redis latest a55fbf438dfd 8 days ago 95MB mysql latest 7bb2586065cd 8 days ago 477MB nginx latest 2bcb04bdb83f 8 days ago 109MB centos latest 9f38484d220f 2 weeks ago 202MB hello-world latest fce289e99eb9 3 months ago 1.84kB docker run -it 9f38484d220f #启动交互式容器 [[email protected] /]# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 02:23 pts/0 00:00:00 /bin/bash root 16 1 0 02:23 pts/0 00:00:00 ps -ef [[email protected] /]# exit exit [[email protected] ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [[email protected] ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8e0ffee781f1 9f38484d220f "/bin/bash" 4 minutes ago Exited (0) 3 minutes ago pensive_lumiere 3af63c5c69c6 hello-world "/hello" 42 hours ago Exited (0) 42 hours ago pensive_dubinsky [[email protected] ~]# docker rename 8e0ffee781f1 mycentos [[email protected] ~]# docker start mycentos mycentos [[email protected] ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8e0ffee781f1 9f38484d220f "/bin/bash" 40 minutes ago Up 6 seconds mycentos [[email protected] ~]# docker stats mycentos CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS 8e0ffee781f1 mycentos 0.00% 380KiB / 991.2MiB 0.04% 656B / 0B 0B / 0B 1 [[email protected] ~]# docker top mycentos UID PID PPID C STIME TTY TIME CMD root 4675 4659 0 10:44 pts/0 00:00:00 /bin/bash [[email protected] ~]# docker attach mycentos 不关容器,安全退出: ctrl +p ctrl+q [[email protected] ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8e0ffee781f1 9f38484d220f "/bin/bash" 3 hours ago Up 2 hours mycentos [[email protected] ~]# docker images centos REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 9f38484d220f 2 weeks ago 202MB [[email protected] ~]# docker run -it --name mycentos01 centos #重新打开一个容器,并命名为mycentos01 [[email protected] ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ed1315df6320 centos "/bin/bash" About a minute ago Up About a minute mycentos01 8e0ffee781f1 9f38484d220f "/bin/bash" 3 hours ago Up 2 hours mycentos
2.列出容器
docker ps -a 显示当前或曾经打开的容器
docker ps -l 显示上一次运行的容器
docker ps -n 3 显示上3次运行的容器
docker ps -ql 只显示上一次运行的容器ID
3.退出容器
exit #退出并关闭容器
ctrl+p,crl+q #退出但不关闭容器
4.停止容器
docker stop 容器 ID或name
docker stop -t n 容器 ID或name # Seconds to wait for stop before killing it (default 10) 杀掉容器前等待多少秒(默认10s)
直接关闭
docker kill 容器 ID或name
[[email protected] ~]# docker stop mycentos mycentos01 #同时停止多个容器 mycentos mycentos01 [[email protected] ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4.启动容器
docker start 容器 ID或name
docker restart 容器 ID或name
[[email protected] ~]# docker start mycentos mycentos01 mycentos mycentos01 [[email protected] ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ed1315df6320 centos "/bin/bash" 9 minutes ago Up 36 seconds mycentos01 8e0ffee781f1 9f38484d220f "/bin/bash" 3 hours ago Up 36 seconds mycentos
5.重启容器
docker restart 容器 ID或name
6.删除停止运行的容器
docker rm 容器 ID或name
docker rm -f 强制删除容器,即使在运行
docker rm -f $(docker ps -qa) # 删除全部容器
docker ps -qa | xargs docker rm # 删除全部已停止容器
docker rmi 删除镜像
[[email protected] ~]# docker ps -n2 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ed1315df6320 centos "/bin/bash" 23 minutes ago Exited (137) 7 minutes ago mycentos01 8e0ffee781f1 9f38484d220f "/bin/bash" 3 hours ago Exited (137) 8 minutes ago mycentos [[email protected] ~]# docker rm mycentos01 mycentos01 [[email protected] ~]# docker ps -n2 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8e0ffee781f1 9f38484d220f "/bin/bash" 3 hours ago Exited (137) 9 minutes ago mycentos 3af63c5c69c6 hello-world "/hello" 45 hours ago Exited (0) 45 hours ago pensive_dubinsky
7.查看所有容器的实时资源占用情况
docker stats
8.查看容器日志
docker logs -f -t --tail 10 容器 ID或name
-f 持续刷新最新日志
-t 显示时间戳
--tail 显示最后几行
[[email protected] ~]# docker run -d --name mycentos1 centos ba18b348bd9857bde9b25a62d848d6239c1c7c0de302a8bc279185a758efd861 [[email protected] ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [[email protected] ~]# docker run -d --name mycentos2 centos /bin/bash -c "while true;do echo Hello World;sleep 3;done" 21494ef841e27c69877afaf1a0be5ae2bb7d32d6862eb7bfa1e6eb237f04efa3 [[email protected] ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 21494ef841e2 centos "/bin/bash -c ‘while…" 2 seconds ago Up 2 seconds mycentos2 [[email protected] ~]# docker logs -f -t --tail 5 21494ef841e2 2019-04-04T07:37:43.721260403Z Hello World 2019-04-04T07:37:46.721904929Z Hello World 2019-04-04T07:37:49.723204790Z Hello World 2019-04-04T07:37:52.724042843Z Hello World 2019-04-04T07:37:55.725116649Z Hello World ...
9.查看容器内进程
docker top 容器 ID或name
[[email protected] ~]# docker run -d centos /bin/bash -c "while true;do echo Nihao;sleep 2 ;done" 4768a70886fcb74cdf00cb19d142fb503a18d2390406dc31ad533d697b423f46 [[email protected] ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4768a70886fc centos "/bin/bash -c ‘while…" 3 seconds ago Up 2 seconds affectionate_cori [[email protected] ~]# docker top 4768a70886fc UID PID PPID C STIME TTY TIME CMD root 9379 9364 0 15:48 ? 00:00:00 /bin/bash -c while true;do echo Nihao;sleep 2 ;done root 9427 9379 0 15:48 ? 00:00:00 sleep 2
10.查看容器细节
docker inspect 容器 ID或name
[[email protected] ~]# docker inspect 4768a70886fc [ { "Id": "4768a70886fcb74cdf00cb19d142fb503a18d2390406dc31ad533d697b423f46", "Created": "2019-04-04T07:48:20.460764642Z", "Path": "/bin/bash", "Args": [ "-c", "while true;do echo Nihao;sleep 2 ;done" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, ....
11.进入容器
docker attach 容器 ID或name
docker exec -t -w -u -e 容器 ID或name 命令 # 功能更强大,也能实现登陆容器内部
-d, --detach Detached mode: run command in the background
-e, --env list Set environment variables
-i, --interactive Keep STDIN open even if not attached
-t, --tty Allocate a pseudo-TTY
-u, --user string Username or UID (format: <name|uid>[:<group|gid>])
-w, --workdir string Working directory inside the container
[[email protected] ~]# docker exec -t -w /tmp -uroot -e LANG=en 4768a70886fc ls ks-script-eC059Y yum.log [[email protected] ~]# docker exec 4768a70886fc ls /tmp ks-script-eC059Y yum.log [[email protected] ~]# docker exec -it 4768a70886fc ls /tmp ks-script-eC059Y yum.log [[email protected] ~]# docker exec -it 4768a70886fc /bin/bash [[email protected] /]# ls anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
12. docker cp
Copy files/folders between a container and the local filesystem 在本地系统和容器间copy文件或文件夹
[[email protected] ~]# docker cp anaconda-ks.cfg 4768a70886fc:/home [[email protected] ~]# docker exec 4768a70886fc ls /home anaconda-ks.cfg [[email protected] ~]# docker exec -it 4768a70886fc ls /tmp ks-script-eC059Y yum.log [[email protected] ~]# docker cp 4768a70886fc:/tmp/yum.log /tmp [[email protected] ~]# ls -l /tmp 总用量 8 -rw-------. 1 root root 0 3月 6 01:34 yum.log
docker run -d /bin/bash -c 的注意点
重启docker 后,会继续执行创建docker 时指定的脚本。
相当于该容器运行的是 镜像+shell 的组合模板。
docker attach 登陆容器后,如果command 命令是前台进程就会一直执行,而不能做其他操作。
以上是关于Docker 常用命令——容器的主要内容,如果未能解决你的问题,请参考以下文章