• ps
  • kill
  • rm
  • rmi

    ps

    • 用法

      Usage: docker ps [OPTIONS]

      List containers

      1. -a, --all=false Show all containers (default shows just running)
      2. --before= Show only container created before Id or Name
      3. -f, --filter=[] Filter output based on conditions provided
      4. --help=false Print usage
      5. -l, --latest=false Show the latest created container, include non-running
      6. -n=-1 Show n last created containers, include non-running
      7. --no-trunc=false Don't truncate output
      8. -q, --quiet=false Only display numeric IDs
      9. -s, --size=false Display total file sizes
      10. --since= Show created since Id or Name, include non-running
    • 例子
    1. $ sudo docker ps -a
    2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    3. b448f729a0b0 centos "/bin/bash" 4 days ago Exited (137) 4 days ago pensive_wilson
    4. 54c7b6d6632e centos "/bin/bash" 4 days ago Exited (0) 3 days ago adoring_wozniak

    利用筛选器筛选出exied状态时0的容器:

    1. $ sudo docker ps -a --filter 'exited=0'
    2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    3. 8d92293a65e9 registry.liugang/centos "/bin/bash" 7 days ago Exited (0) 5 days ago web
    4. 8410f389ea65 registry.liugang/centos "/bin/bash" 7 days ago Exited (0) 7 days ago test_link
    • 总结

    -a参数列出所有状态的容器, -l列出最新创建的容器,包括停止运行状态的容器。

    kill

    • 用法
    1. Usage: docker kill [OPTIONS] CONTAINER [CONTAINER...]
    2. Kill a running container using SIGKILL or a specified signal
    3. --help=false Print usage
    4. -s, --signal=KILL Signal to send to the container
    • 例子
    1. $ sudo docker kill pensive_wilson
    2. pensive_wilson

    这将停止该容器

    • 总结

    结合ps命令,可以做到kill所有正在运行的容器:

    1. $ sudo docker kill $(sudo docker ps -a -q)

    rm

    • 用法
    1. Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
    2. Remove one or more containers
    3. -f, --force=false Force the removal of a running container (uses SIGKILL)
    4. --help=false Print usage
    5. -l, --link=false Remove the specified link
    6. -v, --volumes=false Remove the volumes associated with the container
    • 例子
    1. $ sudo docker rm pensive_wilson
    2. pensive_wilson

    这将删除一个已经停止运行的容器,若容器正在运行,则将会使docker报错,停止容器再删除,或者加上-f参数强制删除(不建议)。

    • 总结

    类似的我们也结合ps删除所有容器:

    1. $ sudo docker kill $(sudo docker ps -a -q)
    2. ...
    3. ...
    4. ...
    5. $ sudo docker rm $(sudo docker ps -a -q)
    6. ...
    7. ...
    8. ...

    要清空容器,首先要保证没有容器在运行。

    rmi

    • 用法
    1. Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]
    2. Remove one or more images
    3. -f, --force=false Force removal of the image
    4. --help=false Print usage
    5. --no-prune=false Do not delete untagged parents
    • 例子
    1. $ sudo docker rmi centos:6.5
    2. ...
    3. ...
    4. ...
    • 总结

    要区分rm于rmi多用法。
    与docker images命令配合来清空镜像:

    1. $ sudo docker rmi $(sudo docker images -a -q)