docker 查看运行容器
docker ps 查看当前docker容器运行的应用,是不是发现跟 linux 的命令一样。
ps 英文全拼:process status
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2752d340876c 7614ae9453d1 "docker-entrypoint..." 9 seconds ago Up 8 seconds 6379/tcp inspiring_allen
[root@localhost ~]#
先介绍下各个字段。
| CONTAINER ID | IMAGE | COMMAND | CREATED | STATUS | PORTS | NAMES |
|---|---|---|---|---|---|---|
| 容器ID | 使用的镜像 | 启动容器后运行的命令 | 容器的创建时间 | 容器状态 | 运行端口 | 容器名字 |
可以看到有个status字段,是不是发现容器也是有状态的。我们可以当前java线程来看、线程有各种状态,那么容器也会有各种状态。
- created 已创建
- restarting 重启中
- runing 运行中
- removing 迁移中
- paused 暂停
- exited 停止
- dead 死亡
再使用帮助命令看下该命令的格式和对应的操作。
[root@localhost ~]# docker ps --help
Usage: docker ps [OPTIONS]
List containers
Options:
-a, --all Show all containers (default shows just running)
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print containers using a Go template
--help Print usage
-n, --last int Show n last created containers (includes all states) (default -1)
-l, --latest Show the latest created container (includes all states)
--no-trunc Don't truncate output
-q, --quiet Only display numeric IDs
-s, --size Display total file sizes
[root@localhost ~]#
-a: 列出当前所有正在运行的容器+历史上运行过的容器。-f:根据条件过滤内容-n:显示最近创建的n个容器-l:显示出最近创建的容器-q:静默模式,只展示容器的编号 (后续会用到)--no-trunc: 不截断输出
这些参数是可以一起用,不是只可以单独用。
[root@localhost ~]# docker ps -a -n1 --no-trunc
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d67dd1a3b3d5a5438c88745314ce8768b6c6aa689eaab4615bf58e16fab7fb1f 7614ae9453d1 "docker-entrypoint.sh redis-server" 4 minutes ago Up 4 minutes 6379/tcp thirsty_bhabha
[root@localhost ~]#
docker 容器运行
- 运行容器 :
docker run容器ID 或者 容器名称 - 启动容器 :
docker start容器ID 或者 容器名称 - 重启容器 :
docker restart容器ID 或者 容器名称 - 停止容器 :
docker stop容器ID 或者 容器名称 - 强制停止容器 :
docker kill容器ID 或者 容器名称
这些命令都是可以帮助命令别忘了。千万别忘了强大的帮助命令。
[root@localhost ~]# docker start --help
Usage: docker start [OPTIONS] CONTAINER [CONTAINER...]
Start one or more stopped containers
Options:
-a, --attach Attach STDOUT/STDERR and forward signals
--detach-keys string Override the key sequence for detaching a container
--help Print usage
-i, --interactive Attach container's STDIN
docker run --help 这个命令可以仔细看下参数很多,很很重要。我写几个。
-d:以后台模式运行容器,即在容器启动后将其放入后台运行。-it:以交互模式运行容器,即启动一个新的终端会话并连接到正在运行的容器。--name:为容器指定一个名称。-p:将容器端口映射到主机端口。-v:将主机目录挂载或文件挂载到容器中。
docker 移除容器
docker rm --help查看移除命令
[root@localhost ~]# docker rm --help
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
Options:
-f, --force Force the removal of a running container (uses SIGKILL)
--help Print usage
-l, --link Remove the specified link
-v, --volumes Remove the volumes associated with the container
docker rm容器ID :根据容器ID移除,运行中的不能移除。docker rm -f容器ID : 强制删除移除,运行中的也能移除。docker rm $(docker ps -aq)删除所有容器,上文说到-q命令这里用到上了。