1. 程式人生 > >docker入門2---docker的初體驗

docker入門2---docker的初體驗

1.8 cut -a too world epo blank name 和我

技術分享圖片

Tomxin7

Simple, Interesting | 簡單,有趣

第一個Docker鏡像?

嘗試運行docker自帶的鏡像“hello-world”,了解docker鏡像的下載和啟動。
docker的整個生命周期有三部分組成:鏡像(image)+容器(container)+倉庫(repository)

1、查看當前鏡像

[root@tomxin docker]# docker images
結果應該是當前系統暫時還沒有任何鏡像

2、下載“hello-world”鏡像

[root@tomxin docker]# docker pull hello-world
Using default tag: latest
Trying to pull repository docker.io/library/hello-world ... 
latest: Pulling from docker.io/library/hello-world
d1725b59e92d: Pull complete 
Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
Status: Downloaded newer image for docker.io/hello-world:latest

3、再次查看鏡像

[root@tomxin docker]# docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
docker.io/hello-world     latest              4ab4c602aa5e        5 weeks ago         1.84 kB

第一個docker容器

運行“hello-world”容器

鏡像和容器的區別:容器是由鏡像實例化而來,這和我們學習的面向對象的概念十分相似,我們可以把鏡像看作類,把容器看作類實例化後的對象。

1、查看容器

[root@tomxin docker] docker ps -a
應該暫時還沒有容器

2、運行“hello-world”,運行成功後,docker會創建一個容器,上面輸出了hello-world這個項目運行,docker所做的工作步驟

[root@tomxin docker]# docker run hello-world

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://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

3、查看全部容器,可以看到在run鏡像之後,創建了一個容器

[root@tomxin docker] docker ps -a

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                    NAMES
b44b6c29ac29        hello-world         "/hello"                 14 minutes ago      Exited (0) 14 minutes ago 

註意:如果沒有提前pull鏡像,直接運行docker run命令也是可以的,在本地沒有找到對應鏡像,docker會自動請求遠程倉庫並且下載、啟動鏡像,下圖展示了docker基本的工作原理。
技術分享圖片

docker入門2---docker的初體驗