• Docker系列-3.Docker生命周期、基础操作


    Docker系列-3.Docker生命周期、基础操作

    Docker容器生命周期

    1. docker create触发create事件,容器进入stopped状态;

    2. docker rm触发destory事件使容器完成从stopped->deleted状态迁移;

    3. docker start 触发start事件使容器完成从stopped->running状态;

    4. docker run触发create事件经过stopped,触发start事件后进入running状态

    5. docker kill 使容器完成从running->stopped状态迁移。docker kill先后触发的die和kill事件,然后会kill掉当前容器中的进程,

    6. docker stop使容器完成从running->stopped状态迁移。docker stop先后触发的die和stop事件,并不会杀掉当前容器的进程。

    7. docker restart使容器完成从running->running状态迁移。先后触发了die、start、restart事件;

    8. docker paused使容器完成从running->paused状态迁移。触发了pause事件;

    9. docker unpause使容器完成从paused->running状态迁移。触发了unpause事件;

    容器操作

    1.查看当前所有容器列表

    [root@localhost ~]# docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                   PORTS               NAMES
    e40604d8e4cd        nginx               "/docker-entrypoint.…"   2 hours ago         Created                                      focused_kirch
    4a10d126524f        ubuntu              "bash"                   4 hours ago         Exited (0) 4 hours ago                       brave_goldwasser
    8fb045bce0da        hello-world         "/hello"                 4 hours ago         Exited (0) 4 hours ago                       flamboyant_lichterman
    

    2.创建容器

    如果本地没有容器的镜像文件,那么会先拉取一个容器的镜像文件

    [root@localhost ~]# docker create httpd
    Unable to find image 'httpd:latest' locally
    latest: Pulling from library/httpd
    6ec8c9369e08: Already exists 
    819d6e0b29e7: Pull complete 
    6a237d0d4aa4: Pull complete 
    cd9a987eec32: Pull complete 
    fdec8f3f8485: Pull complete 
    Digest: sha256:2a9ae199b5efc3e818cdb41c790638fc043ffe1aba6bc61ada28ab6356d044c6
    Status: Downloaded newer image for httpd:latest
    b0629eeb59a60b06bba619fa17dff66270cdbe85302920a7684a3683fe020e48
    
    [root@localhost ~]# docker create nginx
    e40604d8e4cd4d8fd2d91956146bfe5e00d9de6506c48ef9facc2f67d6420dc8
    
    [root@localhost ~]# docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                   PORTS               NAMES
    b0629eeb59a6        httpd               "httpd-foreground"       2 minutes ago       Created                                      priceless_haibt
    e40604d8e4cd        nginx               "/docker-entrypoint.…"   2 hours ago         Created                                      focused_kirch
    4a10d126524f        ubuntu              "bash"                   5 hours ago         Exited (0) 5 hours ago                       brave_goldwasser
    8fb045bce0da        hello-world         "/hello"                 5 hours ago         Exited (0) 5 hours ago                       flamboyant_lichterman
    

    3.运行容器

    [root@localhost ~]# docker start b0629eeb59a6
    b0629eeb59a6
    [root@localhost ~]# docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                   PORTS               NAMES
    b0629eeb59a6        httpd               "httpd-foreground"       12 minutes ago      Up 7 seconds             80/tcp              priceless_haibt
    

    4.停止容器

    处于running状态的容器可以通过docker stopdocker kill来停止容器

    [root@localhost ~]# docker stop b0629eeb59a6
    b0629eeb59a6
    [root@localhost ~]# docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
    b0629eeb59a6        httpd               "httpd-foreground"       37 minutes ago      Exited (0) 2 seconds ago                       priceless_haibt
    
    [root@localhost ~]# docker kill b0629eeb59a6
    b0629eeb59a6
    [root@localhost ~]# docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
    b0629eeb59a6        httpd               "httpd-foreground"       40 minutes ago      Exited (137) 4 seconds ago                       priceless_haibt
    

    5.删除容器

    [root@localhost ~]# docker rm b0629eeb59a6
    b1666dbdc44e
    [root@localhost ~]# docker ps -a
    CONTAINER ID   IMAGE        COMMAND    CREATED          STATUS              NAMES
    [root@localhost ~]#  
    

    6.连接容器

    使用docker attach/exec都可以进入容器,但是使用docker attach连接后exit会直接退出容器,而docker exec 并不会

    [root@localhost ~]# docker ps
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
    4a10d126524f        ubuntu              "bash"              6 hours ago         Up 9 seconds                            brave_goldwasser
    
    [root@localhost ~]# docker attach 4a10d126524f
    root@4a10d126524f:/# top
    
    root@4a10d126524f:/# exit
    exit
    
    [root@localhost ~]# docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
    d0058bbeea30        httpd               "httpd-foreground"       21 minutes ago      Exited (0) 8 minutes ago                       sharp_hoover
    e40604d8e4cd        nginx               "/docker-entrypoint.…"   4 hours ago         Created                                        focused_kirch
    4a10d126524f        ubuntu              "bash"                   7 hours ago         Exited (0) 7 seconds ago                       brave_goldwasser
    8fb045bce0da        hello-world         "/hello"                 7 hours ago         Exited (0) 7 hours ago                         flamboyant_lichterman
    
    ##使用Ctrl+p Ctrl+q正常断开容器连接
    

    7.导出/导入容器

    ##导出容器
    docker export <CONTAINER ID > > my_container.tar
    ##导入容器
    cat my_container.tar |docker import - image_name:tag
    

    镜像操作

    1.搜索镜像

    [root@localhost ~]# docker search redis
    NAME                             DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
    redis                            Redis is an open source key-value store that…   8435                [OK]                
    bitnami/redis                    Bitnami Redis Docker Image                      156                                     [OK]
    sameersbn/redis                                                                  80                                      [OK]
    grokzen/redis-cluster            Redis cluster 3.0, 3.2, 4.0, 5.0, 6.0           70                                      
    

    2.拉取镜像

    [root@localhost ~]# docker pull redis
    Using default tag: latest
    latest: Pulling from library/redis
    6ec8c9369e08: Already exists 
    efe6cceb88f8: Pull complete 
    cdb6bd1ce7c5: Pull complete 
    9d80498f79fe: Pull complete 
    b7cd40c9247b: Pull complete 
    96403647fb55: Pull complete 
    Digest: sha256:d86d6739fab2eaf590cfa51eccf1e9779677bd2502894579bcf3f80cb37b18d4
    Status: Downloaded newer image for redis:latest
    

    3.列出镜像

    [root@localhost ~]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    ubuntu              latest              1e4467b07108        3 days ago          73.9MB
    httpd               latest              9d2a0c6e5b57        5 days ago          166MB
    nginx               latest              8cf1bfb43ff5        5 days ago          132MB
    redis               latest              50541622f4f1        5 days ago          104MB
    hello-world         latest              bf756fb1ae65        6 months ago        13.3kB
    

    4.删除镜像

    [root@localhost ~]# docker rmi redis
    Untagged: redis:latest
    Untagged: redis@sha256:d86d6739fab2eaf590cfa51eccf1e9779677bd2502894579bcf3f80cb37b18d4
    Deleted: sha256:50541622f4f179450f4acec7d16964499525932a81263eafb91e699671f58ee4
    Deleted: sha256:749ad2917127693e3aa3e4b5862e6918605c4fab8a4c3af783423589f4be4596
    Deleted: sha256:605d681500daad3e544ba780a79ab8430ce5532cd12db9eedaadec46acce3dc3
    Deleted: sha256:20fef6e1eca972f0a72b972c8341885c8ef614fb07ce2579666f5bc82736c46c
    Deleted: sha256:2d394a18c7a0943afd82cb53a050fcb6ad08d6b8ac8d8238a4ec6a7682c1393a
    Deleted: sha256:cf0218df82e32588b4a0e24fcb6fcf3c6407544e44e670c49d5deb2f68c7d7eb
    [root@localhost ~]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    ubuntu              latest              1e4467b07108        3 days ago          73.9MB
    httpd               latest              9d2a0c6e5b57        5 days ago          166MB
    nginx               latest              8cf1bfb43ff5        5 days ago          132MB
    hello-world         latest              bf756fb1ae65        6 months ago        13.3kB
    [root@localhost ~]# 
    

    5.导出/导入镜像

    ##导出镜像
    docker save -o nginx.tar nginx:latest
    
    ##导入镜像
    docker load -i nginx.tar 
    
  • 相关阅读:
    快速幂算法
    素数筛
    数论知识点总结
    ABOUT MY NAME
    CF1043F Make It One
    树形DP
    魔兽世界联盟8.1主线任务
    模板std::mutex用法:
    【转】正确的提问方式
    第一个Python游戏窗口
  • 原文地址:https://www.cnblogs.com/elfcafe/p/13401704.html
Copyright © 2020-2023  润新知