Docker開篇之HelloWorld


按照程序世界的慣例,我們應該以HelloWorld的程序為起點開始介紹。那么接下來我們就看看Docker的HelloWorld是如何運行的。

安裝 Docker CE

由於我的系統是OSX,個人推薦使用homebrew安裝docker,該方式方便減少交互,只需在Terminal敲入以下代碼:

brew cask install docker

由於眾所周知原因,下載過程比較慢,需要耐心等待。安裝結果如下所示:

Updating Homebrew...
==> Satisfying dependencies
==> Downloading https://download.docker.com/mac/stable/23011/Docker.dmg
######################################################################## 100.0%
==> Verifying checksum for Cask docker
==> Installing Cask docker
==> Moving App 'Docker.app' to '/Applications/Docker.app'.
🍺  docker was successfully installed!

預備工作:image倉庫的鏡像地址

示例程序 HelloWorld 的image文件需要從倉庫下載,但國內訪問 Docker的官方倉庫很慢,還經常斷線,所以需要把倉庫地址改為國內的鏡像站。這里推薦使用官方鏡像registry.docker-cn.com。添加方法如下圖所示:

docker-mirror-1

docker-mirror-2

應用新的配置后,可以正常獲取鏡像文件了。

運行 HelloWorld 鏡像

接下來是激動人心的時刻,在終端輸入以下代碼:

docker run hello-world

終端會輸出以下信息:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:97ce6fa4b6cdc0790cda65fe7290b74cfebd9fa0c9b8c38e979330d547d22ce1
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
   (amd64)
3. The Docker daemon created a new container from that image which runs the
   executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
   to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

那么恭喜,DockerHelloWorld 的入門示例程序已經運行成功了,覺得是不是 so easy 。下篇文章我會寫些概念的東西,如 什么是容器為什么用容器容器是怎么工作的等一些基礎概念,這些內容是我從網上摘抄過來的,用以理解。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM