• docker 常用命令


    多数命令都有新旧两种格式,命令向后兼容,两种格式都能使用。

    1. docker version

    client 和 server 版本最好一致

    server 也叫 engine,在 mac 或 linux 上也叫 daemon
    
    • 1

    2. docker info

    显示 docker engin 的绝大多数配置

    3. docker

    查看所有 docker 命令

    4. docker container run ...........

    docker run ........... (old way)
    启动一个新的容器

    5. docker container ls

    docker ps (old way)
    列出正在运行的容器

    6. docker container stop container_id

    docker stop container_id (old way)
    停止容器进程,并不删除
    container_id 不必写全,只写前几位独一无二能让docker 识别是哪一个容器就行。

    7. docker container ls -a

    列出全部正在运行和已经停止的容器

    8. run vs. start

    docker container run ........... 启动一个容器
    docker container start ...........启动一个存在的已经停止的容器

    9. --name 指定容器名称,不指定则由docker 随机生成。

    ```bash
    docker container run --publish 80:80 --detach --name webserver nginx
    ```
    此命令启动一个名称为 `webserver` 的 nginx 容器,
    
    • 1
    • 2
    • 3
    • 4

    10. docker container logs container_id_or_name

    docker logs container_id_or_name (old way)
    显示特定容器的 log

    11. docker container top container_id_or_name

    显示容器内的进程

    12. docker container --help

    查看可以应用于容器的命令

    C:\Users\yanchun>docker container --help
    
    Usage:  docker container COMMAND
    
    Manage containers
    
    Commands:
      attach      Attach local standard input, output, and error streams to a running container
      commit      Create a new image from a container's changes
      cp          Copy files/folders between a container and the local filesystem
      create      Create a new container
      diff        Inspect changes to files or directories on a container's filesystem
      exec        Run a command in a running container
      export      Export a container's filesystem as a tar archive
      inspect     Display detailed information on one or more containers
      kill        Kill one or more running containers
      logs        Fetch the logs of a container
      ls          List containers
      pause       Pause all processes within one or more containers
      port        List port mappings or a specific mapping for the container
      prune       Remove all stopped containers
      rename      Rename a container
      restart     Restart one or more containers
      rm          Remove one or more containers
      run         Run a command in a new container
      start       Start one or more stopped containers
      stats       Display a live stream of container(s) resource usage statistics
      stop        Stop one or more running containers
      top         Display the running processes of a container
      unpause     Unpause all processes within one or more containers
      update      Update configuration of one or more containers
      wait        Block until one or more containers stop, then print their exit codes
    
    Run 'docker container COMMAND --help' for more information on a command.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    13. docker container rm id_1 id_2 id_3 .....

    docker rm id_1 id_2 id_3 ..... (old way)
    删除一个或多个容器,但是,出于安全的考虑,不允许删除正在运行的容器,所以下面的命令,有一个容器由于正在运行,删除出错:

    C:\Users\yanchun>docker container ls -a
    CONTAINER ID   IMAGE     COMMAND                  CREATED             STATUS                         PORTS                    NAMES
    b7bea902ff8f   nginx     "/docker-entrypoint.…"   21 minutes ago      Up 21 minutes                  0.0.0.0:80->80/tcp       webserver
    946f7e643cdf   nginx     "/docker-entrypoint.…"   About an hour ago   Exited (0) About an hour ago                            agitated_benz
    d7d47384102d   nginx     "/docker-entrypoint.…"   2 hours ago         Exited (0) About an hour ago                            sharp_kirch
    1ad86b3be23e   redis     "docker-entrypoint.s…"   6 days ago          Exited (255) 4 hours ago       0.0.0.0:6379->6379/tcp   redis-go-chat
    
    C:\Users\yanchun>docker container rm b7 94 d7 1a
    94
    d7
    1a
    Error response from daemon: You cannot remove a running container b7bea902ff8fca1ea8059f1841160637a6acf9536be563e9118eaab2dd10929d. Stop the container before attempting removal or force remove
    
    C:\Users\yanchun>docker container ls -a
    CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                NAMES
    b7bea902ff8f   nginx     "/docker-entrypoint.…"   22 minutes ago   Up 22 minutes   0.0.0.0:80->80/tcp   webserver
    
    C:\Users\yanchun>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    要删除这个正在运行的容器,可以分两步,先用停止,再删除。
    但是也可以使用 -f 选项强制一步删除:

    C:\Users\yanchun>docker container ls
    CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                NAMES
    b7bea902ff8f   nginx     "/docker-entrypoint.…"   25 minutes ago   Up 25 minutes   0.0.0.0:80->80/tcp   webserver
    
    C:\Users\yanchun>docker container rm -f b7
    b7
    
    C:\Users\yanchun>docker container ls -a
    CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
    
    C:\Users\yanchun>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    在这里插入图片描述

  • 相关阅读:
    Chrome 配置samesite=none方式
    redis5.0集群搭建(两台服务器)
    如何使用Net2FTP搭建免费web文件管理器打造个人网盘
    23种设计模式之职责链模式(Chain of Responsibility Pattern)
    MySQL索引
    Spring集成高性能队列Disruptor
    黑帽python第二版(Black Hat Python 2nd Edition)读书笔记 之 第八章 Windows常见特洛伊木马任务(4)沙箱检测
    44.(前端)修改菜单路由地址
    VTK 标注类Widget 文字标注 vtkCaptionWidget
    学习笔记——PCA降维sklearn
  • 原文地址:https://blog.csdn.net/ftell/article/details/126124558