為小白準備的重要 Docker 命令說明

來源:互聯網
上載者:User

為小白準備的重要 Docker 命令說明

早先的教程中,我們學過了在 RHEL CentOS 7 上安裝 Docker 並建立 docker 容器。 在本教程中,我們會學習管理 docker 容器的其他命令。

Docker 命令文法
  1. $ docker [option][command][arguments]

要列出 docker 支援的所有命令,運行

  1. $ docker

我們會看到如下結果,

  1. attach Attach to a running container
  2. build Build an image from a Dockerfile
  3. commit Create a new image from a container's changes
  4. cp Copy files/folders between a container and the local filesystem
  5. create Create a new container
  6. diff Inspect changes on a container's filesystem
  7. events Get real time events from the server
  8. execRun a command in a running container
  9. exportExport a container's filesystem as a tar archive
  10. history Show the history of an image
  11. images List images
  12. import Import the contents from a tarball to create a filesystem image
  13. info Display system-wide information
  14. inspect Return low-level information on a container or image
  15. kill Kill a running container
  16. load Load an image from a tar archive or STDIN
  17. login Log in to a Docker registry
  18. logout Log out from a Docker registry
  19. logs Fetch the logs of a container
  20. network Manage Docker networks
  21. pause Pause all processes within a container
  22. port List port mappings or a specific mapping for the CONTAINER
  23. ps List containers
  24. pull Pull an image or a repository from a registry
  25. push Push an image or a repository to a registry
  26. rename Rename a container
  27. restart Restart a container
  28. rm Remove one or more containers
  29. rmi Remove one or more images
  30. run Run a command in a new container
  31. save Save one or more images to a tar archive
  32. search Search the Docker Hub for images
  33. start Start one or more stopped containers
  34. stats Display a live stream of container(s) resource usage statistics
  35. stop Stop a running container
  36. tag Tag an image into a repository
  37. top Display the running processes of a container
  38. unpause Unpause all processes within a container
  39. update Update configuration of one or more containers
  40. version Show the Docker version information
  41. volume Manage Docker volumes
  42. wait Block until a container stops, then print its exit code

要進一步查看某個命令支援的選項,運行:

  1. $ docker docker-subcommand info

就會列出 docker 子命令所支援的選項了。

 

測試與 Docker Hub 的串連

預設,所有鏡像都是從 Docker Hub 中拉取下來的。我們可以從 Docker Hub 上傳或下載作業系統鏡像。為了檢查我們是否能夠正常地通過 Docker Hub 上傳/下載鏡像,運行

  1. $ docker run hello-world

結果應該是:

  1. HellofromDocker.
  2. This message shows that your installation appears to be working correctly.

輸出結果表示你可以訪問 Docker Hub 而且也能從 Docker Hub 下載 docker 鏡像。

 

搜尋鏡像

搜尋容器的鏡像,運行

  1. $ docker search Ubuntu

我們應該會得到可用的 Ubuntu 鏡像的列表。記住,如果你想要的是官方的鏡像,請檢查 official 這一列上是否為 [OK]

 

下載鏡像

一旦搜尋並找到了我們想要的鏡像,我們可以運行下面語句來下載它:

  1. $ docker pull Ubuntu

要查看所有已下載的鏡像,運行:

  1. $ docker images

 

運行容器

使用已下載鏡像來運行容器,使用下面命令:

  1. $ docker run -it Ubuntu

這裡,使用 -it 會開啟一個 shell 與容器互動。容器啟動並運行後,我們就可以像普通機器那樣來使用它了,我們可以在容器中執行任何命令。

 

顯示所有的 docker 容器

要列出所有 docker 容器,運行:

  1. $ docker ps

會輸出一個容器列表,每個容器都有一個容器 id 標識。

 

停止 docker 容器

要停止 docker 容器,運行:

  1. $ docker stop container-id

 

從容器中退出

要從容器中退出,執行:

  1. $ exit

 

儲存容器狀態

容器運行並更改後(比如安裝了 apache 伺服器),我們可以儲存容器狀態。這會在本地系統上儲存新建立鏡像。

運行下面語句來提交並儲存容器狀態:

  1. $ docker commit 85475ef774 repository/image_name

這裡,commit 命令會儲存容器狀態,85475ef774,是容器的容器 id,repository,通常為 docker hub 上的使用者名稱 (或者新加的倉庫名稱)image_name,是新鏡像的名稱。

我們還可以使用 -m-a 來添加更多資訊。通過 -m,我們可以留個資訊說 apache 伺服器已經安裝好了,而 -a 可以添加作者名稱。

像這樣:

  1. docker commit -m "apache server installed"-a "Dan Daniels"85475ef774 daniels_dan/Cent_container

我們的教程至此就結束了,本教程講解了一下 Docker 中的那些重要的命令,如有疑問,歡迎留言。

更多Docker相關教程見以下內容: 

Docker安裝應用(CentOS 6.5_x64)  

Ubuntu 16.04 伺服器上配置使用 Docker   

Ubuntu 15.04下安裝Docker   

Docker 安裝執行個體  

Docker 建立基礎鏡像   

在 Ubuntu 15.04 上如何安裝Docker及基本用法  

Ubuntu 16.04上Docker使用手記  

使用Docker分分鐘啟動常用應用   

Ubuntu 16.04下Docker修改設定檔不生效解決辦法   

Docker 的詳細介紹:請點這裡
Docker 的:請點這裡

via: http://linuxtechlab.com/important-docker-commands-beginners/

作者:Shusain 譯者:lujun9972 校對:wxy

本文由 LCTT 原創編譯,Linux中國 榮譽推出

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.