1. 程式人生 > >Docker學習筆記--Docker常用命令

Docker學習筆記--Docker常用命令

1. 檢視docker資訊(version、info)

  1. # 檢視docker版本  
  2. $docker version  
  3. # 顯示docker系統的資訊  
  4. $docker info  

2. 對image的操作(search、pull、images、rmi、history)

  1. # 檢索image  
  2. $docker search image_name  
  3. # 下載image  
  4. $docker pull image_name  
  5. # 列出映象列表; -a, --all=false Show all images; --no-trunc=false Don't truncate output; -q, --quiet=false Only show numeric IDs  
  6. $docker images  
  7. # 刪除一個或者多個映象; -f, --force=false Force; --no-prune=false Do not delete untagged parents  
  8. $docker rmi image_name  
  9. # 顯示一個映象的歷史; --no-trunc=false Don't truncate output; -q, --quiet=false Only show numeric IDs  
  10. $docker history image_name  

3. 啟動容器(run)

docker容器可以理解為在沙盒中執行的程序。這個沙盒包含了該程序執行所必須的資源,包括檔案系統、系統類庫、shell 環境等等。但這個沙盒預設是不會執行任何程式的。你需要在沙盒中執行一個程序來啟動某一個容器。這個程序是該容器的唯一程序,所以當該程序結束的時候,容器也會完全的停止。


  1. # 在容器中執行"echo"命令,輸出"hello word"  
  2. $docker run image_name echo "hello word"  
  3. # 互動式進入容器中  
  4. $docker run -i -t image_name /bin/bash  
  5. # 在容器中安裝新的程式  
  6. $docker run image_name apt-get install -y app_name  

Note:  在執行apt-get 命令的時候,要帶上-y引數。如果不指定-y引數的話,apt-get命令會進入互動模式,需要使用者輸入命令來進行確認,但在docker環境中是無法響應這種互動的。apt-get 命令執行完畢之後,容器就會停止,但對容器的改動不會丟失。

4. 檢視容器(ps)

  1. # 列出當前所有正在執行的container  
  2. $docker ps  
  3. # 列出所有的container  
  4. $docker ps -a  
  5. # 列出最近一次啟動的container  
  6. $docker ps -l  

5. 儲存對容器的修改(commit)

當你對某一個容器做了修改之後(通過在容器中執行某一個命令),可以把對容器的修改儲存下來,這樣下次可以從儲存後的最新狀態執行該容器。
  1. # 儲存對容器的修改; -a, --author="" Author; -m, --message="" Commit message  
  2. $docker commit ID new_image_name  

Note:  image相當於類,container相當於例項,不過可以動態給例項安裝新軟體,然後把這個container用commit命令固化成一個image。

6. 對容器的操作(rm、stop、start、kill、logs、diff、top、cp、restart、attach)

  1. # 刪除所有容器  
  2. $docker rm `docker ps -a -q`  
  3. # 刪除單個容器; -f, --force=false; -l, --link=false Remove the specified link and not the underlying container; -v, --volumes=false Remove the volumes associated to the container  
  4. $docker rm Name/ID  
  5. # 停止、啟動、殺死一個容器  
  6. $docker stop Name/ID  
  7. $docker start Name/ID  
  8. $docker kill Name/ID  
  9. # 從一個容器中取日誌; -f, --follow=false Follow log output; -t, --timestamps=false Show timestamps  
  10. $docker logs Name/ID  
  11. # 列出一個容器裡面被改變的檔案或者目錄,list列表會顯示出三種事件,A 增加的,D 刪除的,C 被改變的  
  12. $docker diff Name/ID  
  13. # 顯示一個執行的容器裡面的程序資訊  
  14. $docker top Name/ID  
  15. # 從容器裡面拷貝檔案/目錄到本地一個路徑  
  16. $docker cp Name:/container_path to_path  
  17. $docker cp ID:/container_path to_path  
  18. # 重啟一個正在執行的容器; -t, --time=10 Number of seconds to try to stop for before killing the container, Default=10  
  19. $docker restart Name/ID  
  20. # 附加到一個執行的容器上面; --no-stdin=false Do not attach stdin; --sig-proxy=true Proxify all received signal to the process  
  21. $docker attach ID  

Note: attach命令允許你檢視或者影響一個執行的容器。你可以在同一時間attach同一個容器。你也可以從一個容器中脫離出來,是從CTRL-C。

7. 儲存和載入映象(save、load)

當需要把一臺機器上的映象遷移到另一臺機器的時候,需要儲存映象與載入映象。

  1. # 儲存映象到一個tar包; -o, --output="" Write to an file  
  2. $docker save image_name -o file_path  
  3. # 載入一個tar包格式的映象; -i, --input="" Read from a tar archive file  
  4. $docker load -i file_path  
  5. # 機器a  
  6. $docker save image_name > /home/save.tar  
  7. # 使用scp將save.tar拷到機器b上,然後:  
  8. $docker load < /home/save.tar  

8、 登入registry server(login)

  1. # 登陸registry server; -e, --email="" Email; -p, --password="" Password; -u, --username="" Username  
  2. $docker login  

9. 釋出image(push)

  1. # 釋出docker映象  
  2. $docker push new_image_name  

10.  根據Dockerfile 構建出一個容器

  1. #build  
  2.       --no-cache=false Do not use cache when building the image  
  3.       -q, --quiet=false Suppress the verbose output generated by the containers  
  4.       --rm=true Remove intermediate containers after a successful build  
  5.       -t, --tag="" Repository name (and optionally a tag) to be applied to the resulting image in case of success  
  6. $docker build -t image_name Dockerfile_path