Docker——初級學(xué)習(xí)

Docker——初級學(xué)習(xí)

標(biāo)簽:docker


學(xué)習(xí)內(nèi)容

  1. 學(xué)習(xí)鏡像泽疆、容器和虛擬機(jī)的區(qū)別
  2. 學(xué)習(xí)使用Dockerfile來定義一個容器
  3. 學(xué)習(xí)將本地容器push佛南,再從遠(yuǎn)程pull

鏡像、容器和虛擬機(jī)

鏡像與容器

A container is launched by running an image. An image is an executable package that includes everything needed to run an application--the code, a runtime, libraries, environment variables, and configuration files.

通過運(yùn)行一個鏡像來發(fā)起一個容器。鏡像是一個可執(zhí)行的包灰蛙,包含了需要運(yùn)行一個應(yīng)用的所有東西圣贸,如代碼殴泰、運(yùn)行時間于宙、庫浮驳、環(huán)境變量以及配置文件等。

A container is a runtime instance of an image--what the image becomes in memory when executed (that is, an image with state, or a user process). You can see a list of your running containers with the command, docker ps, just as you would in Linux.

容器是鏡像的運(yùn)行時實例限煞。鏡像被執(zhí)行時抹恳,是在內(nèi)存中的。

容器與虛擬機(jī)

A container runs natively on Linux and shares the kernel of the host machine with other containers. It runs a discrete process, taking no more memory than any other executable, making it lightweight.

容器在Linux上本地運(yùn)行署驻,并與其他容器共享主機(jī)的內(nèi)核。它運(yùn)行一個離散進(jìn)程健霹,不占用任何其他可執(zhí)行文件更多內(nèi)存旺上,從而使其輕巧。

By contrast, a virtual machine (VM) runs a full-blown “guest” operating system with virtual access to host resources through a hypervisor. In general, VMs provide an environment with more resources than most applications need.

相反糖埋,虛擬機(jī)運(yùn)行成熟的“guest”操作系統(tǒng)宣吱,并通過虛擬機(jī)管理程序來對主機(jī)資源進(jìn)行訪問。通常瞳别,VM為環(huán)境提供的資源比大多數(shù)應(yīng)用程序所需要的資源更多征候。

準(zhǔn)備Docker環(huán)境

測試Docker版本

    docker --version #確定docker版本
    docker info (or docker version) #查看更多細(xì)節(jié)

測試Docker安裝

  • 通過運(yùn)行一個簡單的Docker鏡像(hello-world)來測試安裝可工作
    docker run hello-world
    Unable to find image 'hello-world:latest' locally
    latest: Pulling from library/hello-world
    ca4f61b1923c: Pull complete
    Digest: sha256:ca0eeb6fb05351dfc8759c20733c91def84cb8007aa89a5bf606bc8b315b9fc7
    Status: Downloaded newer image for hello-world:latest
    Hello from Docker!
    This message shows that your installation appears to be working correctly.
    ...
  • 列出下載到本地的hello-world鏡像
    docker image ls
  • 列出hello-world容器,如果容器還在運(yùn)行祟敛,則不需要--all
    docker container ls --all

用Dockerfile來定義容器

  • 創(chuàng)建一個新的文件夾疤坝,在次文件夾下新建名為Dockerfile的文件,文件內(nèi)的內(nèi)容如下:
# Use an official Python runtime as a parent image
FROM python:2.7-slim

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]
  • 創(chuàng)建新的兩個文件requirements.txt和app.py
from flask import Flask
from redis import Redis, RedisError
import os
import socket

# Connect to Redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)

app = Flask(__name__)

@app.route("/")
def hello():
    try:
        visits = redis.incr("counter")
    except RedisError:
        visits = "<i>cannot connect to Redis, counter disabled</i>"

    html = "<h3>Hello {name}!</h3>" \
           "<b>Hostname:</b> {hostname}<br/>" \
           "<b>Visits:</b> {visits}"
    return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80)
  • 構(gòu)建app
    進(jìn)入到新建的文件夾后馆铁,可以看到這里有三個文件
$ ls
Dockerfile      app.py          requirements.txt

然后運(yùn)行構(gòu)建命令跑揉,創(chuàng)建一個docker鏡像,使用--tag來命名

docker build --tag=friendlyhello .
  • 運(yùn)行app
docker run -p 4000:80 friendlyhello                   
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:80/ (Press CTRL+C to quit)

在瀏覽器中輸入http://localhost:4000可以看到結(jié)果埠巨,也可以使用curl

 curl http://localhost:4000

<h3>Hello World!</h3><b>Hostname:</b> 8fc990912a14<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>

也可以在后臺運(yùn)行:

docker run -d -p 4000:80 friendlyhello

使用docker container stop來停止進(jìn)程:

共享鏡像

登陸Docker

$ docker login

標(biāo)記鏡像

docker tag image username/repository:tag

例如:

docker tag friendlyhello xiaoyaomakevin/get-started:part2

Publish鏡像

docker push username/repository:tag

例如

docker push xiaoyaomakevin/get-started:part2

從遠(yuǎn)程端拉取并運(yùn)行鏡像

docker run -p 4000:80 username/repository:tag

例如:

docker run -p 4000:80 xiaoyaomakevin/get-started:part2
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末历谍,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子辣垒,更是在濱河造成了極大的恐慌望侈,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件勋桶,死亡現(xiàn)場離奇詭異脱衙,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)哥遮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進(jìn)店門岂丘,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人眠饮,你說我怎么就攤上這事奥帘。” “怎么了仪召?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵寨蹋,是天一觀的道長松蒜。 經(jīng)常有香客問我,道長已旧,這世上最難降的妖魔是什么秸苗? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮运褪,結(jié)果婚禮上惊楼,老公的妹妹穿的比我還像新娘。我一直安慰自己秸讹,他們只是感情好檀咙,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著璃诀,像睡著了一般弧可。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上劣欢,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天棕诵,我揣著相機(jī)與錄音,去河邊找鬼凿将。 笑死校套,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的丸相。 我是一名探鬼主播搔确,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼灭忠!你這毒婦竟也來了膳算?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤弛作,失蹤者是張志新(化名)和其女友劉穎涕蜂,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體映琳,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡机隙,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了萨西。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片有鹿。...
    茶點(diǎn)故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖谎脯,靈堂內(nèi)的尸體忽然破棺而出葱跋,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布娱俺,位于F島的核電站稍味,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏荠卷。R本人自食惡果不足惜模庐,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望油宜。 院中可真熱鬧掂碱,春花似錦、人聲如沸慎冤。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽粪薛。三九已至,卻和暖如春搏恤,著一層夾襖步出監(jiān)牢的瞬間违寿,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工熟空, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留藤巢,地道東北人。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓息罗,卻偏偏與公主長得像掂咒,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子迈喉,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評論 2 355

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