Docker - 常用命令

Docker - 常用命令

  1. 幫助命令:
    1. docker version
    2. docker info
    3. docker --help
  2. 鏡像命令:
    1. docker images:
      1. 列出本地主機上的鏡像:docker images
        在這裏插入圖片描述
      2. 各個選項說明:
        1. REPOSITORY:表示鏡像的倉庫源。
        2. TAG:鏡像的標籤。
        3. IMAGE ID:鏡像ID。
        4. CREATED:鏡像創建時間。
        5. SIZE:鏡像大小。
      3. 同一倉庫源可以有多個 TAG,代表這個倉庫源的不同個版本,我們使用 REPOSITORY:TAG 來定義不同的鏡像。
        如果你不指定一個鏡像的版本標籤,例如你只使用 ubuntu,docker 將默認使用 ubuntu:latest 鏡像。
    2. docker search 某個XXX鏡像名字:
      1. 網站:https://hub.docker.com
      2. 命令:
        1. docker search [OPTIONS] 鏡像名字
        2. OPTIONS說明:
          1. –no-trunc : 顯示完整的鏡像描述。
          2. -s : 列出收藏數不小於指定值的鏡像。
          3. –automated : 只列出 automated build類型的鏡像。
    3. docker pull 某個XXX鏡像名字:
      1. 下載鏡像
      2. docker pull 鏡像名字[:TAG]
    4. docker rmi 某個XXX鏡像名字ID:
      1. 刪除鏡像。
      2. 刪除單個:docker rmi -f 鏡像ID。
      3. 刪除多個:docker rmi -f 鏡像名1:TAG 鏡像名2:TAG 。
      4. 刪除全部:docker rmi -f $(docker images -qa)。
  3. 容器命令:
    1. 有鏡像才能創建容器,這是根本前提(下載一個CentOS鏡像演示):docker pull centos
    2. 新建並啓動容器:
      1. docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
        1. OPTIONS說明(常用):有些是一個減號,有些是兩個減號
          1. –name=“容器新名字”: 爲容器指定一個名稱。
          2. -d: 後臺運行容器,並返回容器ID,也即啓動守護式容器。
          3. -i:以交互模式運行容器,通常與 -t 同時使用。
          4. -t:爲容器重新分配一個僞輸入終端,通常與 -i 同時使用。
          5. -P: 隨機端口映射。
          6. -p: 指定端口映射,有以下四種格式:
            1. ip:hostPort:containerPort
            2. ip::containerPort
            3. hostPort:containerPort
            4. containerPort
        2. 啓動交互式容器:
          1. docker images在這裏插入圖片描述
          2. 使用鏡像centos:latest以交互模式啓動一個容器,在容器內執行/bin/bash命令。
          3. docker run -it centos /bin/bash
          4. ps -ef在這裏插入圖片描述
    3. 列出當前所有正在運行的容器:
      1. docker ps [OPTIONS]
        1. -a :列出當前所有正在運行的容器+歷史上運行過的。
        2. -l :顯示最近創建的容器。
        3. -n:顯示最近n個創建的容器。
        4. -q :靜默模式,只顯示容器編號。
        5. –no-trunc :不截斷輸出。
    4. 退出容器:
      1. exit:容器停止退出。
      2. Ctrl+P+Q:容器不停止退出。
    5. 啓動容器:docker start 容器ID或者容器名。
    6. 重啓容器:docker restart 容器ID或者容器名。
    7. 停止容器:docker stop 容器ID或者容器名。
    8. 強制停止容器:docker kill 容器ID或者容器名。
    9. 刪除已停止的容器:docker rm 容器ID
      1. 一次性刪除多個容器:
        1. docker rm -f $(docker ps -a -q)
        2. docker ps -a -q | xargs docker rm
    10. 重要:
      1. 啓動守護式容器:
        1. docker run -d 容器名
        2. 使用鏡像centos:latest以後臺模式啓動一個容器
        3. 問題:然後docker ps -a 進行查看, 會發現容器已經退出
        4. 很重要的要說明的一點: Docker容器後臺運行,就必須有一個前臺進程。
        5. 容器運行的命令如果不是那些一直掛起的命令(比如運行top,tail),就是會自動退出的。
        6. 這個是docker的機制問題,比如你的web容器,我們以nginx爲例,正常情況下,我們配置啓動服務只需要啓動響應的service即可。
        7. 例如:service nginx start
        8. 但是,這樣做,nginx爲後臺進程模式運行,就導致docker前臺沒有運行的應用,
          這樣的容器後臺啓動後,會立即自殺因爲他覺得他沒事可做了.
          所以,最佳的解決方案是,將你要運行的程序以前臺進程的形式運行
      2. 查看容器日誌:
        1. docker logs -f -t --tail 容器ID
          1. -t 是加入時間戳。
          2. -f 跟隨最新的日誌打印。
          3. –tail 數字 顯示最後多少條
      3. 查看容器內運行的進程:docker top 容器ID
      4. 查看容器內部細節:docker inspect 容器ID
      5. 進入正在運行的容器並以命令行交互:
        1. docker exec -it 容器ID bashShell
        2. 重新進入:docker attach 容器ID
        3. attach 直接進入容器啓動命令的終端,不會啓動新的進程。
        4. exec 是在容器中打開新的終端,並且可以啓動新的進程。
      6. 從容器內拷貝文件到主機上:
        1. docker cp 容器ID:容器內路徑 目的主機路徑
  4. 總結:
    在這裏插入圖片描述
    1. 當前 shell 下 attach 連接指定運行鏡像:
    2. 通過 Dockerfile 定製鏡像:
    3. 提交當前容器爲新的鏡像:
    4. 從容器中拷貝指定文件或者目錄到宿主機中:
    5. 創建一個新的容器,同 run,但不啓動容器:create Create a new container
    6. 查看 docker 容器變化:diff Inspect changes on a container’s filesystem
    7. 從 docker 服務獲取容器實時事件:events Get real time events from the server
    8. 在已存在的容器上運行命令:exec Run a command in an existing container
    9. 導出容器的內容流作爲一個 tar 歸檔文件[對應 import ]:export Stream the contents of a container as a tar archive
    10. 展示一個鏡像形成歷史:history Show the history of an image
    11. 列出系統當前鏡像:images List images
    12. 從tar包中的內容創建一個新的文件系統映像[對應export]:import Create a new filesystem image from the contents of a tarball
    13. 顯示系統相關信息:info Display system-wide information
    14. 查看容器詳細信息:inspect Return low-level information on a container
    15. kill 指定 docker 容器:kill Kill a running container
    16. 從一個 tar 包中加載一個鏡像[對應 save]:load Load an image from a tar archive
    17. 註冊或者登陸一個 docker 源服務器:login Register or Login to the docker registry server
    18. 從當前 Docker registry 退出:logout Log out from a Docker registry server
    19. 輸出當前容器日誌信息:logs Fetch the logs of a container
    20. 查看映射端口對應的容器內部源端口:port Lookup the public-facing port which is NAT-ed to PRIVATE_PORT
    21. 暫停容器:pause Pause all processes within a container
    22. 列出容器列表:ps List containers
    23. 從docker鏡像源服務器拉取指定鏡像或者庫鏡像:pull Pull an image or a repository from the docker registry server
    24. 推送指定鏡像或者庫鏡像至docker源服務器:push Push an image or a repository to the docker registry server
    25. 重啓運行的容器:restart Restart a running container
    26. 移除一個或者多個容器:rm Remove one or more containers
    27. 移除一個或多個鏡像[無容器使用該鏡像纔可刪除,否則需刪除相關容器纔可繼續或 -f 強制刪除]:rmi Remove one or more images
    28. 創建一個新的容器並運行一個命令:run Run a command in a new container
    29. 保存一個鏡像爲一個 tar 包[對應 load]:save Save an image to a tar archive
    30. 在 docker hub 中搜索鏡像:search Search for an image on the Docker Hub
    31. 啓動容器:start Start a stopped containers
    32. 停止容器:stop Stop a running containers
    33. 給源中鏡像打標籤:tag Tag an image into a repository
    34. 查看容器中運行的進程信息:top Lookup the running processes of a container
    35. 取消暫停容器:unpause Unpause a paused container
    36. 查看 docker 版本號:version Show the docker version information
    37. 截取容器停止時的退出狀態值:wait Block until a container stops, then print its exit code
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章