zoukankan      html  css  js  c++  java
  • Docker 常用容器命令

    一、新建并启动容器

    docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

    由于启动命令太多了,我们就选取几个常用的进行演示

    [OPTIONS] 常用值

    --name="容器新名字": 为容器指定一个别名
    -d: 后台运行容器,并返回容器ID,也即启动守护式容器
    -i:以交互模式运行容器,通常与 -t 同时使用
    -t:为容器重新分配一个伪输入终端,通常与 -i 同时使用
    -P(大写 P): 随机端口映射
    -p: 指定端口映射,有以下四种格式
      ip:hostPort:containerPort
      ip::containerPort
      hostPort:containerPort (推荐)
      containerPort

    1、启动 centos 镜像

    下面两条命令是等价的

    docker run -it --name=mycentos centos:centos7.9.2009
    docker run -it --name=mycentos centos:centos7.9.2009 /bin/bash
    

    2、交互模式 / 后台模式启动 tomcat

    // 交互模式启动 tomcat
    docker run -it --name tomcat01 -p 8088:8080 tomcat:8.5.61-jdk8-corretto
    
    // 后台模式启动 tomcat
    docker run -d --name=tomcat02 -p 8089:8080 tomcat:8.5.61-jdk8-corretto
    

    启动信息如下:

    二、列出正在运行的容器

    docker ps [OPTIONS]
    

    -a :列出当前所有正在运行的容器 + 历史上运行过的
    -l :显示最近创建的容器
    -n :显示最近n个创建的容器
    -q :静默模式,只显示容器编号
    -s :显示容器大小
    --no-trunc : 不截断输出

    三、进入正在启动的容器

    docker exec -it 容器 ID /bin/bash

    四、退出容器客户端

    // 方式一、退出容器客户端,并且关闭容器
    exit
    // 方式二、退出容器客户端,不关闭容器
    CTRL + P + Q
    

      

    五、启动容器

    当然也可以使用下列命令启动全部的容器

    docker start $(docker ps -aq)
    

      

    六、关闭容器

    关闭容器和启动容器相似,可以关闭一个,也可以关闭多个,也可以批量关闭

    当然也可以使用下列命令强制关闭容器

    docker kill 容器ID或者容器别名
    

      

    七、删除已停止容器

    八、其它重要命令

    1、查看容器日志

    docker logs -f -t --tail 显示行数 容器ID
    

    -t: 加入时间戳
    -f: 跟随最新的日志打印
    -n 500: 显示 500 行

    2、查看容器内的进程

    docker top 容器 ID
    

    3、查看容器内的细节

    docker inspect 容器 ID 

    以 json 串的形式显示容器的信息

    4、容器和主机之间文件交互

    从容器中拷贝文件到主机

    docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH

    5、从宿主机拷贝文件到容器中

    docker cp [OPTIONS] SRC_PATH CONTAINER:DEST_PATH
    

    九、总结

     常用命令总结

    // 当前 shell 下 attach 连接指定运行镜像
    attach    Attach to a running container         
    // 通过 Dockerfile 定制镜像       
    build     Build an image from a Dockerfile 
    // 提交当前容器为新的镜像             
    commit    Create a new image from a container changes   
    // 从容器中拷贝指定文件或者目录到宿主机中
    cp        Copy files/folders from the containers filesystem to the host path   
    // 创建一个新的容器,同 run,但不启动容器
    create    Create a new container   
    // 查看 docker 容器变化                     
    diff      Inspect changes on a container's filesystem   
    // 从 docker 服务获取容器实时事件
    events    Get real time events from the server 
    // 在已存在的容器上运行命令         
    exec      Run a command in an existing container     
    // 导出容器的内容流作为一个 tar 归档文件[对应 import ]   
    export    Stream the contents of a container as a tar archive  
    // 展示一个镜像形成历史 
    history   Show the history of an image       
    // 列出系统当前镜像           
    images    List images           
    // 从tar包中的内容创建一个新的文件系统映像[对应export]                        
    import    Create a new filesystem image from the contents of a tarball 
    // 显示系统相关信息
    info      Display system-wide information 
    // 查看容器详细信息              
    inspect   Return low-level information on a container  
    // kill 指定 docker 容器 
    kill      Kill a running container         
    // 从一个 tar 包中加载一个镜像[对应 save]              
    load      Load an image from a tar archive 
    // 注册或者登录一个 docker 源服务器             
    login     Register or Login to the docker registry server   
    // 从当前 Docker registry 退出  
    logout    Log out from a Docker registry server  
    // 输出当前容器日志信息        
    logs      Fetch the logs of a container        
    // 查看映射端口对应的容器内部源端口          
    port      Lookup the public-facing port which is NAT-ed to PRIVATE_PORT     
    // 暂停容器
    pause     Pause all processes within a container        
    // 列出容器列表
    ps        List containers     
    // 从 docker 镜像源服务器拉取指定镜像或者库镜像                          
    pull      Pull an image or a repository from the docker registry server   
    // 推送指定镜像或者库镜像至docker源服务器
    push      Push an image or a repository to the docker registry server   
    // 重启运行的容器 
    restart   Restart a running container  
    // 移除一个或者多个容器                 
    rm        Remove one or more containers      
    // 移除一个或多个镜像[无容器使用该镜像才可删除,否则需删除相关容器才可继续或 -f 强制删除]           
    rmi       Remove one or more images      
    // 创建一个新的容器并运行一个命令       
    run       Run a command in a new container  
    //  保存一个镜像为一个 tar 包[对应 load]            
    save      Save an image to a tar archive        
    // 在 docker hub 中搜索镜像        
    search    Search for an image on the Docker Hub   
    // 启动容器      
    start     Start a stopped containers    
    // 停止容器                
    stop      Stop a running containers 
    // 给源中镜像打标签                    
    tag       Tag an image into a repository     
    // 查看容器中运行的进程信息           
    top       Lookup the running processes of a container 
    // 取消暂停容器  
    unpause   Unpause a paused container     
    // 查看 docker 版本号               
    version   Show the docker version information      
    // 截取容器停止时的退出状态值     
    wait      Block until a container stops, then print its exit code
    

      

  • 相关阅读:
    【进阶技术】一篇文章搞掂:Spring Cloud Stream
    es6的foreach循环遍历
    centos下安装redis
    linux下安装nginx
    centos下安装Mysql5.7.20
    ubuntu下安装Mysql
    Maven发布项目丢失Mybatis Mapper包的映射问题
    linux下导入导出oracle的dmp文件
    表达式计算-----------eval()运算符
    Linux下安装mysql
  • 原文地址:https://www.cnblogs.com/xiaomaomao/p/14190637.html
Copyright © 2011-2022 走看看