• 容器(四)容器常用操作【20】


    (四)容器常用操作

    (1)stop/start/restart 容器

    root@cuiyongchao:/dockerfile# docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
    872ebb8a83c1        ubuntu              "/bin/bash -c 'while…"   About an hour ago   Up About an hour                             hardcore_kepler
    root@cuiyongchao:/dockerfile# docker stop 872ebb8a83c1
    872ebb8a83c1
    root@cuiyongchao:/dockerfile# docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                           PORTS                    NAMES
    872ebb8a83c1        ubuntu              "/bin/bash -c 'while…"   About an hour ago   Exited (137) 5 seconds ago                                hardcore_kepler
    
    

    ​ 容器在 docker host 中实际上是一个进程,docker stop 命令本质上是向该进程发送一个 SIGTERM 信号。如果想快速停止容器,可使用 docker kill 命令,其作用是向容器进程发送 SIGKILL 信号。

    root@cuiyongchao:/dockerfile# docker kill 638740d1042a
    638740d1042a
    root@cuiyongchao:/dockerfile# docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                           PORTS                    NAMES
    638740d1042a        httpd               "httpd-foreground"       3 hours ago         Exited (137) 4 seconds ago                                my_http_server
    root@cuiyongchao:/dockerfile# docker start 638740d1042a
    638740d1042a
    root@cuiyongchao:/dockerfile# docker ps 
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
    638740d1042a        httpd               "httpd-foreground"       3 hours ago         Up 3 seconds        80/tcp                   my_http_server
    
    

    docker start 会保留容器的第一次启动时的所有参数。

    docker restart 可以重启容器,其作用就是依次执行 docker stopdocker start

    ​ 容器可能会因某种错误而停止运行。对于服务类容器,我们通常希望在这种情况下容器能够自动重启。启动容器时设置 --restart 就可以达到这个效果。

    root@cuiyongchao:/dockerfile# docker run -d --restart=always httpd
    5830e72ffb636e9c6ebe27ee9b302468144ecf83d4f9d6acca3dec280fb357a6
    root@cuiyongchao:/dockerfile#
    
    

    --restart=always 意味着无论容器因何种原因退出(包括正常退出),就立即重启。该参数的形式还可以是 --restart=on-failure:3,意思是如果启动进程退出代码非0,则重启容器,最多重启3次。

    (2)pause/unpause 容器

    ​ 有时我们只是希望暂时让容器暂停工作一段时间,比如要对容器的文件系统打个快照,或者 dcoker host 需要使用 CPU,这时可以执行 docker pause

    root@cuiyongchao:/dockerfile# docker pause 5830e72ffb63
    5830e72ffb63
    root@cuiyongchao:/dockerfile# docker ps 5830e72ffb63
    "docker ps" accepts no arguments.
    See 'docker ps --help'.
    
    Usage:  docker ps [OPTIONS]
    
    List containers
    root@cuiyongchao:/dockerfile# docker ps 
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                  PORTS                    NAMES
    5830e72ffb63        httpd               "httpd-foreground"       3 minutes ago       Up 3 minutes (Paused)   80/tcp                   hardcore_sanderson
    root@cuiyongchao:/dockerfile# docker unpause 5830e72ffb63
    5830e72ffb63
    root@cuiyongchao:/dockerfile# docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
    5830e72ffb63        httpd               "httpd-foreground"       5 minutes ago       Up 5 minutes        80/tcp                   hardcore_sanderson
    
    

    ​ 处于暂停状态的容器不会占用 CPU 资源,直到通过 docker unpause 恢复运行。

    (3)删除容器

    使用 docker 一段时间后,host 上可能会有大量已经退出了的容器。

    这些容器依然会占用 host 的文件系统资源,如果确认不会再重启此类容器,可以通过 docker rm 删除。

    root@cuiyongchao:/dockerfile# docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS                    NAMES
    37234805f5e2        busybox             "sh"                     19 minutes ago      Exited (127) 14 minutes ago                            nifty_borg
    872ebb8a83c1        ubuntu              "/bin/bash -c 'while…"   2 hours ago         Exited (137) 13 minutes ago                            hardcore_kepler
    d3d17f299cfc        ubuntu              "/bin/bash -c 'while…"   2 hours ago         Exited (137) 2 hours ago                               angry_jepsen
    1d48fd6d1a40        ubuntu              "/bin/bash -c 'while…"   3 hours ago         Exited (137) 3 hours ago                               priceless_lovelace
    
    root@cuiyongchao:/dockerfile# docker rm 37234805f5e2 872ebb8a83c1 d3d17f299cfc
    37234805f5e2
    872ebb8a83c1
    d3d17f299cfc
    root@cuiyongchao:/dockerfile# 
    
    

    docker rm 一次可以指定多个容器,如果希望批量删除所有已经退出的容器,可以执行如下命令:

    docker rm -v $(docker ps -aq -f status=exited)

    root@cuiyongchao:/dockerfile# docker rm $(docker ps -aq -f status=exited)
    1d48fd6d1a40
    fe757aa05f42
    8e8f2f1fff93
    be1ba7d18a9b
    e0899ffc4801
    97f9b6d4c13a
    root@cuiyongchao:/dockerfile# docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
    5830e72ffb63        httpd               "httpd-foreground"       10 minutes ago      Up 10 minutes       80/tcp                   hardcore_sanderson
    638740d1042a        httpd               "httpd-foreground"       3 hours ago         Up 13 minutes       80/tcp     
    
    

    顺便说一句:docker rm 是删除容器,而 docker rmi 是删除镜像。

  • 相关阅读:
    codeforces 336D Vasily the Bear and Beautiful Strings(组合数学)
    13年山东省赛 The number of steps(概率dp水题)
    13年山东省赛 Mountain Subsequences(dp)
    13年山东省赛 Boring Counting(离线树状数组or主席树+二分or划分树+二分)
    codeforces 337C Quiz(贪心)
    codeforces 336C Vasily the Bear and Sequence(贪心)
    codeforces 335A Banana(贪心)
    codeforces 339C Xenia and Bit Operations(线段树水题)
    codeforces 339C Xenia and Weights(dp或暴搜)
    codeforces 340E Iahub and Permutations(错排or容斥)
  • 原文地址:https://www.cnblogs.com/cuiyongchao007/p/13960098.html
Copyright © 2020-2023  润新知