Docker理論與實(shí)踐(二)

文章作者:Tyan
博客:noahsnail.com

1. Docker命令


# 列出系統(tǒng)中的所有容器
docker ps -a

# 結(jié)果
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
aadf299b179b        hello-world         "/hello"            20 minutes ago      Exited (0) 20 minutes ago                       gloomy_khorana

# docker ps僅列出當(dāng)前運(yùn)行的容器
docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

# 運(yùn)行whalesay鏡像
docker fun docker/whalesay cowsay boo

1.1 docker run hello-world解析

這個(gè)命令總共有三部分:

  • docker:告訴操作系統(tǒng)你使用的是docker程序

  • run:創(chuàng)建和運(yùn)行docker容器的子命令

  • hello-world:告訴docker將哪一個(gè)鏡像加載到容器中

1.2 docker run hello-world的運(yùn)行過(guò)程

image是一個(gè)文件系統(tǒng)邀跃,里面有運(yùn)行時(shí)使用的參數(shù)叁执。它沒(méi)有狀態(tài)且不能改變经瓷。容器是鏡像的運(yùn)行實(shí)例嫩痰。命令執(zhí)行時(shí)星爪,Docker Engine會(huì)進(jìn)行以下的步驟:

  1. 檢查hello-world軟件鏡像是否存在

  2. 如果不存在工窍,從Docker Hub上下載hello-world

  3. 加載鏡像到容器中并運(yùn)行

運(yùn)行是根據(jù)鏡像的構(gòu)建過(guò)程執(zhí)行的龄恋,可能一直運(yùn)行拿愧,也可能執(zhí)行幾個(gè)命令就退出短蜕。鏡像可能是非常復(fù)雜的氢架,例如鏡像可以啟動(dòng)一個(gè)數(shù)據(jù)庫(kù)軟件。

2. 創(chuàng)建自己的docker image

2.1 寫一個(gè)Dockerfile


$ mkdir Docker

$ cd Docker

$ touch Dockerfile

$ vim Dockerfile

在Dockerfile中加入下面的代碼并保存:

FROM docker/whalesay:latest
RUN apt-get -y update && apt-get install -y fortunes
CMD /usr/games/fortune -a | cowsay

2.2 從Dockerfile中創(chuàng)建image


# 查看Dockerfile內(nèi)容
$ cat Dockerfile

# 構(gòu)建image
$ docker build -t docker-whale .

2.3 創(chuàng)建image的過(guò)程

  • 首先Docker檢查確保有構(gòu)建的需要的東西
Sending build context to Docker daemon 2.048 kB
  • 然后Docker加載whalesay鏡像
Step 1 : FROM docker/whalesay:latest
 ---> 6b362a9f73eb
  • Docker開(kāi)始執(zhí)行apt-get命令
Step 2 : RUN apt-get -y update && apt-get install -y fortunes
 ---> Running in 3c381fdef64a
Ign http://archive.ubuntu.com trusty InRelease
Get:1 http://archive.ubuntu.com trusty-updates InRelease [65.9 kB]
Get:2 http://archive.ubuntu.com trusty-security InRelease [65.9 kB]
Hit http://archive.ubuntu.com trusty Release.gpg
Get:3 http://archive.ubuntu.com trusty-updates/main Sources [473 kB]
Get:4 http://archive.ubuntu.com trusty-updates/restricted Sources [5247 B]
Get:5 http://archive.ubuntu.com trusty-updates/universe Sources [207 kB]
Get:6 http://archive.ubuntu.com trusty-updates/main amd64 Packages [1122 kB]
Get:7 http://archive.ubuntu.com trusty-updates/restricted amd64 Packages [23.5 kB]
Get:8 http://archive.ubuntu.com trusty-updates/universe amd64 Packages [483 kB]
Hit http://archive.ubuntu.com trusty Release
Get:9 http://archive.ubuntu.com trusty-security/main Sources [152 kB]
Get:10 http://archive.ubuntu.com trusty-security/restricted Sources [3944 B]
Get:11 http://archive.ubuntu.com trusty-security/universe Sources [49.4 kB]
Get:12 http://archive.ubuntu.com trusty-security/main amd64 Packages [659 kB]
Get:13 http://archive.ubuntu.com trusty-security/restricted amd64 Packages [20.2 kB]
Get:14 http://archive.ubuntu.com trusty-security/universe amd64 Packages [178 kB]
Hit http://archive.ubuntu.com trusty/main Sources
Hit http://archive.ubuntu.com trusty/restricted Sources
Hit http://archive.ubuntu.com trusty/universe Sources
Hit http://archive.ubuntu.com trusty/main amd64 Packages
Hit http://archive.ubuntu.com trusty/restricted amd64 Packages
Hit http://archive.ubuntu.com trusty/universe amd64 Packages
Fetched 3508 kB in 25s (139 kB/s)
Reading package lists...
Reading package lists...
Building dependency tree...
Reading state information...
The following extra packages will be installed:
  fortune-mod fortunes-min librecode0
Suggested packages:
  x11-utils bsdmainutils
The following NEW packages will be installed:
  fortune-mod fortunes fortunes-min librecode0
0 upgraded, 4 newly installed, 0 to remove and 87 not upgraded.
Need to get 1961 kB of archives.
After this operation, 4817 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu/ trusty/main librecode0 amd64 3.6-21 [771 kB]
Get:2 http://archive.ubuntu.com/ubuntu/ trusty/universe fortune-mod amd64 1:1.99.1-7 [39.5 kB]
Get:3 http://archive.ubuntu.com/ubuntu/ trusty/universe fortunes-min all 1:1.99.1-7 [61.8 kB]
Get:4 http://archive.ubuntu.com/ubuntu/ trusty/universe fortunes all 1:1.99.1-7 [1089 kB]
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (This frontend requires a controlling tty.)
debconf: falling back to frontend: Teletype
dpkg-preconfigure: unable to re-open stdin: 
Fetched 1961 kB in 8s (221 kB/s)
Selecting previously unselected package librecode0:amd64.
(Reading database ... 13116 files and directories currently installed.)
Preparing to unpack .../librecode0_3.6-21_amd64.deb ...
Unpacking librecode0:amd64 (3.6-21) ...
Selecting previously unselected package fortune-mod.
Preparing to unpack .../fortune-mod_1%3a1.99.1-7_amd64.deb ...
Unpacking fortune-mod (1:1.99.1-7) ...
Selecting previously unselected package fortunes-min.
Preparing to unpack .../fortunes-min_1%3a1.99.1-7_all.deb ...
Unpacking fortunes-min (1:1.99.1-7) ...
Selecting previously unselected package fortunes.
Preparing to unpack .../fortunes_1%3a1.99.1-7_all.deb ...
Unpacking fortunes (1:1.99.1-7) ...
Setting up librecode0:amd64 (3.6-21) ...
Setting up fortune-mod (1:1.99.1-7) ...
Setting up fortunes-min (1:1.99.1-7) ...
Setting up fortunes (1:1.99.1-7) ...
Processing triggers for libc-bin (2.19-0ubuntu6.6) ...
 ---> 5c6dbfc7a7ea
  • 執(zhí)行CMD命令
Removing intermediate container 3c381fdef64a
Step 3 : CMD /usr/games/fortune -a | cowsay
 ---> Running in b0df80e0d4c9
 ---> d1178b780ac6
Removing intermediate container b0df80e0d4c9
Successfully built d1178b780ac6

2.4 測(cè)試自己創(chuàng)建的image

$ docker images

$ docker run docker-whale

# 結(jié)果
 _________________________________________ 
/ I suppose some of the variation between \
| Boston drivers and the rest of the      |
| country is due to the progressive       |
| Massachusetts Driver Education Manual   |
| which I happen to have in my top desk   |
| drawer. Some of the Tips for Better     |
| Driving are worth considering, to wit:  |
|                                         |
| [131.16d]:                              |
|                                         |
| "Directional signals are generally not  |
| used except during vehicle              |
|                                         |
| inspection; however, a left-turn signal |
| is appropriate when making              |
|                                         |
| a U-turn on a divided highway."         |
|                                         |
| [96.7b]:                                |
|                                         |
| "When paying tolls, remember that it is |
| necessary to release the                |
|                                         |
| quarter a full 3 seconds before passing |
| the basket if you are                   |
|                                         |
| traveling more than 60 MPH."            |
|                                         |
| [110.13]:                               |
|                                         |
| "When traveling on a one-way street,    |
| stay to the right, so as not            |
|                                         |
\ to interfere with oncoming traffic."    /
 ----------------------------------------- 
    \
     \
      \     
                    ##        .            
              ## ## ##       ==            
           ## ## ## ##      ===            
       /""""""""""""""""___/ ===        
  ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~   
       \______ o          __/            
        \    \        __/             
          \____\______/ 

3. 在Docker Hub上創(chuàng)建自己的倉(cāng)庫(kù)朋魔,與Git類似

略.

4. Tag, push, and pull your image

Step 1: Tag and push the image

  • 用docker images查看你當(dāng)前的image
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker-whale        latest              d1178b780ac6        2 hours ago         274.9 MB
hello-world         latest              c54a2cc56cbb        11 weeks ago        1.848 kB
docker/whalesay     latest              6b362a9f73eb        16 months ago       247 MB
  • 用docker tag命令和IMAGE ID給image打上tag岖研,YOUR_DOCKERHUB_NAME為你的Docker Hub帳號(hào),它起一個(gè)namespace的作用
docker tag d1178b780ac6 YOUR_DOCKERHUB_NAME/docker-whale:latest
  • docker images重新查看image
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
docker-whale             latest              d1178b780ac6        2 hours ago         274.9 MB
snailtyan/docker-whale   latest              d1178b780ac6        2 hours ago         274.9 MB
hello-world              latest              c54a2cc56cbb        11 weeks ago        1.848 kB
docker/whalesay          latest              6b362a9f73eb        16 months ago       247 MB
  • 使用docker login命令登錄你的Docker Hub賬戶
$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: ***
Password: 
Login Succeeded
  • 使用docker push將image推送到Docker Hub
$ docker push ***/docker-whale
The push refers to a repository [docker.io/***/docker-whale]
122284833f25: Pushed 
5f70bf18a086: Mounted from docker/whalesay 
d061ee1340ec: Mounted from docker/whalesay 
d511ed9e12e1: Mounted from docker/whalesay 
091abc5148e4: Mounted from docker/whalesay 
b26122d57afa: Mounted from docker/whalesay 
37ee47034d9b: Mounted from docker/whalesay 
528c8710fd95: Mounted from docker/whalesay 
1154ba695078: Mounted from docker/whalesay 
latest: digest: sha256:18a6032f5726bdd6cea7c15faa02d9dcf1f12ff591c42ca15fc95c0d83de04fc size: 2614
  • 你可以在你的Docker Hub上看到***/docker-whale image了

Step 2: Pull your new image

  • 先刪除本地的docker-whale
# 用id刪除image
$ docker rmi -f 7d9495d03763

# 用name刪除image
$ docker rmi -f docker-whale
  • 通過(guò)docker run來(lái)pull image
$ docker run ***/docker-whale
Unable to find image '***/docker-whale:latest' locally
latest: Pulling from ***/docker-whale

e190868d63f8: Already exists 
909cd34c6fd7: Already exists 
0b9bfabab7c1: Already exists 
a3ed95caeb02: Already exists 
00bf65475aba: Already exists 
c57b6bcc83e3: Already exists 
8978f6879e2f: Already exists 
8eed3712d2cf: Already exists 
2bb8a73a1829: Already exists 
Digest: sha256:18a6032f5726bdd6cea7c15faa02d9dcf1f12ff591c42ca15fc95c0d83de04fc
Status: Downloaded newer image for ***/docker-whale:latest
 ________________________________________ 
/ Would it help if I got out and pushed? \
|                                        |
\ -- Princess Leia Organa                /
 ---------------------------------------- 
    \
     \
      \     
                    ##        .            
              ## ## ##       ==            
           ## ## ## ##      ===            
       /""""""""""""""""___/ ===        
  ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~   
       \______ o          __/            
        \    \        __/             
          \____\______/ 
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末铺厨,一起剝皮案震驚了整個(gè)濱河市缎玫,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌解滓,老刑警劉巖赃磨,帶你破解...
    沈念sama閱讀 216,651評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異洼裤,居然都是意外死亡邻辉,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)值骇,“玉大人莹菱,你說(shuō)我怎么就攤上這事≈ù瘢” “怎么了道伟?”我有些...
    開(kāi)封第一講書人閱讀 162,931評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)使碾。 經(jīng)常有香客問(wèn)我蜜徽,道長(zhǎng),這世上最難降的妖魔是什么票摇? 我笑而不...
    開(kāi)封第一講書人閱讀 58,218評(píng)論 1 292
  • 正文 為了忘掉前任拘鞋,我火速辦了婚禮,結(jié)果婚禮上矢门,老公的妹妹穿的比我還像新娘盆色。我一直安慰自己,他們只是感情好祟剔,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,234評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布隔躲。 她就那樣靜靜地躺著,像睡著了一般峡扩。 火紅的嫁衣襯著肌膚如雪蹭越。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書人閱讀 51,198評(píng)論 1 299
  • 那天教届,我揣著相機(jī)與錄音响鹃,去河邊找鬼。 笑死案训,一個(gè)胖子當(dāng)著我的面吹牛买置,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播强霎,決...
    沈念sama閱讀 40,084評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼忿项,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了城舞?” 一聲冷哼從身側(cè)響起轩触,我...
    開(kāi)封第一講書人閱讀 38,926評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎家夺,沒(méi)想到半個(gè)月后脱柱,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,341評(píng)論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡拉馋,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,563評(píng)論 2 333
  • 正文 我和宋清朗相戀三年榨为,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了惨好。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,731評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡随闺,死狀恐怖日川,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情矩乐,我是刑警寧澤龄句,帶...
    沈念sama閱讀 35,430評(píng)論 5 343
  • 正文 年R本政府宣布,位于F島的核電站散罕,受9級(jí)特大地震影響撒璧,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜笨使,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,036評(píng)論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望僚害。 院中可真熱鬧硫椰,春花似錦、人聲如沸萨蚕。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,676評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)岳遥。三九已至奕翔,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間浩蓉,已是汗流浹背派继。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 32,829評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留捻艳,地道東北人驾窟。 一個(gè)月前我還...
    沈念sama閱讀 47,743評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像认轨,于是被迫代替她去往敵國(guó)和親绅络。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,629評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容