Static Website Using Docker

Create a Docker Container to serve static web content that can be viewed from browser

The goal for this article tis to describe a process for serving static web files from a Docker Container.
The website structure is very simple and consists of only 3 files:

./site/
    style.css
    app.js
    index.html

At the project root there is a Dockerfile:

./
    Dockerfile

The website displays "loading" text . when the JavaScript file is loaded,Hello World is displayed in big red letters:


Hello World

Here is the HTML:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Docker static site</title>
    <script src="app.js"></script>
    <link href="style.css" rel="stylesheet" />
  </head>
  <body>
    Loading
  </body>
</html>

Here is the Dockerfile:

FROM nanoserver/iis
COPY ./site/ /inetpub/wwwroot/

The lines in the Dockerfile are key to getting the web server image created.This file allows us to create a new docker image.The image is used to run a docker container.

The first line specifies the base image. In this case , it is an image with a configured Nano Server with IIS.There are smaller web server image that are usually preferable.

The second line will copy the local project files from the 'site' folder to the wwwroot folder of the nanoserver image.
That is everything needed to get a web server started to serve the web page.To create the image,start with docker build:

> docker build -t webserver-image:v1 .

The docker build command is used to create an image. When it is executed from a command line within the directory of a Dockerfile,the file will be used to create the image.The -t option allows the ability to name and optionally tag the image.In this case,the name is webserver-image with the v1 tag.Tags are generally used to version images.The last argument is the path used to build the image.In this case,it is . which is the current directory.

Running the command will build the image:

> docker build -t webserver-image:v1 .
Sending build context to Docker daemon 26.11kB
Step 1/2 : FROM nanoserver/iis
---> 7eac2eab1a5c
Step 2/2 : COPY ./site/ /inetpub/wwwroot/
---> fca4962e8674
Successfully built fca4962e8674
Successfully tagged webserver-image:v1

The build succeeded.This can be verified by running docker image ls:

> docker image ls
REPOSITORY      TAG IMAGE ID     CREATED       SIZE
webserver-image v1  ffd9f77d44b7 3 seconds ago 1.29GB

If the build doesn't succeed,there may be a few things to double check.This includes making sure the Dockerfile is available,nanoserver images can be pulled,and paths are accurate.
Now that an image is created,it can be used to create a container.This can be done with the docker run comman:

> docker run --name web-dev -d -it -p 80:80 webserver-image:v1

After running the command, the comtainer id will be displayed:

Hide   Copy Code
> docker run --name web-dev -d -it -p 80:80 webserver-image:v1
fde46cdc36fabba3aef8cb3b91856dbd554ff22d63748d486b8eed68a9a3b370

A docker container was created successfully.This can be verified by executing docker container ls:

> docker container ls
CONTAINER ID IMAGE              COMMAND                  CREATED
STATUS        PORTS              NAMES
fde46cdc36fa webserver-image:v1 "c:\\windows\\system32…" 31 seconds ago
Up 25 seconds 0.0.0.0:80->80/tcp web-dev

The container id is displayed(a shorted version of what was shown when executing docker run).The image that was used for the container is also displayed along with when it was created,the status,port and the container name.
The following docker inspect command will display the IP address:

> docker inspect -f "{{ .NetworkSettings.Networks.nat.IPAddress }}" web-dev
172.19.112.171

This IP address is what can be called in a browser to view the page:


Hello world

There is now a working container that serves the web page!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市影晓,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌徽职,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,080評(píng)論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件柿冲,死亡現(xiàn)場(chǎng)離奇詭異呵晚,居然都是意外死亡壶栋,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,422評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門赞咙,熙熙樓的掌柜王于貴愁眉苦臉地迎上來责循,“玉大人,你說我怎么就攤上這事攀操≡悍拢” “怎么了?”我有些...
    開封第一講書人閱讀 157,630評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵速和,是天一觀的道長(zhǎng)歹垫。 經(jīng)常有香客問我,道長(zhǎng)颠放,這世上最難降的妖魔是什么排惨? 我笑而不...
    開封第一講書人閱讀 56,554評(píng)論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮慈迈,結(jié)果婚禮上若贮,老公的妹妹穿的比我還像新娘省有。我一直安慰自己,他們只是感情好谴麦,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,662評(píng)論 6 386
  • 文/花漫 我一把揭開白布蠢沿。 她就那樣靜靜地躺著,像睡著了一般匾效。 火紅的嫁衣襯著肌膚如雪舷蟀。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,856評(píng)論 1 290
  • 那天面哼,我揣著相機(jī)與錄音野宜,去河邊找鬼。 笑死魔策,一個(gè)胖子當(dāng)著我的面吹牛匈子,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播闯袒,決...
    沈念sama閱讀 39,014評(píng)論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼虎敦,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了政敢?” 一聲冷哼從身側(cè)響起其徙,我...
    開封第一講書人閱讀 37,752評(píng)論 0 268
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎喷户,沒想到半個(gè)月后唾那,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,212評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡褪尝,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,541評(píng)論 2 327
  • 正文 我和宋清朗相戀三年闹获,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片恼五。...
    茶點(diǎn)故事閱讀 38,687評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡昌罩,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出灾馒,到底是詐尸還是另有隱情茎用,我是刑警寧澤,帶...
    沈念sama閱讀 34,347評(píng)論 4 331
  • 正文 年R本政府宣布睬罗,位于F島的核電站轨功,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏容达。R本人自食惡果不足惜古涧,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,973評(píng)論 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望花盐。 院中可真熱鬧羡滑,春花似錦菇爪、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,777評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至职祷,卻和暖如春氏涩,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背有梆。 一陣腳步聲響...
    開封第一講書人閱讀 32,006評(píng)論 1 266
  • 我被黑心中介騙來泰國(guó)打工是尖, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人泥耀。 一個(gè)月前我還...
    沈念sama閱讀 46,406評(píng)論 2 360
  • 正文 我出身青樓饺汹,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親爆袍。 傳聞我的和親對(duì)象是個(gè)殘疾皇子首繁,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,576評(píng)論 2 349

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

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,312評(píng)論 0 10
  • 七絕?慶祝十九大勝利閉幕 秋高氣爽風(fēng)清朗,畿輔國(guó)賢計(jì)共商陨囊。 破浪乘風(fēng)帆正競(jìng),“特色”道路奔“小康”夹攒!
    白云之外閱讀 240評(píng)論 0 0
  • 選自己想扔掉的蜘醋,在意的,能觸動(dòng)自己的咏尝,自己承受不了的压语,不想要的。留下的是自己的舒適區(qū)编检。選兩個(gè)胎食,記住先后順序,主要性...
    綻蕊向陽閱讀 7,686評(píng)論 0 1
  • 《媽在家不遠(yuǎn)》 ——遠(yuǎn)在他鄉(xiāng)的游子吉斌 寫于2016年5月8日母親節(jié) 媽媽允懂, 今天是您的節(jié)日 兒女...
    刀鋒勇士閱讀 108評(píng)論 2 1